Skip to content

Commit 722cab5

Browse files
Tom KirkpatrickTom Kirkpatrick
authored andcommitted
fix: Set app.migrating to true when called via migrateByName
1 parent 718a100 commit 722cab5

File tree

5 files changed

+31
-6
lines changed

5 files changed

+31
-6
lines changed

lib/models/migration.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ module.exports = function(Migration, options) {
6262
assert(typeof name === 'string', 'The to argument must be a string, not ' + typeof name);
6363
assert(typeof cb === 'function', 'The cb argument must be a function, not ' + typeof cb);
6464

65+
if (Migration.app.migrating) {
66+
Migration.log.warn('Unable to start migration: already running');
67+
process.nextTick(function() {
68+
cb();
69+
});
70+
return cb.promise;
71+
}
72+
73+
Migration.hrstart = process.hrtime();
74+
Migration.app.migrating = true;
75+
6576
Migration.log.info(name, 'running.');
6677
const scriptPath = path.resolve(path.join(Migration.migrationsDir, name));
6778

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module.exports = {
22
up: function(dataSource, next) {
3-
next();
3+
process.nextTick(() => next())
44
},
55
down: function(dataSource, next) {
6-
next();
6+
process.nextTick(() => next())
77
}
88
};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module.exports = {
22
up: function(dataSource, next) {
3-
next();
3+
process.nextTick(() => next())
44
},
55
down: function(dataSource, next) {
6-
next();
6+
process.nextTick(() => next())
77
}
88
};
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module.exports = {
22
up: function(dataSource, next) {
3-
next();
3+
process.nextTick(() => next())
44
},
55
down: function(dataSource, next) {
6-
next();
6+
process.nextTick(() => next())
77
}
88
};

test/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,20 @@ describe('loopback db migrate', function() {
8585
.catch(done);
8686
});
8787

88+
describe('migrateByName', function() {
89+
it('should set a property on app to indicate that migration is running', function(done) {
90+
var self = this;
91+
expect(app.migrating).to.be.undefined;
92+
var promise = app.models.Migration.migrateByName('0002-somechanges.js');
93+
expect(app.migrating).to.be.true;
94+
promise.then(function() {
95+
expect(app.migrating).to.be.undefined;
96+
done();
97+
})
98+
.catch(done);
99+
});
100+
});
101+
88102
describe('migrate', function() {
89103
it('should set a property on app to indicate that migrations are running', function(done) {
90104
var self = this;

0 commit comments

Comments
 (0)