Skip to content

Commit 9797be0

Browse files
committed
allow skipping order as well
1 parent 3728343 commit 9797be0

2 files changed

Lines changed: 33 additions & 8 deletions

File tree

src/migration.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,16 @@ var validate = function(cb) {
3030
this.db.collection(this.collection).find({}, {}, {order : 1}).toArray(function(err, docs){
3131
assert.equal(err, null);
3232
var _steps = utilities.arrayToObject(this.steps, 'id');
33-
docs.forEach(function(dbStep, index){
33+
docs.forEach(function(dbStep){
34+
var index = this.steps.findIndex(function(step){return step.id === dbStep.id});
3435
if(this.steps[index]){
35-
this.steps[index].status = statuses.skipped;
36-
console.log(this.options);
37-
if(!this.options.ignoreOrder && (!_steps[dbStep.id] || (dbStep.order && dbStep.order != _steps[dbStep.id].order))){
36+
this.steps[index].status = statuses.skipped;
37+
if(!_steps[dbStep.id] || (dbStep.order !== undefined && dbStep.order != _steps[dbStep.id].order)){
38+
if(this.options.ignoreOrder) return;
3839
this.steps[index].status = statuses.error;
3940
cb("[" + dbStep.id + "] was already migrated on [" + dbStep.date + "] in a different order. Database order[" + dbStep.order + "] - Current migration on this order[" + this.steps[index].id + "]");
40-
}else if(!this.options.ignoreChecksum && (dbStep.checksum != this.steps[index].checksum)){
41+
}else if(dbStep.checksum != _steps[dbStep.id].checksum){
42+
if(this.options.ignoreChecksum) return;
4143
this.steps[index].status = statuses.error;
4244
cb("[" + dbStep.id + "] was already migrated on [" + dbStep.date + "] in a different version. Database version[" + dbStep.checksum + "] - Current version[" + this.steps[index].checksum + "]");
4345
}

test/migration-itest.js

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ describe('Mongration.Migration', function() {
151151
migration2.add(files[1]); // "edited": same id, different content
152152

153153
migration2.migrate(function(err, result) {
154-
console.log(err,result);
155154
should.not.exist(err);
156155
result.should.be.an('array');
157156
result.should.have.lengthOf(1);
@@ -162,6 +161,29 @@ describe('Mongration.Migration', function() {
162161
});
163162
});
164163

164+
it('does rollback if order changed', function(done) {
165+
var migration = new Migration(config);
166+
var files = getFiles('migrations/migration-order');
167+
migration.add(files[1]);
168+
169+
migration.migrate(function(err, result) {
170+
should.not.exist(err);
171+
var migration2 = new Migration(config);
172+
migration2.add([files[0], files[1]]);
173+
174+
migration2.migrate(function(err, result) {
175+
err.should.match(/already migrated(.*)in a different order/)
176+
result.should.be.an('array');
177+
result.should.have.lengthOf(2);
178+
result.should.have.deep.property('[0].id', '1');
179+
result.should.have.deep.property('[0].status', 'not-run');
180+
result.should.have.deep.property('[1].id', '2');
181+
result.should.have.deep.property('[1].status', 'error');
182+
done();
183+
});
184+
});
185+
});
186+
165187
it('does skip if order changed', function(done) {
166188
var migration = new Migration(config);
167189
var files = getFiles('migrations/migration-order');
@@ -172,11 +194,12 @@ describe('Mongration.Migration', function() {
172194
migration2.add([files[0], files[1]]);
173195

174196
migration2.migrate(function(err, result) {
175-
console.log(err, result);
176197
should.not.exist(err);
177198
result.should.be.an('array');
178199
result.should.have.lengthOf(2);
179-
result.should.have.deep.property('[1].id', '1');
200+
result.should.have.deep.property('[0].id', '1');
201+
result.should.have.deep.property('[1].id', '2');
202+
result.should.have.deep.property('[0].status', 'ok');
180203
result.should.have.deep.property('[1].status', 'skipped');
181204
done();
182205
});

0 commit comments

Comments
 (0)