1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

Reduce duplication when adding a new migration.

This commit is contained in:
Jari Bakken 2014-11-10 15:58:11 +01:00
parent 55a4f476c9
commit d30bc6a54c
3 changed files with 20 additions and 13 deletions

View File

@ -43,7 +43,7 @@ npm test
1. Create `migrations/sql/NNN-your-migration-name.up.sql` with your change in SQL.
2. Create `migrations/sql/NNN-your-migration-name.down.sql` with the rollback of your change in SQL.
3. Run `db-migrate create your-migration-name` and edit the generated file to run the above SQL files.
3. Run `db-migrate create your-migration-name` and edit the generated file to have this line: `module.exports = require('../lib/migrationRunner').create('NNN-your-migration-name');`
4. Run `npm run db-migrate-up`.
5. Generate LB artifact using `scripts/generate-liquibase-artifact` (TODO: make this internal)

18
lib/migrationRunner.js Normal file
View File

@ -0,0 +1,18 @@
var fs = require('fs');
var util = require('util');
var path = require('path');
var runMigration = function(path, db, callback) {
db.runSql(fs.readFileSync(path, {encoding: 'utf8'}), callback);
};
module.exports = {
create: function (name) {
var format = path.resolve(__dirname, '../migrations/sql/%s.%s.sql');
return {
up: runMigration.bind(null, util.format(format, name, 'up')),
down: runMigration.bind(null, util.format(format, name, 'down'))
};
}
};

View File

@ -1,12 +1 @@
var fs = require('fs');
var util = require('util');
var path = require('path').resolve(__dirname, 'sql/001-initial-schema.%s.sql');
exports.up = function(db, callback) {
var content = fs.readFileSync(util.format(path, 'up'), {encoding: 'utf8'});
db.runSql(content, callback);
};
exports.down = function(db, callback) {
db.runSql(fs.readFileSync(util.format(path, 'down'), {encoding: 'utf8'}), callback);
};
module.exports = require('../lib/migrationRunner').create('001-initial-schema');