Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add callback to run #547

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api.js
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,8 @@ dbmigrate.prototype = {
/**
* Executes the default routine.
*/
run: function () {
load('run')(this.internals, this.config);
run: function (callback) {
load('run')(this.internals, this.config, callback);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not a valid definition, everything needs to be a promise in the first place. callbacks are just second class citizens and just supported for backwards compatibility.

}
};

Expand Down
27 changes: 16 additions & 11 deletions lib/commands/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ var log = require('db-migrate-shared').log;
var optimist = require('optimist');
var load = require('./');
var transition = load('transition');
Promise = require('bluebird');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

never require as global


function run (internals, config) {
function run (internals, config, callback) {
var action = internals.argv._.shift();
var folder = action.split(':');

action = folder[0];
var toExecute = null;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

always move definitions to the top of the current scope

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also do not initialize something with null just leave it alone when you want to have it published for the scope but filled later.


switch (action) {
case 'transition':
Expand All @@ -20,10 +22,10 @@ function run (internals, config) {
internals.matching = folder[1];
internals.migrationMode = folder[1];
}
load('create-migration')(internals, config);
toExecute = load('create-migration');
break;
case 'sync':
var executeSync = load('sync');
toExecute = load('sync');

if (internals.argv._.length === 0) {
log.error('Missing sync destination!');
Expand All @@ -38,7 +40,6 @@ function run (internals, config) {
internals.migrationMode = folder[1];
}

executeSync(internals, config);
break;
case 'up':
case 'down':
Expand All @@ -60,11 +61,9 @@ function run (internals, config) {
}

if (action === 'up') {
var executeUp = load('up');
executeUp(internals, config);
toExecute = load('up');
} else {
var executeDown = load('down');
executeDown(internals, config);
toExecute = load('down');
}
break;

Expand All @@ -73,7 +72,7 @@ function run (internals, config) {
log.info('Please enter a valid command, i.e. db:create|db:drop');
} else {
internals.mode = folder[1];
load('db')(internals, config);
toExecute = load('db');
}
break;
case 'seed':
Expand All @@ -86,9 +85,9 @@ function run (internals, config) {
}

internals.argv._.shift();
load('undo-seed')(internals, config);
toExecute = load('undo-seed');
} else {
load('seed')(internals, config);
toExecute = load('seed');
}
break;

Expand All @@ -112,6 +111,12 @@ function run (internals, config) {
}
break;
}

if (toExecute) {
toExecute(internals, config, callback);
} else {
callback();
}
}

module.exports = run;