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
72 changes: 36 additions & 36 deletions src/migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ var StepFileReader = require('./steps').Reader;
var StepVersionCollection = require('./steps').VersionCollection;
var utilities = require('./utils/utility-functions');

function Migration(dbConfig) {
function Migration(dbConfig, options) {
assert.notEqual(dbConfig.migrationCollection, null);

this.options = options;
this.dbConfig = dbConfig;
this.steps = [];
this.migrationFiles = [];
Expand All @@ -32,7 +33,7 @@ var validate = function(cb) {

docs.forEach(function(dbStep, index){
if(this.steps[index]){
this.steps[index].status = statuses.skipped;
this.steps[index].status = statuses.skipped;

if(!_steps[dbStep.id] || (dbStep.order && dbStep.order != _steps[dbStep.id].order)){
this.steps[index].status = statuses.error;
Expand Down Expand Up @@ -91,7 +92,7 @@ var rollback = function(cb, error) {
}
}.bind(this)
}.bind(this)),

function(err, results){
this.steps = merge(this.steps, reverseSteps.reverse());
cb(err || error);
Expand Down Expand Up @@ -128,49 +129,48 @@ Migration.prototype.migrate = function(doneCb) {
this.steps.push(_step);
}.bind(this));

new MongoConnection(this.dbConfig).connect(function(err, db){
assert.equal(err, null);
this.db = db;

validate.call(this, function(err){
new MongoConnection(this.dbConfig, this.options).connect(function(err, db){
assert.equal(err, null);
this.db = db;
validate.call(this, function(err){
if(err){
return callback(err);
return callback(err);
}
async.series(
this.steps.map(function(step){
return function(cb){
if(step.status === statuses.skipped){
step.status = statuses.skipped;
cb();
}else if(step.status === statuses.pending){
step.up(db, function(err){
if(err){
step.status = statuses.error;
return cb("[" + step.id + "] unable to complete migration: " + err);
}

this.db.collection(this.collection).insert(new StepVersionCollection(step.id, step.checksum, step.order, new Date()), function(err){
if(err){
step.status = statuses.error;
return cb("[" + step.id + "] failed to save migration version: " + err);
}
step.status = statuses.ok;
cb();
});
}.bind(this));
return function(cb){
if(step.status === statuses.skipped){
step.status = statuses.skipped;
cb();
}else if(step.status === statuses.pending){
step.up(db, function(err){
if(err){
step.status = statuses.error;
return cb("[" + step.id + "] unable to complete migration: " + err);
}
}.bind(this)

this.db.collection(this.collection).insert(new StepVersionCollection(step.id, step.checksum, step.order, new Date()), function(err){
if(err){
step.status = statuses.error;
return cb("[" + step.id + "] failed to save migration version: " + err);
}
step.status = statuses.ok;
cb();
});
}.bind(this));
}
}.bind(this)
}.bind(this)),

function(err){
if(err){
rollback.call(this, callback, err);
}else{
callback();
}
if(err){
rollback.call(this, callback, err);
}else{
callback();
}
}.bind(this)
);
}.bind(this));
}.bind(this));
}.bind(this));
};

Expand Down
18 changes: 15 additions & 3 deletions src/utils/mongo-connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ var assert = require('assert');

var MongoClient = require('mongodb').MongoClient;

function MongoConnection(config){

function MongoConnection(config, options){
this.options = options;

if(config.mongoUri){
this.connectionUri = config.mongoUri;
}else{
Expand All @@ -20,7 +21,18 @@ function MongoConnection(config){
}

MongoConnection.prototype.connect = function(cb){
MongoClient.connect(this.connectionUri || this.getConnectionUri(), cb);
MongoClient.connect(this.connectionUri || this.getConnectionUri(), this.options || null,
function(err, db) {
if(this.options && this.options.pass && this.options.user) {
db.authenticate(this.options.user, this.options.pass, function(err) {
if(err) {
return cb(err);
}
})
}

return cb(err, db);
}.bind(this));
}

MongoConnection.prototype.getConnectionUri = function(){
Expand Down