From d30bc6a54cbc0ae0b445d8860bf13e0d075f4704 Mon Sep 17 00:00:00 2001 From: Jari Bakken Date: Mon, 10 Nov 2014 15:58:11 +0100 Subject: [PATCH] Reduce duplication when adding a new migration. --- README.md | 2 +- lib/migrationRunner.js | 18 ++++++++++++++++++ migrations/20141020151056-initial-schema.js | 13 +------------ 3 files changed, 20 insertions(+), 13 deletions(-) create mode 100644 lib/migrationRunner.js diff --git a/README.md b/README.md index 6d93f30cc1..9f02925078 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/lib/migrationRunner.js b/lib/migrationRunner.js new file mode 100644 index 0000000000..f8145f8a2c --- /dev/null +++ b/lib/migrationRunner.js @@ -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')) + }; + } +}; diff --git a/migrations/20141020151056-initial-schema.js b/migrations/20141020151056-initial-schema.js index 86a37e0911..3859a6a6dc 100644 --- a/migrations/20141020151056-initial-schema.js +++ b/migrations/20141020151056-initial-schema.js @@ -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'); \ No newline at end of file