Skip to content

Commit

Permalink
fix: Set app.migrating to true when called via migrateByName
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom Kirkpatrick authored and Tom Kirkpatrick committed Feb 1, 2017
1 parent 718a100 commit 722cab5
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 6 deletions.
11 changes: 11 additions & 0 deletions lib/models/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,17 @@ module.exports = function(Migration, options) {
assert(typeof name === 'string', 'The to argument must be a string, not ' + typeof name);
assert(typeof cb === 'function', 'The cb argument must be a function, not ' + typeof cb);

if (Migration.app.migrating) {
Migration.log.warn('Unable to start migration: already running');
process.nextTick(function() {
cb();
});
return cb.promise;
}

Migration.hrstart = process.hrtime();
Migration.app.migrating = true;

Migration.log.info(name, 'running.');
const scriptPath = path.resolve(path.join(Migration.migrationsDir, name));

Expand Down
4 changes: 2 additions & 2 deletions test/fixtures/simple-app/server/migrations/0001-initialize.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
up: function(dataSource, next) {
next();
process.nextTick(() => next())
},
down: function(dataSource, next) {
next();
process.nextTick(() => next())
}
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
up: function(dataSource, next) {
next();
process.nextTick(() => next())
},
down: function(dataSource, next) {
next();
process.nextTick(() => next())
}
};
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module.exports = {
up: function(dataSource, next) {
next();
process.nextTick(() => next())
},
down: function(dataSource, next) {
next();
process.nextTick(() => next())
}
};
14 changes: 14 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ describe('loopback db migrate', function() {
.catch(done);
});

describe('migrateByName', function() {
it('should set a property on app to indicate that migration is running', function(done) {
var self = this;
expect(app.migrating).to.be.undefined;
var promise = app.models.Migration.migrateByName('0002-somechanges.js');
expect(app.migrating).to.be.true;
promise.then(function() {
expect(app.migrating).to.be.undefined;
done();
})
.catch(done);
});
});

describe('migrate', function() {
it('should set a property on app to indicate that migrations are running', function(done) {
var self = this;
Expand Down

0 comments on commit 722cab5

Please sign in to comment.