Skip to content

Commit

Permalink
handling missing migrations with grace and truth
Browse files Browse the repository at this point in the history
  • Loading branch information
timgit committed Mar 3, 2017
1 parent ef7c755 commit bfa0bc4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ class Db {
migrate(version, uninstall) {
let migration = migrations.get(this.config.schema, version, uninstall);

if(!migration){
let errorMessage = `Migration to version ${version} failed because it could not be found. Your database may have been upgraded by a newer version of pg-boss`;
return Promise.reject(new Error(errorMessage));
}

return Promise.each(migration.commands, command => this.executeSql(command))
.then(() => migration.version);
}
Expand Down
10 changes: 10 additions & 0 deletions test/migrationTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,14 @@ describe('migration', function() {
});
});

it('migrating to non-existent version fails gracefully', function(finished){

contractor.create()
.then(() => db.migrate('¯\_(ツ)_/¯'))
.catch(error => {
assert(error.message.indexOf('could not be found') > -1);
finished();
});
});

});

0 comments on commit bfa0bc4

Please sign in to comment.