From 2ce6ee6e055e80222a640d5efd7001fef9000129 Mon Sep 17 00:00:00 2001 From: Andres Olave Date: Fri, 29 Jul 2016 10:20:23 +0300 Subject: [PATCH 1/8] Add Support for Db based persistence. A Store persists of file name, direction & time - each execution is persisted --- bin/migrate | 29 ++++++-- lib/adapters/filestore.js | 46 ++++++++++++ lib/migrate.js | 4 +- lib/migration.js | 8 +- lib/set.js | 151 +++++++++++++++++++++++++------------- test/basic.js | 111 ++++++++++++++++------------ test/issue-33.js | 7 +- 7 files changed, 246 insertions(+), 110 deletions(-) create mode 100644 lib/adapters/filestore.js diff --git a/bin/migrate b/bin/migrate index f77fd15..843235f 100755 --- a/bin/migrate +++ b/bin/migrate @@ -41,6 +41,9 @@ var usage = [ , ' --state-file set path to state file (migrations/.migrate)' , ' --template-file set path to template file to use for new migrations' , ' --date-format set a date format to use for new migration filenames' + , ' --db-adapter select the db adapter to use: filestore (default) or mongo' + , ' --db-url url for the db adapter to use' + , ' --db-table table to use (migrations)' , ' Commands:' , '' , ' down [name] migrate down till given migration' @@ -99,6 +102,12 @@ while (args.length) { case '--state-file': options.stateFile = required(); break; + case '--db-adapter': + options.dbAdapter = required(); + case '--db-url': + options.dbUrl = required(); + case '--db-table': + options.dbTable = required(); case '--template-file': template = fs.readFileSync(required()); break; @@ -192,17 +201,25 @@ function create(name) { */ function performMigration(direction, migrationName) { - var state = options.stateFile || join('migrations', '.migrate'); - var set = migrate.load(state, 'migrations'); + var dbAdapterType = options.dbAdapter || 'filestore'; + var dbAdapter = require('../lib/adapters/' + dbAdapterType); + + var store; + if (dbAdapterType === 'filestore') { + var state = options.stateFile || join('migrations', '.migrate'); + store = new dbAdapter(state); + } else { + var dbUrl = options.dbUrl; + var dbTable = options.dbTable || 'migrations'; + store = new dbAdapter(dbUrl, dbTable); + } + + var set = migrate.load(store, 'migrations'); set.on('migration', function(migration, direction){ log(direction, migration.title); }); - var migrationPath = migrationName - ? join('migrations', migrationName) - : migrationName; - set[direction](migrationName, function (err) { if (err) { log('error', err); diff --git a/lib/adapters/filestore.js b/lib/adapters/filestore.js new file mode 100644 index 0000000..f598782 --- /dev/null +++ b/lib/adapters/filestore.js @@ -0,0 +1,46 @@ +/** + * Created by arolave on 26/07/2016. + */ +var fs = require('fs'); +var readline = require('readline') + +module.exports = FileStore; + +function FileStore(path) { + this.path = path; +} + +FileStore.prototype.load = function (callback) { + var stream = require('fs').createReadStream(this.path, {encoding: 'utf8'}); + var lineReader = readline.createInterface({ + input: stream + }); + + var arr = []; + + lineReader.on('line', function (line) { + console.log(line); + try { + arr.push(JSON.parse(line)); + } catch(err) { + callback(err); + } + + }); + + lineReader.on('close', function (err) { + callback(err, arr); + }); + + stream.on('error', function (err) { + callback(err); + }); +}; + +FileStore.prototype.save = function (migration, callback) { + fs.appendFile(this.path, JSON.stringify(migration) + '\n', 'utf8', callback); +}; + +FileStore.prototype.reset = function (callback) { + fs.unlink(this.path, callback) +}; \ No newline at end of file diff --git a/lib/migrate.js b/lib/migrate.js index 04756e3..2c2b19e 100644 --- a/lib/migrate.js +++ b/lib/migrate.js @@ -35,9 +35,9 @@ function migrate(title, up, down) { } } -exports.load = function (stateFile, migrationsDirectory) { +exports.load = function (store, migrationsDirectory) { - var set = new Set(stateFile); + var set = new Set(store); var dir = path.resolve(migrationsDirectory); fs.readdirSync(dir).filter(function(file){ diff --git a/lib/migration.js b/lib/migration.js index 361e828..a5e1661 100644 --- a/lib/migration.js +++ b/lib/migration.js @@ -15,4 +15,10 @@ function Migration(title, up, down) { this.title = title; this.up = up; this.down = down; -} \ No newline at end of file +} + +Migration.prototype.run = function (operation, callback) { + this.operation = operation; + this.timestamp = new Date(); + return this[operation](callback); +}; \ No newline at end of file diff --git a/lib/set.js b/lib/set.js index 1bf3947..e2f4dbd 100644 --- a/lib/set.js +++ b/lib/set.js @@ -20,17 +20,16 @@ var EventEmitter = require('events').EventEmitter module.exports = Set; /** - * Initialize a new migration `Set` with the given `path` + * Initialize a new migration `Set` with the given `store` * which is used to store data between migrations. * - * @param {String} path + * @param {String} store * @api private */ -function Set(path) { +function Set(store) { this.migrations = []; - this.path = path; - this.pos = 0; + this.store = store; }; /** @@ -58,10 +57,10 @@ Set.prototype.addMigration = function(title, up, down){ * @api public */ -Set.prototype.save = function(fn){ - var self = this - , json = JSON.stringify(this); - fs.writeFile(this.path, json, function(err){ +Set.prototype.save = function(migration, fn){ + var self = this; + + this.store.save(migration, function (err) { if (err) return fn(err); self.emit('save'); @@ -70,23 +69,50 @@ Set.prototype.save = function(fn){ }; /** - * Load the migration data and call `fn(err, obj)`. + * Load all the migration data and call `fn(err, obj)`. * * @param {Function} fn * @return {Type} * @api public */ -Set.prototype.load = function(fn){ +Set.prototype.load = function(fn) { this.emit('load'); - fs.readFile(this.path, 'utf8', function(err, json){ - if (err) return fn(err); - try { - fn(null, JSON.parse(json)); - } catch (err) { - fn(err); - } + var self = this; + this.store.load(function(err, allExecutedMigrations) { + self.allExecutedMigrations = allExecutedMigrations; + fn(err, allExecutedMigrations); }); +} + +/** + * Load the net migration state data and call `fn(err, obj)`. + * Net migration data means all the 'ups' minus the 'downs' + * + * @param {Function} fn + * @return {Type} + * @api public + */ +Set.prototype.loadNet = function (fn) { + var self = this; + this.load(function(err) { + if (err) + return fn(err); + + var netMigrationsObj = self.allExecutedMigrations.reduce( + function (obj, m) { + if (obj[m.title] && m.operation == 'down') { + delete obj[m.title]; + } else { + obj[m.title] = true; + } + return obj; + }, + {} + ); + self.appliedMigrationTitles = Object.keys(netMigrationsObj).sort(); + fn(null, self.appliedMigrationTitles) + }) }; /** @@ -125,29 +151,14 @@ Set.prototype.migrate = function(direction, migrationName, fn){ migrationName = null; } var self = this; - this.load(function(err, obj){ + this.loadNet(function(err){ if (err) { if ('ENOENT' != err.code) return fn(err); - } else { - self.pos = obj.pos; } self._migrate(direction, migrationName, fn); }); }; -/** - * Get index of given migration in list of migrations - * - * @api private - */ - - function positionOfMigration(migrations, filename) { - for(var i=0; i < migrations.length; ++i) { - if (migrations[i].title == filename) return i; - } - return -1; - } - /** * Perform migration. * @@ -156,39 +167,75 @@ Set.prototype.migrate = function(direction, migrationName, fn){ Set.prototype._migrate = function(direction, migrationName, fn){ var self = this - , migrations - , migrationPos; + , newMigrations; + + //Consider using lodash for get*Migrations Function - much cleaner + function getUpMigrations(appliedTitles, allMigrations) { + if (!appliedTitles) + return allMigrations.slice(0); //clone - if (!migrationName) { - migrationPos = direction == 'up' ? this.migrations.length : 0; - } else if ((migrationPos = positionOfMigration(this.migrations, migrationName)) == -1) { - return fn(new Error("Could not find migration: " + migrationName)); + return allMigrations.filter(function (m) { + return appliedTitles.indexOf(m.title) === -1; + }); // filter out applied migrations } - switch (direction) { - case 'up': - migrations = this.migrations.slice(this.pos, migrationPos+1); - break; - case 'down': - migrations = this.migrations.slice(migrationPos, this.pos).reverse(); - break; + function getDownMigrations(appliedTitles, allMigrations) { + if (!allMigrations || !appliedTitles) + return []; + + var migrationTitles = allMigrations.map(function (m) { + return m.title + }); + + return appliedTitles.reverse().reduce(function (buff, title) { + var i = migrationTitles.indexOf(title); + if (i >= 0) + buff.push(allMigrations[i]); + return buff; + }, + []); // Select applied migrations, use reduce in case the files have been deleted + } + + function takeUntil(arr, val, prop) { + var buff = []; + for(var i = 0, found = false; i Date: Sun, 31 Jul 2016 19:08:10 +0300 Subject: [PATCH 2/8] Split node-migrate & the filestore Reusable tests change migration script --- bin/migrate | 127 ++++++----- lib/adapters/filestore.js | 46 ---- package.json | 3 +- test/basic.js | 212 ------------------ test/common/basic.js | 204 +++++++++++++++++ .../fixtures/basic/1-add-guy-ferrets.js | 0 .../fixtures/basic/2-add-girl-ferrets.js | 0 .../fixtures/basic/3-add-emails.js | 0 test/{ => common}/fixtures/db.js | 0 .../fixtures/issue-33/1-migration.js | 0 .../fixtures/issue-33/2-migration.js | 0 .../fixtures/issue-33/3-migration.js | 0 test/common/issue-33.js | 53 +++++ test/index.js | 14 ++ test/issue-33.js | 57 ----- 15 files changed, 345 insertions(+), 371 deletions(-) delete mode 100644 lib/adapters/filestore.js delete mode 100644 test/basic.js create mode 100644 test/common/basic.js rename test/{ => common}/fixtures/basic/1-add-guy-ferrets.js (100%) rename test/{ => common}/fixtures/basic/2-add-girl-ferrets.js (100%) rename test/{ => common}/fixtures/basic/3-add-emails.js (100%) rename test/{ => common}/fixtures/db.js (100%) rename test/{ => common}/fixtures/issue-33/1-migration.js (100%) rename test/{ => common}/fixtures/issue-33/2-migration.js (100%) rename test/{ => common}/fixtures/issue-33/3-migration.js (100%) create mode 100644 test/common/issue-33.js create mode 100644 test/index.js delete mode 100644 test/issue-33.js diff --git a/bin/migrate b/bin/migrate index 843235f..4930627 100755 --- a/bin/migrate +++ b/bin/migrate @@ -31,26 +31,71 @@ var cwd; * Usage information. */ -var usage = [ +var migrationCliHandlers = [ + { + usageOptions: [ + ' -c, --chdir change the working directory' + , ' --template-file set path to template file to use for new migrations' + , ' --date-format set a date format to use for new migration filenames' + , ' --store select the db adapter to use: file (default) or mongo' + ], + parseArg : function (arg) { + switch (arg) { + case '-h': + case '--help': + case 'help': + console.log(usage); + process.exit(); + return true; + case '-c': + case '--chdir': + process.chdir(cwd = required()); + return true; + case '--store': + options.store = required(); + return true; + case '--template-file': + template = fs.readFileSync(required()); + return true; + case '--date-format': + options.dateFormat = required(); + return true; + } + } + } +]; + +['file', 'mongo'].forEach(function(storeType) { + try { + var Store = require('node-migrate-' + storeType + 'store'); + // validStoreTypes.push(storeType); + if (Store.cliHandler) + migrationCliHandlers.push(Store.cliHandler) + } catch(e) { + //ignore missing types + } +}); + +var usageArr = [ '' , ' Usage: migrate [options] [command]' , '' , ' Options:' - , '' - , ' -c, --chdir change the working directory' - , ' --state-file set path to state file (migrations/.migrate)' - , ' --template-file set path to template file to use for new migrations' - , ' --date-format set a date format to use for new migration filenames' - , ' --db-adapter select the db adapter to use: filestore (default) or mongo' - , ' --db-url url for the db adapter to use' - , ' --db-table table to use (migrations)' + , '']; + +migrationCliHandlers.forEach(function (handler) { + usageArr = usageArr.concat(handler.usageOptions) +}); + +usageArr = usageArr.concat([ , ' Commands:' , '' , ' down [name] migrate down till given migration' , ' up [name] migrate up till given migration (the default command)' , ' create [title] create a new migration file with optional [title]' , '' -].join('\n'); +]); +var usage = usageArr.join('\n'); /** * Migration template. @@ -84,42 +129,22 @@ function abort(msg) { } // parse arguments - var arg; while (args.length) { arg = args.shift(); - switch (arg) { - case '-h': - case '--help': - case 'help': - console.log(usage); - process.exit(); - break; - case '-c': - case '--chdir': - process.chdir(cwd = required()); - break; - case '--state-file': - options.stateFile = required(); - break; - case '--db-adapter': - options.dbAdapter = required(); - case '--db-url': - options.dbUrl = required(); - case '--db-table': - options.dbTable = required(); - case '--template-file': - template = fs.readFileSync(required()); - break; - case '--date-format': - options.dateFormat = required(); - break; - default: - if (options.command) { - options.args.push(arg); - } else { - options.command = arg; - } + var optionKey; + for (var i = 0; i Date: Sun, 31 Jul 2016 19:08:43 +0300 Subject: [PATCH 3/8] update gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index e9af0b1..1b4f654 100644 --- a/.gitignore +++ b/.gitignore @@ -2,5 +2,6 @@ node_modules *.sock .migrate +*.iml migrations/ From 4443977d73496f833a70429a454ef827d3cd9e14 Mon Sep 17 00:00:00 2001 From: Andres Olave Date: Sun, 31 Jul 2016 20:11:58 +0300 Subject: [PATCH 4/8] Fixes for running tests from mongo --- .gitignore | 1 + bin/migrate | 3 +-- lib/migrate.js | 2 +- package.json | 2 +- test/common/basic.js | 11 +++++------ test/common/issue-33.js | 3 +-- test/index.js | 2 +- 7 files changed, 11 insertions(+), 13 deletions(-) diff --git a/.gitignore b/.gitignore index 1b4f654..3379668 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ node_modules *.sock .migrate *.iml +.idea migrations/ diff --git a/bin/migrate b/bin/migrate index 4930627..d567342 100755 --- a/bin/migrate +++ b/bin/migrate @@ -67,8 +67,7 @@ var migrationCliHandlers = [ ['file', 'mongo'].forEach(function(storeType) { try { - var Store = require('node-migrate-' + storeType + 'store'); - // validStoreTypes.push(storeType); + var Store = require('migrate-' + storeType + 'store'); if (Store.cliHandler) migrationCliHandlers.push(Store.cliHandler) } catch(e) { diff --git a/lib/migrate.js b/lib/migrate.js index 2c2b19e..55dfac9 100644 --- a/lib/migrate.js +++ b/lib/migrate.js @@ -28,7 +28,7 @@ function migrate(title, up, down) { migrate.set = new Set(title); // no migration path } else if (!migrate.set) { - throw new Error('must invoke migrate(path) before running migrations'); + throw new Error('must invoke migrate(store) before running migrations'); // run migrations } else { return migrate.set; diff --git a/package.json b/package.json index 6408d4f..5a00998 100644 --- a/package.json +++ b/package.json @@ -24,6 +24,6 @@ "license": "MIT", "dependencies": { "dateformat": "^1.0.12", - "node-migrate-filestore": "latest" + "migrate-filestore": "https://github.com/sloops77/node-migrate-filestore#728be28a731a0892e350149ce30424bffbefc664" } } diff --git a/test/common/basic.js b/test/common/basic.js index 8b5b45b..0317df8 100644 --- a/test/common/basic.js +++ b/test/common/basic.js @@ -51,8 +51,7 @@ module.exports = function (BASE, storeUnderTest) { } beforeEach(function () { - var Store = require('node-migrate-' + storeUnderTest.storeType + 'store'); - store = new (Function.prototype.bind.apply(Store, [null].concat(storeUnderTest.args))); + store = new (Function.prototype.bind.apply(storeUnderTest.Store, [null].concat(storeUnderTest.args))); set = migrate.load(store, BASE); }); @@ -61,7 +60,7 @@ module.exports = function (BASE, storeUnderTest) { store.reset(done) }); - it('should handle basic migration using ' + storeUnderTest.storeType, function (done) { + it('should handle basic migration using', function (done) { set.up(function (err) { assert.ifError(err); assertPets(function () { @@ -89,7 +88,7 @@ module.exports = function (BASE, storeUnderTest) { }); - it('should add a new migration using ' + storeUnderTest.storeType, function (done) { + it('should add a new migration using', function (done) { set.addMigration('add dogs', function (next) { db.pets.push({name: 'simon'}); db.pets.push({name: 'suki'}); @@ -118,7 +117,7 @@ module.exports = function (BASE, storeUnderTest) { }); - it('should emit events using ' + storeUnderTest.storeType, function (done) { + it('should emit events using', function (done) { set.addMigration('4-adjust-emails.js', function (next) { db.pets.forEach(function (pet) { if (pet.email) @@ -171,7 +170,7 @@ module.exports = function (BASE, storeUnderTest) { }); - it('should migrate to named migration using ' + storeUnderTest.storeType, function (done) { + it('should migrate to named migration using', function (done) { set.up('1-add-guy-ferrets.js', function (err) { assert.ifError(err); assertFirstMigration(function () { diff --git a/test/common/issue-33.js b/test/common/issue-33.js index 103d531..327cb12 100644 --- a/test/common/issue-33.js +++ b/test/common/issue-33.js @@ -13,8 +13,7 @@ module.exports = function(BASE, storeUnderTest) { var store; beforeEach(function () { - var Store = require('node-migrate-' + storeUnderTest.storeType + 'store'); - store = new (Function.prototype.bind.apply(Store, [null].concat(storeUnderTest.args))); + store = new (Function.prototype.bind.apply(storeUnderTest.Store, [null].concat(storeUnderTest.args))); set = migrate.load(store, BASE); }); diff --git a/test/index.js b/test/index.js index 11ecfed..d357d0e 100644 --- a/test/index.js +++ b/test/index.js @@ -2,7 +2,7 @@ var path = require('path'); var getStoreUnderTest = function(base) { var STATE = path.join(base, '.migrate'); - return {storeType: 'file', args: [{stateFile: STATE}]} + return {Store: require('migrate-filestore'), args: [{stateFile: STATE}]} }; var BASIC_BASE = path.join(__dirname, 'common', 'fixtures', 'basic'); From cc7d7fd5b82379776707f19e8c9cfc6f74843690 Mon Sep 17 00:00:00 2001 From: Andres Olave Date: Mon, 1 Aug 2016 13:32:39 +0300 Subject: [PATCH 5/8] add support for locks --- lib/migration.js | 6 +++++- lib/set.js | 21 +++++++++++++++------ test/common/basic.js | 34 ++++++++++++++++++++++++++++++++-- 3 files changed, 52 insertions(+), 9 deletions(-) diff --git a/lib/migration.js b/lib/migration.js index a5e1661..eba5068 100644 --- a/lib/migration.js +++ b/lib/migration.js @@ -20,5 +20,9 @@ function Migration(title, up, down) { Migration.prototype.run = function (operation, callback) { this.operation = operation; this.timestamp = new Date(); - return this[operation](callback); + try { + return this[operation](callback); + } catch (e) { + callback(e); + } }; \ No newline at end of file diff --git a/lib/set.js b/lib/set.js index e2f4dbd..4778c82 100644 --- a/lib/set.js +++ b/lib/set.js @@ -81,7 +81,12 @@ Set.prototype.load = function(fn) { var self = this; this.store.load(function(err, allExecutedMigrations) { self.allExecutedMigrations = allExecutedMigrations; - fn(err, allExecutedMigrations); + fn(err, allExecutedMigrations, function(doneCallback) { + if (self.store.unlock) + self.store.unlock(doneCallback); + else + doneCallback() + }); }); } @@ -95,9 +100,9 @@ Set.prototype.load = function(fn) { */ Set.prototype.loadNet = function (fn) { var self = this; - this.load(function(err) { + this.load(function(err, res, done) { if (err) - return fn(err); + return fn(err, res, done); var netMigrationsObj = self.allExecutedMigrations.reduce( function (obj, m) { @@ -111,7 +116,7 @@ Set.prototype.loadNet = function (fn) { {} ); self.appliedMigrationTitles = Object.keys(netMigrationsObj).sort(); - fn(null, self.appliedMigrationTitles) + fn(null, self.appliedMigrationTitles, done) }) }; @@ -151,11 +156,15 @@ Set.prototype.migrate = function(direction, migrationName, fn){ migrationName = null; } var self = this; - this.loadNet(function(err){ + this.loadNet(function(err, migrations, done){ if (err) { if ('ENOENT' != err.code) return fn(err); } - self._migrate(direction, migrationName, fn); + self._migrate(direction, migrationName, function(err, res) { + done(function () { + fn(err, res); + }); + }); }); }; diff --git a/test/common/basic.js b/test/common/basic.js index 0317df8..259bf76 100644 --- a/test/common/basic.js +++ b/test/common/basic.js @@ -9,9 +9,10 @@ module.exports = function (BASE, storeUnderTest) { var store; function assertNumMigrations(num, cb) { - set.loadNet(function (err, migrations) { + set.loadNet(function (err, migrations, done) { + assert.ifError(err); assert.equal(migrations.length, num); - cb(); + done(cb) }) } @@ -60,6 +61,35 @@ module.exports = function (BASE, storeUnderTest) { store.reset(done) }); + it('should handle exceptions ok', function (done) { + + set.up(function (err) { + assert.ifError(err); + assertPets(function () { + set.addMigration('add dogs', function (next) { + console.log('up error'); + next('error'); + }); + + set.up(function (err) { + assert(err !== null); + assertPets(function () { + set.migrations.pop(); + set.addMigration('add dogs', function (next) { + console.log('up error'); + throw new Error('error'); + }); + set.up(function (err) { + assert(err !== null); + assertPets(done); + }); + }); + }); + }); + + }); + }); + it('should handle basic migration using', function (done) { set.up(function (err) { assert.ifError(err); From 3082f550a49330adc820fb71bc1a2fdf79ab4007 Mon Sep 17 00:00:00 2001 From: Andres Olave Date: Mon, 1 Aug 2016 15:22:24 +0300 Subject: [PATCH 6/8] fix module load --- bin/migrate | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/migrate b/bin/migrate index d567342..f66457e 100755 --- a/bin/migrate +++ b/bin/migrate @@ -226,7 +226,7 @@ function create(name) { function performMigration(direction, migrationName) { var storeType = options.store || 'file'; - var Store = require('node-migrate-' + storeType + 'store'); + var Store = require('migrate-' + storeType + 'store'); var store = new (Function.prototype.bind.apply(Store, [null].concat(options))); From 5b586467060f7bea8d4a8dd7b0e97d1baeb69f3f Mon Sep 17 00:00:00 2001 From: Andres Olave Date: Mon, 1 Aug 2016 15:52:13 +0300 Subject: [PATCH 7/8] split out executable from js for cli --- bin/migrate | 254 +--------------------------------------------- bin/migratecli.js | 254 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 255 insertions(+), 253 deletions(-) create mode 100644 bin/migratecli.js diff --git a/bin/migrate b/bin/migrate index f66457e..eea849a 100755 --- a/bin/migrate +++ b/bin/migrate @@ -1,255 +1,3 @@ #!/usr/bin/env node -/** - * Module dependencies. - */ - -var migrate = require('../') - , join = require('path').join - , fs = require('fs') - , dateFormat = require('dateformat'); - -/** - * Arguments. - */ - -var args = process.argv.slice(2); - -/** - * Option defaults. - */ - -var options = { args: [] }; - -/** - * Current working directory. - */ - -var cwd; - -/** - * Usage information. - */ - -var migrationCliHandlers = [ - { - usageOptions: [ - ' -c, --chdir change the working directory' - , ' --template-file set path to template file to use for new migrations' - , ' --date-format set a date format to use for new migration filenames' - , ' --store select the db adapter to use: file (default) or mongo' - ], - parseArg : function (arg) { - switch (arg) { - case '-h': - case '--help': - case 'help': - console.log(usage); - process.exit(); - return true; - case '-c': - case '--chdir': - process.chdir(cwd = required()); - return true; - case '--store': - options.store = required(); - return true; - case '--template-file': - template = fs.readFileSync(required()); - return true; - case '--date-format': - options.dateFormat = required(); - return true; - } - } - } -]; - -['file', 'mongo'].forEach(function(storeType) { - try { - var Store = require('migrate-' + storeType + 'store'); - if (Store.cliHandler) - migrationCliHandlers.push(Store.cliHandler) - } catch(e) { - //ignore missing types - } -}); - -var usageArr = [ - '' - , ' Usage: migrate [options] [command]' - , '' - , ' Options:' - , '']; - -migrationCliHandlers.forEach(function (handler) { - usageArr = usageArr.concat(handler.usageOptions) -}); - -usageArr = usageArr.concat([ - , ' Commands:' - , '' - , ' down [name] migrate down till given migration' - , ' up [name] migrate up till given migration (the default command)' - , ' create [title] create a new migration file with optional [title]' - , '' -]); -var usage = usageArr.join('\n'); - -/** - * Migration template. - */ - -var template = [ - '\'use strict\'' - , '' - , 'exports.up = function(next) {' - , ' next();' - , '};' - , '' - , 'exports.down = function(next) {' - , ' next();' - , '};' - , '' -].join('\n'); - -// require an argument - -function required() { - if (args.length) return args.shift(); - abort(arg + ' requires an argument'); -} - -// abort with a message - -function abort(msg) { - console.error(' %s', msg); - process.exit(1); -} - -// parse arguments -var arg; -while (args.length) { - arg = args.shift(); - var optionKey; - for (var i = 0; i change the working directory' + , ' --template-file set path to template file to use for new migrations' + , ' --date-format set a date format to use for new migration filenames' + , ' --store select the db adapter to use: file (default) or mongo' + ], + parseArg : function (arg) { + switch (arg) { + case '-h': + case '--help': + case 'help': + console.log(usage); + process.exit(); + return true; + case '-c': + case '--chdir': + process.chdir(cwd = required()); + return true; + case '--store': + options.store = required(); + return true; + case '--template-file': + template = fs.readFileSync(required()); + return true; + case '--date-format': + options.dateFormat = required(); + return true; + } + } + } +]; + +['file', 'mongo'].forEach(function(storeType) { + try { + var Store = require('migrate-' + storeType + 'store'); + if (Store.cliHandler) + migrationCliHandlers.push(Store.cliHandler) + } catch(e) { + //ignore missing types + } +}); + +var usageArr = [ + '' + , ' Usage: migrate [options] [command]' + , '' + , ' Options:' + , '']; + +migrationCliHandlers.forEach(function (handler) { + usageArr = usageArr.concat(handler.usageOptions) +}); + +usageArr = usageArr.concat([ + , ' Commands:' + , '' + , ' down [name] migrate down till given migration' + , ' up [name] migrate up till given migration (the default command)' + , ' create [title] create a new migration file with optional [title]' + , '' +]); +var usage = usageArr.join('\n'); + +/** + * Migration template. + */ + +var template = [ + '\'use strict\'' + , '' + , 'exports.up = function(next) {' + , ' next();' + , '};' + , '' + , 'exports.down = function(next) {' + , ' next();' + , '};' + , '' +].join('\n'); + +// require an argument + +function required() { + if (args.length) return args.shift(); + abort(arg + ' requires an argument'); +} + +// abort with a message + +function abort(msg) { + console.error(' %s', msg); + process.exit(1); +} + +// parse arguments +var arg; +while (args.length) { + arg = args.shift(); + var optionKey; + for (var i = 0; i Date: Mon, 1 Aug 2016 17:29:13 +0300 Subject: [PATCH 8/8] fixes for running cli --- bin/migratecli.js | 43 +++++++++++++++++++++---------------------- 1 file changed, 21 insertions(+), 22 deletions(-) diff --git a/bin/migratecli.js b/bin/migratecli.js index 79b9199..bfdb238 100644 --- a/bin/migratecli.js +++ b/bin/migratecli.js @@ -25,6 +25,22 @@ var options = { args: [] }; var cwd; +/** + * Migration template. + */ +var template = [ + '\'use strict\'' + , '' + , 'exports.up = function(next) {' + , ' next();' + , '};' + , '' + , 'exports.down = function(next) {' + , ' next();' + , '};' + , '' +].join('\n'); + /** * Usage information. */ @@ -50,14 +66,12 @@ var migrationCliHandlers = [ process.chdir(cwd = required()); return true; case '--store': - options.store = required(); - return true; + return 'store'; case '--template-file': template = fs.readFileSync(required()); return true; case '--date-format': - options.dateFormat = required(); - return true; + return 'dateFormat' } } } @@ -94,23 +108,6 @@ usageArr = usageArr.concat([ ]); var usage = usageArr.join('\n'); -/** - * Migration template. - */ - -var template = [ - '\'use strict\'' - , '' - , 'exports.up = function(next) {' - , ' next();' - , '};' - , '' - , 'exports.down = function(next) {' - , ' next();' - , '};' - , '' -].join('\n'); - // require an argument function required() { @@ -132,8 +129,10 @@ while (args.length) { var optionKey; for (var i = 0; i