Skip to content

Commit

Permalink
Test: add unique preserves CHECK constraints
Browse files Browse the repository at this point in the history
Ensure that adding a `UNIQUE` constraint to a column preserves existing
`CHECK` constraints on the column.
  • Loading branch information
andrew-farries committed Jan 18, 2024
1 parent 2965ae1 commit dd65a2f
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions pkg/migrations/op_set_unique_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
},
})
}

0 comments on commit dd65a2f

Please sign in to comment.