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
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Options:
//port: '27017' //include a port if necessary
}
}
-i Prompt before run migrations

Commands:
down [revision] migrate down (stop at optional revision name/number)
Expand Down Expand Up @@ -213,5 +214,3 @@ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


91 changes: 62 additions & 29 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var migrate = require('./lib/migrate'),
path = require('path'),
join = path.join,
fs = require('fs'),
readline = require('readline'),
verror = require('verror');

/**
Expand All @@ -24,7 +25,8 @@ var previousWorkingDirectory = process.cwd();

var configFileName = 'default-config.json',
dbConfig = null,
dbProperty = 'mongoAppDb';
dbProperty = 'mongoAppDb',
interactive = false;

/**
* Usage information.
Expand All @@ -35,11 +37,12 @@ var usage = [
, ''
, ' Options:'
, ''
, ' -runmm, --runMongoMigrate Run the migration from the command line'
, ' -dbc, --dbConfig JSON string containing db settings (overrides -c, -cfg, & -dbn)'
, ' -c, --chdir <path> change the working directory'
, ' -cfg, --config <path> DB config file name'
, ' -dbn, --dbPropName <string> Property name for database connection in config file'
, ' -runmm, --runMongoMigrate Run the migration from the command line'
, ' -dbc, --dbConfig JSON string containing db settings (overrides -c, -cfg, & -dbn)'
, ' -c, --chdir <path> Change the working directory'
, ' -cfg, --config <path> DB config file name'
, ' -dbn, --dbPropName <string> Property name for database connection in config file'
, ' -i Prompt before run migrations'
, ''
, ' Commands:'
, ''
Expand All @@ -54,8 +57,7 @@ var usage = [
*/

var template = [
''
, 'var mongodb = require(\'mongodb\');'
'var mongodb = require(\'mongodb\');'
, ''
, 'exports.up = function(db, next){'
, ' next();'
Expand All @@ -64,7 +66,6 @@ var template = [
, 'exports.down = function(db, next){'
, ' next();'
, '};'
, ''
].join('\n');

/**
Expand Down Expand Up @@ -261,7 +262,7 @@ function runMongoMigrate(direction, migrationEnd, next) {
} else {
process.exit();
}
}
};
}

var db = require('./lib/db');
Expand All @@ -285,30 +286,55 @@ function runMongoMigrate(direction, migrationEnd, next) {
db: dbConnection,
migrationCollection: migrationCollection
});
migrations(direction, lastMigrationNum, migrateTo).forEach(function(path){
var mod = require(process.cwd() + '/' + path);
migrate({
num: parseInt(path.split('/')[1].match(/^(\d+)/)[0], 10),
title: path,
up: mod.up,
down: mod.down});
});
var migrationsToRun = migrations(direction, lastMigrationNum, migrateTo);

//Revert working directory to previous state
process.chdir(previousWorkingDirectory);
if (interactive && migrationsToRun.length > 0) {
var rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});

var set = migrate();
migrationsToRun.forEach(function(migration) {
log(direction, migration);
});
rl.question('These migrations will be run, continue? [y/N] ', function(answer) {
rl.close();
if (answer === 'y' || answer === 'Y') {
_run();
} else {
process.exit(0);
}
});
} else {
_run();
}

set.on('migration', function(migration, direction){
log(direction, migration.title);
});
function _run() {
migrationsToRun.forEach(function(path){
var mod = require(process.cwd() + '/' + path);
migrate({
num: parseInt(path.split('/')[1].match(/^(\d+)/)[0], 10),
title: path,
up: mod.up,
down: mod.down});
});

set.on('save', function(){
log('migration', 'complete');
return next();
});
//Revert working directory to previous state
process.chdir(previousWorkingDirectory);

var set = migrate();

set.on('migration', function(migration, direction){
log(direction, migration.title);
});

set.on('save', function(){
log('migration', 'complete');
return next();
});

set[direction](null, lastMigrationNum);
set[direction](null, lastMigrationNum);
}
});
});
}
Expand Down Expand Up @@ -336,6 +362,10 @@ function setDbConfig(conf) {
dbConfig = JSON.parse(conf);
}

function setInteractive(conf) {
interactive = true;
}

var runmmIdx = args.indexOf('-runmm'),
runMongoMigrateIdx = args.indexOf('--runMongoMigrate');
if (runmmIdx > -1 || runMongoMigrateIdx > -1) {
Expand Down Expand Up @@ -368,6 +398,9 @@ if (runmmIdx > -1 || runMongoMigrateIdx > -1) {
case '--dbPropName':
setConfigFileProperty(required());
break;
case '-i':
setInteractive();
break;
default:
if (options.command) {
options.args.push(arg);
Expand Down
2 changes: 1 addition & 1 deletion lib/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function getReplicaSetServers(opts, mongodb) {
var replServers = opts.replicaSet.map(function(replicaSet) {
var split = replicaSet.split(":");
var host = split[0] || 'localhost';
var port = split[1] || 27017;
var port = parseInt(split[1]) || 27017;
return new mongodb.Server(host, port);
});
return new mongodb.ReplSet(replServers);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongodb-migrate",
"version": "2.0.1",
"version": "2.1.1",
"description": "Migration framework for mongo in node",
"keywords": [
"mongo",
Expand Down