Browse files

Providing transactions during DDL (for Postgres)

  • Loading branch information...
1 parent 3ad78af commit d83ed045d6dbefdd8a356c3981f4b5e0b2661d57 @alsonkemp alsonkemp committed May 26, 2012
Showing with 21 additions and 7 deletions.
  1. +2 −0 lib/driver/base.js
  2. +12 −1 lib/driver/pg.js
  3. +7 −6 lib/migrator.js
View
2 lib/driver/base.js
@@ -153,6 +153,8 @@ module.exports = Base = Class.extend({
}
},
+ startMigration: function(cb){cb()},
+ endMigration: function(cb){cb()},
// sql, params, callback
// sql, callback
runSql: function() {
View
13 lib/driver/pg.js
@@ -2,6 +2,7 @@ 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({
@@ -10,6 +11,13 @@ var PgDriver = Base.extend({
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(' ');
@@ -18,6 +26,8 @@ var PgDriver = Base.extend({
switch(str) {
case type.DATE_TIME:
return 'TIMESTAMP';
+ case type.BLOG:
+ return 'BYTEA';
}
return this._super(str);
},
@@ -75,6 +85,7 @@ var PgDriver = Base.extend({
new_param.push(param[param.length-1]);
params[0] = new_param.join('');
}
+ log.info('pg.runSql', params);
this.connection.query.apply(this.connection, params);
},
@@ -94,4 +105,4 @@ var PgDriver = Base.extend({
exports.connect = function(config, callback) {
var db = config.db || new pg.Client(config);
callback(null, new PgDriver(db));
-};
+};
View
13 lib/migrator.js
@@ -116,14 +116,15 @@ Migrator.prototype = {
callback(null);
return;
}
-
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);
- });
+ });
});
},

0 comments on commit d83ed04

Please sign in to comment.