diff --git a/pkg/migrations/op_set_unique_test.go b/pkg/migrations/op_set_unique_test.go index aca893db..6e732401 100644 --- a/pkg/migrations/op_set_unique_test.go +++ b/pkg/migrations/op_set_unique_test.go @@ -339,5 +339,67 @@ func TestSetColumnUnique(t *testing.T) { ValidatedForeignKeyMustExist(t, db, "public", "employees", "fk_employee_department") }, }, + { + name: "check constraints are preserved when adding a unique constraint", + migrations: []migrations.Migration{ + { + Name: "01_add_table", + Operations: migrations.Operations{ + &migrations.OpCreateTable{ + Name: "reviews", + Columns: []migrations.Column{ + { + Name: "id", + Type: "serial", + Pk: true, + }, + { + Name: "username", + Type: "text", + }, + { + Name: "review", + Type: "text", + Check: &migrations.CheckConstraint{ + Name: "reviews_review_check", + Constraint: "length(review) > 3", + }, + }, + }, + }, + }, + }, + { + Name: "02_set_unique", + Operations: migrations.Operations{ + &migrations.OpAlterColumn{ + Table: "reviews", + Column: "username", + Unique: &migrations.UniqueConstraint{ + Name: "reviews_username_unique", + }, + Up: "username", + Down: "username", + }, + }, + }, + }, + afterStart: func(t *testing.T, db *sql.DB) { + // Inserting a row that violates the check constraint should fail. + MustNotInsert(t, db, "public", "02_set_unique", "reviews", map[string]string{ + "username": "alice", + "review": "x", + }, testutils.CheckViolationErrorCode) + }, + afterRollback: func(t *testing.T, db *sql.DB) { + }, + afterComplete: func(t *testing.T, db *sql.DB) { + // Inserting a row that violates the check constraint should fail. + MustNotInsert(t, db, "public", "02_set_unique", "reviews", map[string]string{ + "username": "bob", + "review": "y", + }, testutils.CheckViolationErrorCode) + }, + }, }) }