Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge branch 'master' of github.com:nearinfinity/node-db-migrate

  • Loading branch information...
commit f209539fa9d8c937563637ed4b1204ffdef79722 2 parents b725d18 + 663edc2
@kunklejr kunklejr authored
Showing with 123 additions and 4 deletions.
  1. +2 −1  lib/driver/base.js
  2. +116 −0 lib/driver/pg.js
  3. +5 −3 lib/migrator.js
View
3  lib/driver/base.js
@@ -106,7 +106,6 @@ module.exports = Base = Class.extend({
if (options.ifExists) {
ifExistsSql = 'IF EXISTS';
}
-
var sql = util.format('DROP TABLE %s %s', ifExistsSql, tableName);
this.runSql(sql, callback);
},
@@ -154,6 +153,8 @@ module.exports = Base = Class.extend({
}
},
+ startMigration: function(cb){cb()},
+ endMigration: function(cb){cb()},
// sql, params, callback
// sql, callback
runSql: function() {
View
116 lib/driver/pg.js
@@ -0,0 +1,116 @@
+var util = require('util');
+var pg = require('pg');
+var Base = require('./base');
+var type = require('../data_type');
+var log = require('../log');
+
+
+var PgDriver = Base.extend({
+ init: function(connection) {
+ this._super();
+ this.connection = connection;
+ this.connection.connect();
+ },
+ startMigration: function(cb){
+ this.runSql('BEGIN;', function() { cb()});
+ },
+
+ endMigration: function(cb){
+ this.runSql('COMMIT;', function(){cb(null)});
+ },
+
+ createColumnDef: function(name, spec, options) {
+ return [name, spec.autoIncrement ? '' : this.mapDataType(spec.type), this.createColumnConstraint(spec, options)].join(' ');
+ },
+ mapDataType: function(str) {
+ switch(str) {
+ case type.DATE_TIME:
+ return 'TIMESTAMP';
+ case type.BLOG:
+ return 'BYTEA';
+ }
+ return this._super(str);
+ },
+
+ createColumnConstraint: function(spec, options) {
+ var constraint = [];
+ if (spec.primaryKey && options.emitPrimaryKey) {
+ if (spec.autoIncrement) {
+ constraint.push('SERIAL');
+ } else {
+ constraint.push('PRIMARY KEY');
+ }
+ }
+
+ if (spec.notNull) {
+ constraint.push('NOT NULL');
+ }
+
+ if (spec.unique) {
+ constraint.push('UNIQUE');
+ }
+
+ if (typeof spec.defaultValue != 'undefined') {
+ constraint.push('DEFAULT');
+ if (typeof spec.defaultValue == 'string'){
+ constraint.push("'" + spec.defaultValue + "'");
+ } else {
+ constraint.push(spec.defaultValue);
+ }
+ }
+
+ return constraint.join(' ');
+ },
+
+ renameTable: function(tableName, newTableName, callback) {
+ var sql = util.format('RENAME TABLE %s TO %s', tableName, newTableName);
+ this.runSql(sql, callback);
+ },
+
+ removeColumn: function(tableName, columnName, callback) {
+ var sql = util.format("ALTER TABLE %s DROP COLUMN %s", tableName, columnName);
+ this.runSql(sql, callback);
+ },
+
+ renameColumn: function(tableName, oldColumnName, newColumnName, callback) {
+ var sql = util.format("ALTER TABLE %s RENAME COLUMN %s TO %s", tableName, oldColumnName, newColumnName);
+ this.runSql(sql, callback)
+ },
+
+ //changeColumn: function(tableName, columnName, columnSpec, callback) {
+ //},
+
+ runSql: function() {
+ params = arguments;
+ if (params.length > 2){
+ // We have parameters, but db-migrate uses "?" for param substitutions.
+ // PG uses "$1", "$2", etc so fix up the "?" into "$1", etc
+ var param = params[0].split('?'),
+ new_param = [];
+ for (var i = 0; i < param.length-1; i++){
+ new_param.push(param[i], "$" + (i+1));
+ }
+ new_param.push(param[param.length-1]);
+ params[0] = new_param.join('');
+ }
+ log.info('pg.runSql', params);
+ this.connection.query.apply(this.connection, params);
+ },
+
+ all: function() {
+ params = arguments;
+ this.connection.query.apply(this.connection, [params[0], function(err, result){
+ params[1](err, result.rows);
+ }]);
+ },
+
+ close: function() {
+ this.connection.end();
+ }
+
+});
+
+exports.connect = function(config, callback) {
+ var db = config.db || new pg.Client(config);
+ callback(null, new PgDriver(db));
+};
View
8 lib/migrator.js
@@ -118,9 +118,11 @@ Migrator.prototype = {
}
async.forEachSeries(toRun, function(migration, next) {
- self.up(migration.up.bind(migration), function(err) {
- if (err) { callback(err); return; }
- self.writeMigrationRecord(migration, next);
+ self.driver.startMigration(function() {
+ self.up(migration.up.bind(migration), function(err) {
+ if (err) { callback(err); return; }
+ self.writeMigrationRecord(migration, function() { self.driver.endMigration(next) });
+ });
});
}, callback);
});
Please sign in to comment.
Something went wrong with that request. Please try again.