Skip to content

Commit

Permalink
support migration scripts relax#68
Browse files Browse the repository at this point in the history
  • Loading branch information
magalhas committed Oct 9, 2015
1 parent 4c24635 commit afb05c2
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
20 changes: 13 additions & 7 deletions app.js
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();
38 changes: 38 additions & 0 deletions lib/server/migrate.js
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;
});
}
13 changes: 13 additions & 0 deletions lib/server/models/migration.js
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 added migrations/.gitempty
Empty file.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"react-google-maps": "^2.0.3",
"relax-framework": "^0.0.7",
"rimraf": "^2.4.3",
"semver": "^5.0.3",
"sharp": "^0.11.2",
"slug": "^0.9.1",
"soundmanager2": "git://github.com/relax/SoundManager2.git",
Expand Down

0 comments on commit afb05c2

Please sign in to comment.