Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added dry-run option

  • Loading branch information...
commit 53ef390074f3541ce5d4ca37417feedb8d69defc 1 parent 4b35bf9
@joeferner joeferner authored
View
1  README.md
@@ -21,6 +21,7 @@ Options:
--env, -e The environment to run the migrations under. [default: "dev"]
--migrations-dir, -m The directory containing your migration files. [default: "./migrations"]
--count, -c Max number of migrations to run.
+ --dry-run Prints the SQL but doesn't run it. [boolean]
--verbose, -v Verbose mode. [default: false]
--config Location of the database.json file. [default: "./database.json"]
```
View
10 bin/db-migrate
@@ -11,7 +11,7 @@ var index = require('../index');
var log = require('../lib/log');
process.on('uncaughtException', function(err) {
- log.error(err);
+ log.error(err.stack);
process.exit(1);
});
@@ -34,6 +34,9 @@ var argv = optimist
.alias('c', 'count')
.string('c')
+ .describe('dry-run', "Prints the SQL but doesn't run it.")
+ .boolean('dry-run')
+
.describe('verbose', 'Verbose mode.')
.alias('v', 'verbose')
.boolean('v')
@@ -53,6 +56,10 @@ if (argv.help || argv._.length == 0) {
}
global.verbose = argv.verbose;
+global.dryRun = argv['dry-run'];
+if(global.dryRun) {
+ log.info('dry run');
+}
function createMigrationDir(dir, callback) {
fs.stat(dir, function(err, stat) {
@@ -102,6 +109,7 @@ function executeUp() {
migrator.migrationsDir = path.resolve(argv['migrations-dir']);
migrator.driver.createMigrationsTable(function(err) {
assert.ifError(err);
+ log.verbose('migration table created');
migrator.up(argv, onComplete.bind(this, migrator));
});
});
View
5 lib/driver/base.js
@@ -61,6 +61,7 @@ module.exports = Base = Class.extend({
},
createTable: function(tableName, options, callback) {
+ log.verbose('creating table:', tableName);
var columnSpecs = options;
var tableOptions = {};
@@ -206,11 +207,11 @@ module.exports = Base = Class.extend({
// sql, params, callback
// sql, callback
runSql: function() {
- throw new Error('not yet implemented');
+ throw new Error('not implemented');
},
all: function(sql, params, callback) {
- throw new Error('not yet implemented');
+ throw new Error('not implemented');
},
escape: function(str) {
View
8 lib/driver/index.js
@@ -1,11 +1,17 @@
+var log = require('../log');
+
exports.connect = function(config, callback) {
if (config.driver === undefined) {
throw new Error('config must include a driver key specifing which driver to use');
}
- var driver = require('./' + config.driver);
+ var req = './' + config.driver;
+ log.verbose('require:', req);
+ var driver = require(req);
+ log.verbose('connecting');
driver.connect(config, function(err, db) {
if (err) { callback(err); return; }
+ log.verbose('connected');
callback(null, db);
});
};
View
10 lib/driver/mysql.js
@@ -3,6 +3,7 @@ var moment = require('moment');
var mysql = require('mysql');
var Base = require('./base');
var type = require('../data_type');
+var log = require('../log');
var MysqlDriver = Base.extend({
init: function(connection) {
@@ -120,11 +121,16 @@ var MysqlDriver = Base.extend({
},
runSql: function() {
- this.connection.query.apply(this.connection, arguments);
+ var callback = arguments[arguments.length - 1];
+ log.sql.apply(null, arguments);
+ if(global.dryRun) {
+ return callback();
+ }
+ return this.connection.query.apply(this.connection, arguments);
},
all: function() {
- this.connection.query.apply(this.connection, arguments);
+ return this.connection.query.apply(this.connection, arguments);
},
close: function() {
View
8 lib/driver/pg.js
@@ -162,6 +162,8 @@ var PgDriver = Base.extend({
},
runSql: function() {
+ var callback = arguments[arguments.length - 1];
+
params = arguments;
if (params.length > 2){
// We have parameters, but db-migrate uses "?" for param substitutions.
@@ -174,7 +176,11 @@ var PgDriver = Base.extend({
new_param.push(param[param.length-1]);
params[0] = new_param.join('');
}
- log.verbose('pg.runSql', params);
+
+ log.sql.apply(null, params);
+ if(global.dryRun) {
+ return callback();
+ }
this.connection.query.apply(this.connection, params);
},
View
7 lib/driver/sqlite3.js
@@ -1,6 +1,7 @@
var util = require('util');
var sqlite3 = require('sqlite3').verbose();
var Base = require('./base');
+var log = require('../log');
var type = require('../data_type');
var defaultMode = sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE;
@@ -51,6 +52,12 @@ var Sqlite3Driver = Base.extend({
//},
runSql: function() {
+ var callback = arguments[arguments.length - 1];
+ log.sql.apply(null, arguments);
+ if(global.dryRun) {
+ return callback();
+ }
+
this.connection.run.apply(this.connection, arguments);
},
View
20 lib/log.js
@@ -1,6 +1,26 @@
exports.info = console.info.bind(console, '[INFO]');
exports.warn = console.warn.bind(console, '[WARN]');
exports.error = console.error.bind(console, '[ERROR]');
+exports.sql = function(sql) {
+ if (global.dryRun || global.verbose) {
+ var args = Array.prototype.slice.call(arguments).slice(1);
+ args = args.slice(0, args.length - 1);
+ if(global.verbose) {
+ if(args.length > 0) {
+ console.log('[SQL]', sql, args);
+ } else {
+ console.log('[SQL]', sql);
+ }
+ }
+ if (global.dryRun) {
+ if(args.length > 0) {
+ console.log(sql, args);
+ } else {
+ console.log(sql);
+ }
+ }
+ }
+};
exports.verbose = function() {
if (global.verbose) {
console.log.bind(console, '[INFO]').apply(console, arguments);
View
3  lib/migration.js
@@ -3,6 +3,7 @@ var path = require('path');
var inflection = require('./inflection');
var lpad = require('./util').lpad;
var config = require('./config');
+var log = require('./log');
function formatPath(dir, name) {
return path.join(dir, name + '.js');
@@ -99,6 +100,7 @@ Migration.parseName = function(path) {
};
Migration.loadFromFilesystem = function(dir, callback) {
+ log.verbose('loading migrations from dir', dir);
fs.readdir(dir, function(err, files) {
if (err) { callback(err); return; }
files = files.filter(function(file) {
@@ -112,6 +114,7 @@ Migration.loadFromFilesystem = function(dir, callback) {
};
Migration.loadFromDatabase = function(dir, driver, callback) {
+ log.verbose('loading migrations from database');
driver.all('SELECT * FROM migrations ORDER BY name DESC', function(err, dbResults) {
if (err) { callback(err); return; }
var migrations = dbResults.map(function(result) {
View
10 lib/migrator.js
@@ -108,10 +108,10 @@ Migrator.prototype = {
upToBy: function(partialName, count, callback) {
var self = this;
- var all = Migration.loadFromFilesystem(self.migrationsDir, function(err, allMigrations) {
+ Migration.loadFromFilesystem(self.migrationsDir, function(err, allMigrations) {
if (err) { callback(err); return; }
- var complete = Migration.loadFromDatabase(self.migrationsDir, self.driver, function(err, completedMigrations) {
+ Migration.loadFromDatabase(self.migrationsDir, self.driver, function(err, completedMigrations) {
if (err) { callback(err); return; }
var toRun = filterUp(allMigrations, completedMigrations, partialName, count);
@@ -122,6 +122,7 @@ Migrator.prototype = {
}
async.forEachSeries(toRun, function(migration, next) {
+ log.verbose('preparing to run up migration:', migration.name);
self.driver.startMigration(function() {
self.up(migration.up.bind(migration), function(err) {
if (err) { callback(err); return; }
@@ -143,8 +144,8 @@ Migrator.prototype = {
downToBy: function(partialName, count, callback) {
var self = this;
- var complete = Migration.loadFromDatabase(self.migrationsDir, self.driver, function(err, completedMigrations) {
- if (err) { callback(err); return; }
+ Migration.loadFromDatabase(self.migrationsDir, self.driver, function(err, completedMigrations) {
+ if (err) { return callback(err); }
var toRun = filterDown(completedMigrations, partialName, count);
@@ -155,6 +156,7 @@ Migrator.prototype = {
}
async.forEachSeries(toRun, function(migration, next) {
+ log.verbose('preparing to run down migration:', migration.name);
self.down(migration.down.bind(migration), function(err) {
if (err) { callback(err); return; }
self.deleteMigrationRecord(migration, next);
Please sign in to comment.
Something went wrong with that request. Please try again.