Permalink
Browse files

Merge pull request #46 from lede/master

handle unique constraints in changeColumn() for postgres
  • Loading branch information...
2 parents ffc43b4 + 654eb74 commit 394e3420be67b188d4457b0e0701a652f3373d7e @kunklejr kunklejr committed Sep 14, 2012
Showing with 20 additions and 1 deletion.
  1. +20 −1 lib/driver/pg.js
View
@@ -116,7 +116,26 @@ var PgDriver = Base.extend({
function setNotNull() {
var setOrDrop = columnSpec.notNull === true ? 'SET' : 'DROP';
var sql = util.format("ALTER TABLE %s ALTER COLUMN %s %s NOT NULL", tableName, columnName, setOrDrop);
- this.runSql(sql, setDefaultValue.bind(this));
+ this.runSql(sql, setUnique.bind(this));
+ }
+
+ function setUnique(err) {
+ if (err) {
+ callback(err);
+ }
+
+ var sql;
+ var constraintName = tableName + '_' + columnName + '_unique';
+
+ if (columnSpec.unique === true) {
+ sql = util.format("ALTER TABLE %s ADD CONSTRAINT %s UNIQUE (%s)", tableName, constraintName, columnName);
+ this.runSql(sql, setDefaultValue.bind(this));
+ } else if (columnSpec.unique === false) {
+ sql = util.format("ALTER TABLE %s DROP CONSTRAINT %s", tableName, constraintName);
+ this.runSql(sql, setDefaultValue.bind(this));
+ }
+
+ setDefaultValue.call(this);
}
function setDefaultValue(err) {

0 comments on commit 394e342

Please sign in to comment.