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

throw error if a migration method doesn't return a thenable #157

Merged
merged 1 commit into from
Nov 18, 2018
Merged
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
6 changes: 5 additions & 1 deletion src/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ module.exports = class Migration {
}
if (!fun) throw new Error('Could not find migration method: ' + method);
const wrappedFun = this.options.migrations.wrap(fun);
const result = wrappedFun.apply(migration, args);
if (!result || typeof result.then !== 'function') {
throw new Error(`Migration ${this.file} (or wrapper) didn't return a promise`);
}

await wrappedFun.apply(migration, args);
await result;
}
};
31 changes: 29 additions & 2 deletions test/Umzug/execute.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,33 @@ describe('execute', function () {
this.migration.down.restore();
});

it('requires migration methods to return a promise', function () {
helper.clearTmp();
helper
.prepareMigrations(1, { names: ['123-migration'], returnUndefined: true })
.then(() => {
this.migration = require('../tmp/123-migration.js');
this.upStub = sinon.stub(this.migration, 'up').callsFake(Bluebird.resolve);
this.downStub = sinon.stub(this.migration, 'down').callsFake(Bluebird.resolve);
this.logSpy = sinon.spy();
this.umzug = new Umzug({
migrations: { path: join(__dirname, '/../tmp/') },
storageOptions: { path: join(__dirname, '/../tmp/umzug.json') },
logging: this.logSpy,
});
return this.umzug.execute({
migrations: ['123-migration'],
method: 'up',
});
})
.then(() => {
throw new Error('expected migration to fail');
})
.catch(error => {
expect(error.message).to.match(/Migration 123-migration.js (or wrapper) didn't return a promise/i);
});
});

it('runs the up method of the migration', function () {
return this
.migrate('up')
Expand Down Expand Up @@ -180,8 +207,8 @@ describe('coffee-script support', function () {
'\'use strict\'',
'',
'module.exports =',
' up: () ->',
' down: () ->',
' up: () -> Promise.resolve()',
' down: () -> Promise.resolve()',
].join('\n')
);
});
Expand Down
9 changes: 5 additions & 4 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const helper = module.exports = {
}
},

generateDummyMigration: function (name, subDirectories) {
generateDummyMigration: function (name, subDirectories, options = {}) {
let path = join(__dirname, '/tmp/');
if (subDirectories) {
if (!_.isArray(subDirectories)) {
Expand All @@ -40,8 +40,8 @@ const helper = module.exports = {
'\'use strict\';',
'',
'module.exports = {',
' up: function () {},',
' down: function () {}',
` up: function () { return ${options.returnUndefined ? 'undefined' : 'Promise.resolve()'}; },`,
` down: function () { return ${options.returnUndefined ? 'undefined' : 'Promise.resolve()'}; }`,
'};',
].join('\n')
);
Expand All @@ -58,6 +58,7 @@ const helper = module.exports = {
// example 3: ['foo',['foo','bar2']] ==> generates /foo and /foo/bar2
...options || {},
};
const {returnUndefined} = options;

return new Promise((resolve) => {
let names = options.names;
Expand All @@ -68,7 +69,7 @@ const helper = module.exports = {
_.times(count, (i) => {
num++;
names.push(options.names[i] || (num + '-migration'));
helper.generateDummyMigration(names[i], options.directories[i]);
helper.generateDummyMigration(names[i], options.directories[i], {returnUndefined});
});

resolve(names);
Expand Down