Skip to content

Commit

Permalink
feat(sync): add sync mode
Browse files Browse the repository at this point in the history
Refers to #383
Resolves #313
Refers to #222
  • Loading branch information
wzrdtales committed Sep 30, 2016
1 parent 376fdc3 commit fa1a161
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 12 deletions.
103 changes: 95 additions & 8 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,36 @@ dbmigrate.prototype = {
}.bind(this)).asCallback(callback);
},

/**
* Executes up a given number of migrations or a specific one.
*
* Defaults to up all migrations if no count is given.
*/
sync: function(specification, opts, callback) {

if (arguments.length > 0) {
if (typeof(specification) === 'string') {

this.internals.argv.destination = specification;
}

if (typeof(opts) === 'string') {

this.internals.migrationMode = opts;
this.internals.matching = opts;
}
else if (typeof(opts) === 'function') {

callback = opts;
}
}

return Promise.fromCallback(function(callback) {

executeSync(this.internals, this.config, callback);
}.bind(this)).asCallback(callback);
},

/**
* Executes down for all currently migrated migrations.
*/
Expand Down Expand Up @@ -520,7 +550,8 @@ function setDefaultArgv(internals, isModule) {
'ignore-completed-migrations': false
})
.usage(
'Usage: db-migrate [up|down|reset|create|db|seed|transition] [[dbname/]migrationName|all] [options]'
'Usage: db-migrate [up|down|reset|sync|create|db|seed|transition] ' +
'[[dbname/]migrationName|all] [options]'
)

.describe('env',
Expand Down Expand Up @@ -558,14 +589,16 @@ function setDefaultArgv(internals, isModule) {
.string('config')

.describe('sql-file',
'Automatically create two sql files for up and down statements in /sqls and generate the javascript code that loads them.'
'Automatically create two sql files for up and down statements in ' +
'/sqls and generate the javascript code that loads them.'
)
.boolean('sql-file')

.describe('coffee-file', 'Create a coffeescript migration file')
.boolean('coffee-file')
.describe('ignore-on-init',
'Create files that will run only if ignore-on-init in the env is set to false (currently works onlt with SQL)'
'Create files that will run only if ignore-on-init in the env is set ' +
'to false (currently works onlt with SQL)'
).boolean('ignore-on-init')

.describe('migration-table',
Expand Down Expand Up @@ -883,6 +916,42 @@ function executeUp(internals, config, callback) {
});
}

function executeSync(internals, config, callback) {

migrationHook(internals)
.then(function() {

var Migrator = require('./lib/migrator.js');
var index = require('./connect');

if (!internals.argv.count) {
internals.argv.count = Number.MAX_VALUE;
}
index.connect({
config: config.getCurrent().settings,
internals: internals
}, Migrator, function(err, migrator) {
assert.ifError(err);

if (internals.locTitle)
migrator.migrationsDir = path.resolve(internals.argv['migrations-dir'],
internals.locTitle);
else
migrator.migrationsDir = path.resolve(internals.argv['migrations-dir']);

internals.migrationsDir = migrator.migrationsDir;

migrator.driver.createMigrationsTable(function(err) {
assert.ifError(err);
log.verbose('migration table created');

migrator.sync(internals.argv, internals.onComplete.bind(this,
migrator, internals, callback));
});
});
});
}

function executeDown(internals, config, callback) {

migrationHook(internals)
Expand Down Expand Up @@ -1095,6 +1164,24 @@ function run(internals, config) {
}
executeCreateMigration(internals, config);
break;
case 'sync':

if (internals.argv._.length === 0) {

log.error('Missing sync destination!');
process.exit(1);
}

internals.argv.count = Number.MAX_VALUE;
internals.argv.destination = internals.argv._.shift().toString();

if (folder[1]) {
internals.matching = folder[1];
internals.migrationMode = folder[1];
}

executeSync(internals, config);
break;
case 'up':
case 'down':
case 'reset':
Expand All @@ -1104,10 +1191,9 @@ function run(internals, config) {

if (internals.argv._.length > 0) {
if (action === 'down') {
log.info(
'Ignoring migration name for down migrations. Use --count to control how many down migrations are run.'
);
internals.argv.destination = null;

internals.argv.count = internals.argv.count || Number.MAX_VALUE;
internals.argv.destination = internals.argv._.shift().toString();
} else {
internals.argv.destination = internals.argv._.shift().toString();
}
Expand Down Expand Up @@ -1166,7 +1252,8 @@ function run(internals, config) {
}
else {

log.error('Invalid Action: Must be [up|down|create|reset|seed|db].');
log.error('Invalid Action: Must be [up|down|create|reset|sync|seed|' +
'db|transition].');
optimist.showHelp();
process.exit(1);
}
Expand Down
31 changes: 28 additions & 3 deletions lib/migrator.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,35 @@ Migrator.prototype = {
if (dbmUtil.isFunction(funcOrOpts)) {
return funcOrOpts(this.driver, callback);
} else {
this.downToBy(funcOrOpts.count, callback);
this.downToBy(funcOrOpts.destination, funcOrOpts.count, callback);
}
},

sync: function(funcOrOpts, callback) {

var self = this;

Migration.loadFromDatabase(self.migrationsDir, self._driver,
self.internals, function(err, completedMigrations) {
if (err) { callback(err); return; }

var mode = dbmUtil.syncMode(
completedMigrations,
funcOrOpts.destination
);
if(mode === 1) {

log.info('Syncing upwards.');
self.up(funcOrOpts, callback);
}
else {

log.info('Syncing downwards.');
self.down(funcOrOpts, callback);
}
});
},

upToBy: function(partialName, count, callback) {
var self = this;
Migration.loadFromFilesystem(self.migrationsDir, self.internals, function(err, allMigrations) {
Expand Down Expand Up @@ -173,12 +198,12 @@ Migrator.prototype = {
});
},

downToBy: function(count, callback) {
downToBy: function(partialName, count, callback) {
var self = this;
Migration.loadFromDatabase(self.migrationsDir, self._driver, self.internals, function(err, completedMigrations) {
if (err) { return callback(err); }

var toRun = dbmUtil.filterDown(completedMigrations, count);
var toRun = dbmUtil.filterDown(completedMigrations, partialName, count);

if (toRun.length === 0) {
log.info('No migrations to run');
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"dependencies": {
"balanced-match": "^0.4.2",
"bluebird": "^3.1.1",
"db-migrate-shared": "^1.0.2",
"db-migrate-shared": "^1.1.2",
"dotenv": "^2.0.0",
"final-fs": "^1.6.0",
"inflection": "^1.10.0",
Expand Down

0 comments on commit fa1a161

Please sign in to comment.