diff --git a/lib/models/migration.js b/lib/models/migration.js index 7b12379..8303e62 100644 --- a/lib/models/migration.js +++ b/lib/models/migration.js @@ -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)); diff --git a/test/fixtures/simple-app/server/migrations/0001-initialize.js b/test/fixtures/simple-app/server/migrations/0001-initialize.js index 5882cca..5970149 100644 --- a/test/fixtures/simple-app/server/migrations/0001-initialize.js +++ b/test/fixtures/simple-app/server/migrations/0001-initialize.js @@ -1,8 +1,8 @@ module.exports = { up: function(dataSource, next) { - next(); + process.nextTick(() => next()) }, down: function(dataSource, next) { - next(); + process.nextTick(() => next()) } }; diff --git a/test/fixtures/simple-app/server/migrations/0002-somechanges.js b/test/fixtures/simple-app/server/migrations/0002-somechanges.js index 5882cca..5970149 100644 --- a/test/fixtures/simple-app/server/migrations/0002-somechanges.js +++ b/test/fixtures/simple-app/server/migrations/0002-somechanges.js @@ -1,8 +1,8 @@ module.exports = { up: function(dataSource, next) { - next(); + process.nextTick(() => next()) }, down: function(dataSource, next) { - next(); + process.nextTick(() => next()) } }; diff --git a/test/fixtures/simple-app/server/migrations/0003-morechanges.js b/test/fixtures/simple-app/server/migrations/0003-morechanges.js index 5882cca..5970149 100644 --- a/test/fixtures/simple-app/server/migrations/0003-morechanges.js +++ b/test/fixtures/simple-app/server/migrations/0003-morechanges.js @@ -1,8 +1,8 @@ module.exports = { up: function(dataSource, next) { - next(); + process.nextTick(() => next()) }, down: function(dataSource, next) { - next(); + process.nextTick(() => next()) } }; diff --git a/test/test.js b/test/test.js index f8a6521..1d22e24 100644 --- a/test/test.js +++ b/test/test.js @@ -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;