Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

added handling for unique constraints when changing a column on postg…

…res backend
  • Loading branch information...
commit 654eb748c06be8fd5fc4d92b5b5687a6d0d63f72 1 parent ffc43b4
@ryanmeador ryanmeador authored
Showing with 20 additions and 1 deletion.
  1. +20 −1 lib/driver/pg.js
View
21 lib/driver/pg.js
@@ -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) {
Please sign in to comment.
Something went wrong with that request. Please try again.