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 db:migrate:pending to check for pending migrations, closes #165 #379

Closed
wants to merge 2 commits 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
32 changes: 32 additions & 0 deletions lib/tasks/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,38 @@ module.exports = {
}
},

'db:migrate:pending': {
descriptions: {
'short': 'List pending migrations.',
'long': [
'The command lists pending migrations and exits with non-zero status if',
'there are any.'
]
},

task: function () {
return getMigrator('migration').then(function (migrator) {
return ensureCurrentMetaSchema(migrator).then(function () {
return migrator.pending();
}).then(function (migrations) {
if (migrations.length === 0) {
console.log('No pending migrations, database schema up to date.');
process.exit(0);
} else {
console.log('Pending migrations:');
migrations.forEach(function (migration) {
console.log(migration.file);
});
process.exit(3);
}
}).catch(function (err) {
console.error(err);
process.exit(1);
Copy link

Choose a reason for hiding this comment

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

process.exit(255);?
I wouldn't exit with the same exit code as if it were pending migrations.

});
});
}
},

'db:seed': {
descriptions: {
'short': 'Run specified seeder.',
Expand Down
81 changes: 81 additions & 0 deletions test/db/migrate/pending.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
'use strict';

var expect = require('expect.js');
var Support = require(__dirname + '/../../support');
var helpers = require(__dirname + '/../../support/helpers');
var gulp = require('gulp');
var _ = require('lodash');

describe(Support.getTestDialectTeaser('db:migrate:pending'), function () {
describe('when there are migrations', function () {
var prepare = function (callback, options) {
options = _.assign({ config: {} }, options || {});
options.cli = options.cli || {};
_.defaults(options.cli, { pipeStdout: true });

var config = helpers.getTestConfig();
var configContent = 'module.exports = ' + JSON.stringify(config);
var result = '';

return gulp
.src(Support.resolveSupportPath('tmp'))
.pipe(helpers.clearDirectory())
.pipe(helpers.runCli('init'))
.pipe(helpers.removeFile('config/config.json'))
.pipe(helpers.overwriteFile(configContent, 'config/config.js'))
.pipe(helpers.runCli('db:migrate:pending', options.cli))
.on('data', function (data) {
result += data.toString();
})
.on('end', function () {
callback(null, result);
});
};

it('passes and reports no pending migrations', function (done) {
prepare(function (_, stdout) {
expect(stdout).to.contain('No pending migrations');
done();
}, {
config: { logging: true }
});
});
});

describe('when there are no migrations', function () {
var prepare = function (callback, options) {
options = _.assign({ config: {} }, options || {});
options.cli = options.cli || {};
_.defaults(options.cli, { pipeStdout: true });

var config = helpers.getTestConfig();
var configContent = 'module.exports = ' + JSON.stringify(config);
var result = '';

return gulp
.src(Support.resolveSupportPath('tmp'))
.pipe(helpers.clearDirectory())
.pipe(helpers.runCli('init'))
.pipe(helpers.removeFile('config/config.json'))
.pipe(helpers.copyMigration('createPerson.js'))
.pipe(helpers.overwriteFile(configContent, 'config/config.js'))
.pipe(helpers.runCli('db:migrate:pending', options.cli))
.on('data', function (data) {
result += data.toString();
})
.on('end', function () {
callback(null, result);
});
};

it('fails with a not 0 exit code and reports pending migrations', function (done) {
prepare(function (_, stdout) {
expect(stdout).to.contain('createPerson.js');
done();
}, {
cli: { exitCode: 3 },
config: { logging: true }
});
});
});
});
2 changes: 1 addition & 1 deletion test/support/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ module.exports = {
if (options.exitCode) {
try {
expect(err).to.be.ok();
expect(err.code).to.equal(1);
expect(err.code).to.equal(options.exitCode);
callback(null, result);
} catch (e) {
callback(e, result);
Expand Down