From 5c4cad9cb1ac9b30f7591f6542ba193957ce843b Mon Sep 17 00:00:00 2001 From: Andrew Farries Date: Wed, 17 Jan 2024 08:43:26 +0000 Subject: [PATCH] Rename `ConstraintMust` -> `CheckConstraintMust` Change the assertion to look for `CHECK` constraints and rename the assertion functions accordingly. --- pkg/migrations/op_add_column_test.go | 4 ++-- pkg/migrations/op_common_test.go | 11 ++++++----- pkg/migrations/op_create_table_test.go | 4 ++-- pkg/migrations/op_set_check_test.go | 6 +++--- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/pkg/migrations/op_add_column_test.go b/pkg/migrations/op_add_column_test.go index c4739854..dee111fe 100644 --- a/pkg/migrations/op_add_column_test.go +++ b/pkg/migrations/op_add_column_test.go @@ -547,12 +547,12 @@ func TestAddNotNullColumnWithNoDefault(t *testing.T) { afterRollback: func(t *testing.T, db *sql.DB) { // the check constraint has been dropped. constraintName := migrations.NotNullConstraintName("description") - ConstraintMustNotExist(t, db, "public", "products", constraintName) + CheckConstraintMustNotExist(t, db, "public", "products", constraintName) }, afterComplete: func(t *testing.T, db *sql.DB) { // the check constraint has been dropped. constraintName := migrations.NotNullConstraintName("description") - ConstraintMustNotExist(t, db, "public", "products", constraintName) + CheckConstraintMustNotExist(t, db, "public", "products", constraintName) // can't insert a null description into the new view; the column now has a NOT NULL constraint. MustNotInsert(t, db, "public", "02_add_column", "products", map[string]string{ diff --git a/pkg/migrations/op_common_test.go b/pkg/migrations/op_common_test.go index 147f1bdc..feb19321 100644 --- a/pkg/migrations/op_common_test.go +++ b/pkg/migrations/op_common_test.go @@ -195,16 +195,16 @@ func TriggerMustExist(t *testing.T, db *sql.DB, schema, table, trigger string) { } } -func ConstraintMustNotExist(t *testing.T, db *sql.DB, schema, table, constraint string) { +func CheckConstraintMustNotExist(t *testing.T, db *sql.DB, schema, table, constraint string) { t.Helper() - if constraintExists(t, db, schema, table, constraint) { + if checkConstraintExists(t, db, schema, table, constraint) { t.Fatalf("Expected constraint %q to not exist", constraint) } } -func ConstraintMustExist(t *testing.T, db *sql.DB, schema, table, constraint string) { +func CheckConstraintMustExist(t *testing.T, db *sql.DB, schema, table, constraint string) { t.Helper() - if !constraintExists(t, db, schema, table, constraint) { + if !checkConstraintExists(t, db, schema, table, constraint) { t.Fatalf("Expected constraint %q to exist", constraint) } } @@ -278,7 +278,7 @@ func indexExists(t *testing.T, db *sql.DB, schema, table, index string) bool { return exists } -func constraintExists(t *testing.T, db *sql.DB, schema, table, constraint string) bool { +func checkConstraintExists(t *testing.T, db *sql.DB, schema, table, constraint string) bool { t.Helper() var exists bool @@ -288,6 +288,7 @@ func constraintExists(t *testing.T, db *sql.DB, schema, table, constraint string FROM pg_catalog.pg_constraint WHERE conrelid = $1::regclass AND conname = $2 + AND contype = 'c' )`, fmt.Sprintf("%s.%s", schema, table), constraint).Scan(&exists) if err != nil { diff --git a/pkg/migrations/op_create_table_test.go b/pkg/migrations/op_create_table_test.go index 445b66d4..cc60cf34 100644 --- a/pkg/migrations/op_create_table_test.go +++ b/pkg/migrations/op_create_table_test.go @@ -200,7 +200,7 @@ func TestCreateTable(t *testing.T) { }, afterStart: func(t *testing.T, db *sql.DB) { // The check constraint exists on the new table. - ConstraintMustExist(t, db, "public", migrations.TemporaryName("users"), "check_name_length") + CheckConstraintMustExist(t, db, "public", migrations.TemporaryName("users"), "check_name_length") // Inserting a row into the table succeeds when the check constraint is satisfied. MustInsert(t, db, "public", "01_create_table", "users", map[string]string{ @@ -217,7 +217,7 @@ func TestCreateTable(t *testing.T) { }, afterComplete: func(t *testing.T, db *sql.DB) { // The check constraint exists on the new table. - ConstraintMustExist(t, db, "public", "users", "check_name_length") + CheckConstraintMustExist(t, db, "public", "users", "check_name_length") // Inserting a row into the table succeeds when the check constraint is satisfied. MustInsert(t, db, "public", "01_create_table", "users", map[string]string{ diff --git a/pkg/migrations/op_set_check_test.go b/pkg/migrations/op_set_check_test.go index 048a2307..ae5cec81 100644 --- a/pkg/migrations/op_set_check_test.go +++ b/pkg/migrations/op_set_check_test.go @@ -57,7 +57,7 @@ func TestSetCheckConstraint(t *testing.T) { ColumnMustExist(t, db, "public", "posts", migrations.TemporaryName("title")) // A check constraint has been added to the temporary column - ConstraintMustExist(t, db, "public", "posts", "check_title_length") + CheckConstraintMustExist(t, db, "public", "posts", "check_title_length") // Inserting a row that meets the check constraint into the old view works. MustInsert(t, db, "public", "01_add_table", "posts", map[string]string{ @@ -100,7 +100,7 @@ func TestSetCheckConstraint(t *testing.T) { ColumnMustNotExist(t, db, "public", "posts", migrations.TemporaryName("title")) // The check constraint no longer exists. - ConstraintMustNotExist(t, db, "public", "posts", "check_title_length") + CheckConstraintMustNotExist(t, db, "public", "posts", "check_title_length") // The up function no longer exists. FunctionMustNotExist(t, db, "public", migrations.TriggerFunctionName("posts", "title")) @@ -114,7 +114,7 @@ func TestSetCheckConstraint(t *testing.T) { }, afterComplete: func(t *testing.T, db *sql.DB) { // The check constraint exists on the new table. - ConstraintMustExist(t, db, "public", "posts", "check_title_length") + CheckConstraintMustExist(t, db, "public", "posts", "check_title_length") // Inserting a row that meets the check constraint into the new view works. MustInsert(t, db, "public", "02_add_check_constraint", "posts", map[string]string{