From ccac9258e770053ae0c64c223e14231a39970e34 Mon Sep 17 00:00:00 2001 From: GitHub Date: Thu, 27 Jul 2017 14:56:12 +0200 Subject: [PATCH 01/11] added options object --- src/utils/mongo-connection.js | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/utils/mongo-connection.js b/src/utils/mongo-connection.js index ad811e5..eb3b28c 100644 --- a/src/utils/mongo-connection.js +++ b/src/utils/mongo-connection.js @@ -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{ @@ -20,7 +21,7 @@ function MongoConnection(config){ } MongoConnection.prototype.connect = function(cb){ - MongoClient.connect(this.connectionUri || this.getConnectionUri(), cb); + MongoClient.connect(this.connectionUri || this.getConnectionUri(), this.options || {}, cb); } MongoConnection.prototype.getConnectionUri = function(){ From a52b9ad88c260c2bf726e0928e0306806edd4aef Mon Sep 17 00:00:00 2001 From: GitHub Date: Thu, 27 Jul 2017 15:16:14 +0200 Subject: [PATCH 02/11] added options --- src/migration.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/migration.js b/src/migration.js index 988e9d3..a313ac8 100644 --- a/src/migration.js +++ b/src/migration.js @@ -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 = []; @@ -128,7 +129,7 @@ Migration.prototype.migrate = function(doneCb) { this.steps.push(_step); }.bind(this)); - new MongoConnection(this.dbConfig).connect(function(err, db){ + new MongoConnection(this.dbConfig, this.options).connect(function(err, db){ assert.equal(err, null); this.db = db; From 45588dd83ae50092cf52da2793ce23cb5ed55b4d Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 28 Jul 2017 14:45:59 +0200 Subject: [PATCH 03/11] added auth --- src/migration.js | 77 +++++++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 31 deletions(-) diff --git a/src/migration.js b/src/migration.js index a313ac8..2ae535b 100644 --- a/src/migration.js +++ b/src/migration.js @@ -130,48 +130,63 @@ Migration.prototype.migrate = function(doneCb) { }.bind(this)); new MongoConnection(this.dbConfig, this.options).connect(function(err, db){ - assert.equal(err, null); - this.db = db; + var runMigrations = function() { + assert.equal(err, null); + this.db = db; - validate.call(this, function(err){ + 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)); + } + + if(this.options.pass || this.options.user) { + var adminDb = db.admin(); + adminDb.authenticate(this.options.user, this.options.pass, function(err) { + if(err) { + //TODO: handle error + console.log(err) + } + runMigrations() + }) + }else { + runMigrations(); + } }.bind(this)); }; From bf238b3b83ea9d14ea58b195a6445be2f968f495 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 28 Jul 2017 15:11:09 +0200 Subject: [PATCH 04/11] added console log --- src/migration.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/migration.js b/src/migration.js index 2ae535b..b0976ea 100644 --- a/src/migration.js +++ b/src/migration.js @@ -132,6 +132,11 @@ Migration.prototype.migrate = function(doneCb) { new MongoConnection(this.dbConfig, this.options).connect(function(err, db){ var runMigrations = function() { assert.equal(err, null); + + //TODO: handle error + if(err) { + console.log(err); + } this.db = db; validate.call(this, function(err){ From 62e31abd7b6c7fe5976251bec42a3817822b6123 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 28 Jul 2017 15:12:24 +0200 Subject: [PATCH 05/11] removed study db --- src/migration.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/migration.js b/src/migration.js index b0976ea..2ae535b 100644 --- a/src/migration.js +++ b/src/migration.js @@ -132,11 +132,6 @@ Migration.prototype.migrate = function(doneCb) { new MongoConnection(this.dbConfig, this.options).connect(function(err, db){ var runMigrations = function() { assert.equal(err, null); - - //TODO: handle error - if(err) { - console.log(err); - } this.db = db; validate.call(this, function(err){ From 117ba835633c0fd7b7ce80596c067ca75c7c2fb7 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 28 Jul 2017 15:15:25 +0200 Subject: [PATCH 06/11] changed db --- src/migration.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/migration.js b/src/migration.js index 2ae535b..adc8bef 100644 --- a/src/migration.js +++ b/src/migration.js @@ -176,8 +176,7 @@ Migration.prototype.migrate = function(doneCb) { } if(this.options.pass || this.options.user) { - var adminDb = db.admin(); - adminDb.authenticate(this.options.user, this.options.pass, function(err) { + db.authenticate(this.options.user, this.options.pass, function(err) { if(err) { //TODO: handle error console.log(err) From 2e29d70c721473993e508f81ef0af5574dd7c017 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 28 Jul 2017 16:03:18 +0200 Subject: [PATCH 07/11] small changes --- src/migration.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/migration.js b/src/migration.js index adc8bef..f2a5ed7 100644 --- a/src/migration.js +++ b/src/migration.js @@ -131,6 +131,7 @@ Migration.prototype.migrate = function(doneCb) { new MongoConnection(this.dbConfig, this.options).connect(function(err, db){ var runMigrations = function() { + console.log('migrate.runMigrations:', JSON.stringify(err)) assert.equal(err, null); this.db = db; @@ -176,10 +177,11 @@ Migration.prototype.migrate = function(doneCb) { } if(this.options.pass || this.options.user) { - db.authenticate(this.options.user, this.options.pass, function(err) { + var adminDb = db.admin(); + adminDb.authenticate(this.options.user, this.options.pass, function(err) { if(err) { //TODO: handle error - console.log(err) + console.log('adminDb.authenticate:', JSON.stringify(err)) } runMigrations() }) From edfee8a059cfee8bd523acd2e0ebbf2f7bad9955 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 28 Jul 2017 18:54:18 +0200 Subject: [PATCH 08/11] small changes --- src/migration.js | 15 ++++++++------- src/utils/mongo-connection.js | 2 +- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/src/migration.js b/src/migration.js index f2a5ed7..aaa545f 100644 --- a/src/migration.js +++ b/src/migration.js @@ -33,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; @@ -92,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); @@ -130,9 +130,10 @@ Migration.prototype.migrate = function(doneCb) { }.bind(this)); new MongoConnection(this.dbConfig, this.options).connect(function(err, db){ - var runMigrations = function() { - console.log('migrate.runMigrations:', JSON.stringify(err)) - assert.equal(err, null); + console.log('migrate.runMigrations:', JSON.stringify(err)) + assert.equal(err, null); + + var runMigrations = function(db) { this.db = db; validate.call(this, function(err){ @@ -183,10 +184,10 @@ Migration.prototype.migrate = function(doneCb) { //TODO: handle error console.log('adminDb.authenticate:', JSON.stringify(err)) } - runMigrations() + runMigrations(db) }) }else { - runMigrations(); + runMigrations(db); } }.bind(this)); }; diff --git a/src/utils/mongo-connection.js b/src/utils/mongo-connection.js index eb3b28c..3d4fe69 100644 --- a/src/utils/mongo-connection.js +++ b/src/utils/mongo-connection.js @@ -21,7 +21,7 @@ function MongoConnection(config, options){ } MongoConnection.prototype.connect = function(cb){ - MongoClient.connect(this.connectionUri || this.getConnectionUri(), this.options || {}, cb); + MongoClient.connect(this.connectionUri || this.getConnectionUri(), this.options || null, cb); } MongoConnection.prototype.getConnectionUri = function(){ From 45bb93e11ecfd4cbb0e81e511748b6da023ad863 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 28 Jul 2017 19:00:44 +0200 Subject: [PATCH 09/11] small changes --- src/migration.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/migration.js b/src/migration.js index aaa545f..417a100 100644 --- a/src/migration.js +++ b/src/migration.js @@ -130,10 +130,10 @@ Migration.prototype.migrate = function(doneCb) { }.bind(this)); new MongoConnection(this.dbConfig, this.options).connect(function(err, db){ - console.log('migrate.runMigrations:', JSON.stringify(err)) + console.log('migrate.runMigrations:', JSON.stringify(err)); assert.equal(err, null); - var runMigrations = function(db) { + var runMigrations = function() { this.db = db; validate.call(this, function(err){ @@ -175,7 +175,7 @@ Migration.prototype.migrate = function(doneCb) { }.bind(this) ); }.bind(this)); - } + }.bind(this); if(this.options.pass || this.options.user) { var adminDb = db.admin(); @@ -184,10 +184,10 @@ Migration.prototype.migrate = function(doneCb) { //TODO: handle error console.log('adminDb.authenticate:', JSON.stringify(err)) } - runMigrations(db) + runMigrations() }) }else { - runMigrations(db); + runMigrations(); } }.bind(this)); }; From e7f1b1e836cb580eb1c975f03cdf9cab7181cd73 Mon Sep 17 00:00:00 2001 From: GitHub Date: Mon, 31 Jul 2017 11:58:24 +0200 Subject: [PATCH 10/11] removed console logs and change location of auth --- src/migration.js | 20 +------------------- src/utils/mongo-connection.js | 13 ++++++++++++- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/src/migration.js b/src/migration.js index 417a100..56067e3 100644 --- a/src/migration.js +++ b/src/migration.js @@ -130,12 +130,8 @@ Migration.prototype.migrate = function(doneCb) { }.bind(this)); new MongoConnection(this.dbConfig, this.options).connect(function(err, db){ - console.log('migrate.runMigrations:', JSON.stringify(err)); - assert.equal(err, null); - - var runMigrations = function() { + assert.equal(err, null); this.db = db; - validate.call(this, function(err){ if(err){ return callback(err); @@ -175,20 +171,6 @@ Migration.prototype.migrate = function(doneCb) { }.bind(this) ); }.bind(this)); - }.bind(this); - - if(this.options.pass || this.options.user) { - var adminDb = db.admin(); - adminDb.authenticate(this.options.user, this.options.pass, function(err) { - if(err) { - //TODO: handle error - console.log('adminDb.authenticate:', JSON.stringify(err)) - } - runMigrations() - }) - }else { - runMigrations(); - } }.bind(this)); }; diff --git a/src/utils/mongo-connection.js b/src/utils/mongo-connection.js index 3d4fe69..11df70d 100644 --- a/src/utils/mongo-connection.js +++ b/src/utils/mongo-connection.js @@ -21,7 +21,18 @@ function MongoConnection(config, options){ } MongoConnection.prototype.connect = function(cb){ - MongoClient.connect(this.connectionUri || this.getConnectionUri(), this.options || null, cb); + MongoClient.connect(this.connectionUri || this.getConnectionUri(), this.options || null, + function(err, db) { + if(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(){ From e6212c8e26892392ad58f6f14c82e3a4eed940a4 Mon Sep 17 00:00:00 2001 From: GitHub Date: Fri, 4 Aug 2017 09:49:14 +0200 Subject: [PATCH 11/11] fixed auth condition --- src/utils/mongo-connection.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/mongo-connection.js b/src/utils/mongo-connection.js index 11df70d..f4b2ef6 100644 --- a/src/utils/mongo-connection.js +++ b/src/utils/mongo-connection.js @@ -23,7 +23,7 @@ function MongoConnection(config, options){ MongoConnection.prototype.connect = function(cb){ MongoClient.connect(this.connectionUri || this.getConnectionUri(), this.options || null, function(err, db) { - if(this.options.pass || this.options.user) { + if(this.options && this.options.pass && this.options.user) { db.authenticate(this.options.user, this.options.pass, function(err) { if(err) { return cb(err);