forked from relax/relax
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
65 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,22 @@ | ||
import app from './lib/server'; | ||
import config from './config'; | ||
import mongoose from 'mongoose'; | ||
import config from './config'; | ||
import logger from './lib/logger'; | ||
import app from './lib/server'; | ||
import migrate from './lib/server/migrate'; | ||
|
||
// Connect mongoose | ||
if (!config.db) { | ||
throw new Error('Configuration to MongoDB required'); | ||
} | ||
mongoose.connect(config.db.uri, config.db); | ||
|
||
// Start server | ||
var server = app.listen(config.port, () => { | ||
var port = server.address().port; | ||
logger.debug('Listening at port', port); | ||
}); | ||
// Run migrations | ||
migrate() | ||
.then(() => { | ||
// Start server | ||
var server = app.listen(config.port, () => { | ||
var port = server.address().port; | ||
logger.debug('Listening at port', port); | ||
}); | ||
}) | ||
.done(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import {readdirSync} from 'fs'; | ||
import {basename, extname, join} from 'path'; | ||
import Q from 'q'; | ||
import semver from 'semver'; | ||
import logger from '../logger'; | ||
import MigrationModel from './models/migration'; | ||
|
||
const migrationsPath = join(__dirname, '../../migrations'); | ||
|
||
function runMigration (path) { | ||
const migration = require(join(migrationsPath, path)); | ||
return migration() | ||
.then(() => saveMigration(path)) | ||
.then(() => logger.info(`migration ${path} was applied`)); | ||
} | ||
|
||
function saveMigration (path) { | ||
return new MigrationModel({_id: path}).save(); | ||
} | ||
|
||
export default function migrate () { | ||
return Q() | ||
.then(() => { | ||
var promise = Q(); | ||
|
||
readdirSync(migrationsPath) | ||
.map((path) => extname(path) === '.js' ? basename(path) : false) | ||
.filter((path) => path && semver.valid(path.split('-')[0])) | ||
.sort((a, b) => semver.compare(a.split('-')[0], b.split('-')[0])) | ||
.forEach((path) => { | ||
promise = promise | ||
.then(() => MigrationModel.findOne({_id: path}).exec()) | ||
.then((migration) => !migration && runMigration(path)); | ||
}); | ||
|
||
return promise; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import mongoose from 'mongoose'; | ||
|
||
const migrationSchema = new mongoose.Schema({ | ||
_id: { | ||
type: String | ||
}, | ||
when: { | ||
type: Date, | ||
default: Date.now | ||
} | ||
}); | ||
|
||
export default mongoose.model('Migration', migrationSchema); |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters