Skip to content

Commit

Permalink
Test: set not null preserves CHECK constraints
Browse files Browse the repository at this point in the history
Ensure that altering a column to make it `NOT NULL` retains any check
constraints that were defined on the column.
  • Loading branch information
andrew-farries committed Jan 19, 2024
1 parent ff8b692 commit 000d79e
Showing 1 changed file with 57 additions and 1 deletion.
58 changes: 57 additions & 1 deletion pkg/migrations/op_set_notnull_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ func TestSetNotNull(t *testing.T) {
},
},
{
name: "setting a nullable column to not null retains any default defined on the column",
name: "setting a column to not null retains any default defined on the column",
migrations: []migrations.Migration{
{
Name: "01_add_table",
Expand Down Expand Up @@ -364,6 +364,62 @@ func TestSetNotNull(t *testing.T) {
}, rows)
},
},
{
name: "setting a column to not null retains any check constraints defined on the column",
migrations: []migrations.Migration{
{
Name: "01_add_table",
Operations: migrations.Operations{
&migrations.OpCreateTable{
Name: "users",
Columns: []migrations.Column{
{
Name: "id",
Type: "integer",
Pk: true,
},
{
Name: "name",
Type: "text",
Nullable: true,
Check: &migrations.CheckConstraint{
Name: "name_length",
Constraint: "length(name) > 3",
},
},
},
},
},
},
{
Name: "02_set_not_null",
Operations: migrations.Operations{
&migrations.OpAlterColumn{
Table: "users",
Column: "name",
Nullable: ptr(false),
Up: "(SELECT CASE WHEN name IS NULL THEN 'anonymous' ELSE name END)",
},
},
},
},
afterStart: func(t *testing.T, db *sql.DB) {
// Inserting a row that violates the check constraint should fail.
MustNotInsert(t, db, "public", "02_set_not_null", "users", map[string]string{
"id": "1",
"name": "a",
}, 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_not_null", "users", map[string]string{
"id": "2",
"name": "b",
}, testutils.CheckViolationErrorCode)
},
},
})
}

Expand Down

0 comments on commit 000d79e

Please sign in to comment.