Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,11 @@ If migration step *1-changed-step-sample* was succesfully run, then a developer
"[1-changed-step-sample] was already migrated in a different version. Current version[0685c17d538741a062c488ea776b0576] - Database version[72d9204bee94542118bd99cdde8edc0f]"
```

If there is no way to avoid changes in already executed step files, the *checksum* comparison can be disabled:
```javascript
mydbMigration.disableChecksumComparison();
```


### Migration Order

Expand Down
11 changes: 10 additions & 1 deletion src/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function Migration(dbConfig) {
this.steps = [];
this.migrationFiles = [];
this.collection = dbConfig.migrationCollection;
this.compareChecksums = true;
};

var validate = function(cb) {
Expand All @@ -37,7 +38,7 @@ var validate = function(cb) {
if(!_steps[dbStep.id] || (dbStep.order && dbStep.order != _steps[dbStep.id].order)){
this.steps[index].status = statuses.error;
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 + "]");
}else if(dbStep.checksum != this.steps[index].checksum){
}else if(this.compareChecksums && dbStep.checksum != this.steps[index].checksum){
this.steps[index].status = statuses.error;
cb("[" + dbStep.id + "] was already migrated on [" + dbStep.date + "] in a different version. Database version[" + dbStep.checksum + "] - Current version[" + this.steps[index].checksum + "]");
}
Expand Down Expand Up @@ -110,6 +111,14 @@ Migration.prototype.addAllFromPath = function(dirpath) {
}.bind(this));
};

Migration.prototype.disableChecksumComparison = function () {
this.compareChecksums = false;
};

Migration.prototype.enableChecksumComparison = function () {
this.compareChecksums = true;
};

Migration.prototype.migrate = function(doneCb) {
var callback = function(err){
var resp = this.steps.map(function(step){
Expand Down
21 changes: 21 additions & 0 deletions test/migration-itest.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,27 @@ describe('Mongration.Migration', function() {
});
});

it('does ignore checksum if disabled', function (done) {
var migration = new Migration(config);
var files = getFiles('migrations/rejects-if-edited');
migration.add(files[0]);

migration.migrate(function (err, result) {
should.not.exist(err);
var migration2 = new Migration(config);
migration2.add(files[1]); // "edited": same id, different content
migration2.disableChecksumComparison();

migration2.migrate(function (err, result) {
should.not.exist(err);
result.should.be.an('array');
result.should.have.lengthOf(1);
result[0].should.deep.equal({ id: '1', status: 'skipped' });
done();
});
});
});

// TODO: add test...
// dunno how to trigger error "already migrated in a different order"
it('does rollback if wrong order');
Expand Down
11 changes: 11 additions & 0 deletions test/migrations/ignore-checksum/1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
id: '1',

up: function(db, callback) {
callback();
},

down: function(db, cb) {
cb();
}
};
11 changes: 11 additions & 0 deletions test/migrations/ignore-checksum/edit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
id: '1',

up: function(db, cb2) {
cb2();
},

down: function(db, cb) {
cb();
}
};