Skip to content

Commit

Permalink
Merge pull request #46 from lede/master
Browse files Browse the repository at this point in the history
handle unique constraints in changeColumn() for postgres
  • Loading branch information
kunklejr committed Sep 14, 2012
2 parents ffc43b4 + 654eb74 commit 394e342
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion lib/driver/pg.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 394e342

Please sign in to comment.