Fix #308 onDelete / onUpdate in PostgreSQL, with tests

This commit is contained in:
Tim Griesser 2014-06-12 13:57:01 -04:00
parent 7722011721
commit 1e936ffd3c
5 changed files with 24 additions and 21 deletions

View File

@ -117,19 +117,6 @@ TableCompiler_MySQL.prototype.dropUnique = function(column, indexName) {
this.pushQuery('alter table ' + this.tableName() + ' drop index ' + indexName);
};
// Compile a foreign key command.
TableCompiler_MySQL.prototype.foreign = function(foreignData) {
var sql = Schema.TableCompiler.prototype.foreign.apply(this, arguments);
if (sql) {
// Once we have the basic foreign key creation statement constructed we can
// build out the syntax for what should happen on an update or delete of
// the affected columns, which will get something like 'cascade', etc.
if (foreignData.onDelete) sql += ' on delete ' + foreignData.onDelete;
if (foreignData.onUpdate) sql += ' on update ' + foreignData.onUpdate;
this.pushQuery(sql);
}
};
client.TableBuilder = TableBuilder_MySQL;
client.TableCompiler = TableCompiler_MySQL;

View File

@ -47,12 +47,6 @@ TableCompiler_PG.prototype.createQuery = function(columns) {
if (hasComment) this.comment(this.single.comment);
};
// Compile a foreign key command.
TableCompiler_PG.prototype.foreign = function(foreignData) {
var sql = Schema.TableCompiler.prototype.foreign.apply(this, arguments);
if (sql) this.pushQuery(sql);
};
// Compiles the comment on the table.
TableCompiler_PG.prototype.comment = function(comment) {
this.pushQuery('comment on table ' + this.tableName() + ' is ' + "'" + (this.single.comment || '') + "'");

View File

@ -50,8 +50,10 @@ TableCompiler.prototype.foreign = function(foreignData) {
var column = this.formatter.columnize(foreignData.column);
var references = this.formatter.columnize(foreignData.references);
var inTable = this.formatter.wrap(foreignData.inTable);
return 'alter table ' + this.tableName() + ' add constraint ' + keyName + ' ' +
'foreign key (' + column + ') references ' + inTable + ' (' + references + ')';
var onUpdate = foreignData.onUpdate ? ' on update ' + foreignData.onUpdate : '';
var onDelete = foreignData.onDelete ? ' on delete ' + foreignData.onDelete : '';
this.pushQuery('alter table ' + this.tableName() + ' add constraint ' + keyName + ' ' +
'foreign key (' + column + ') references ' + inTable + ' (' + references + ')' + onUpdate + onDelete);
}
};

View File

@ -191,6 +191,16 @@ module.exports = function(client) {
expect(tableSql[0].sql).to.equal('alter table `users` add constraint users_foo_id_foreign foreign key (`foo_id`) references `orders` (`id`)');
});
it("adds foreign key with onUpdate and onDelete", function() {
tableSql = new SchemaBuilder().createTable('person', function(table) {
table.integer('user_id').notNull().references('users.id').onDelete('SET NULL');
table.integer('account_id').notNull().references('id').inTable('accounts').onUpdate('cascade');
}).toSQL();
equal(3, tableSql.length);
expect(tableSql[1].sql).to.equal('alter table `person` add constraint person_user_id_foreign foreign key (`user_id`) references `users` (`id`) on delete SET NULL');
expect(tableSql[2].sql).to.equal('alter table `person` add constraint person_account_id_foreign foreign key (`account_id`) references `accounts` (`id`) on update cascade');
});
it('test adding incrementing id', function() {
tableSql = new SchemaBuilder().table('users', function() {
this.increments('id');

View File

@ -159,6 +159,16 @@ module.exports = function(client) {
expect(tableSql[1].sql).to.equal('alter table "accounts" add constraint accounts_account_id_foreign foreign key ("account_id") references "users" ("id")');
});
it("adds foreign key with onUpdate and onDelete", function() {
tableSql = new SchemaBuilder().createTable('person', function(table) {
table.integer('user_id').notNull().references('users.id').onDelete('SET NULL');
table.integer('account_id').notNull().references('id').inTable('accounts').onUpdate('cascade');
}).toSQL();
equal(3, tableSql.length);
expect(tableSql[1].sql).to.equal('alter table "person" add constraint person_user_id_foreign foreign key ("user_id") references "users" ("id") on delete SET NULL');
expect(tableSql[2].sql).to.equal('alter table "person" add constraint person_account_id_foreign foreign key ("account_id") references "accounts" ("id") on update cascade');
});
it("adding unique key", function() {
tableSql = new SchemaBuilder().table('users', function(table) {
table.unique('foo', 'bar');