Skip to content

Commit 1f26ef1

Browse files
Support table and column rename operations preceding create_constraint UNIQUE operations (#676)
Follow up to #674 which ensured that `create_constraint` `CHECK` operations could be preceded by rename table and rename column operations. This PR ensures that `create_constraint` `UNIQUE` operations can be preceded by rename table and rename column operations as in the following example: ```json { "name": "19_multiple_ops", "operations": [ { "rename_table": { "from": "items", "to": "products" } }, { "rename_column": { "table": "products", "from": "name", "to": "item_name" } }, { "create_constraint": { "table": "products", "type": "unique", "name": "unique_item_name", "columns": ["item_name"], "up": { "item_name": "item_name || '-from-up'" }, "down": { "item_name": "item_name || '-from-down'" } } } ] } ```
1 parent f3432da commit 1f26ef1

File tree

2 files changed

+115
-3
lines changed

2 files changed

+115
-3
lines changed

pkg/migrations/op_create_constraint.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func (o *OpCreateConstraint) Start(ctx context.Context, conn db.DB, latestSchema
7575

7676
switch o.Type {
7777
case OpCreateConstraintTypeUnique:
78-
return table, createUniqueIndexConcurrently(ctx, conn, s.Name, o.Name, o.Table, temporaryNames(o.Columns))
78+
return table, createUniqueIndexConcurrently(ctx, conn, s.Name, o.Name, table.Name, temporaryNames(o.Columns))
7979
case OpCreateConstraintTypeCheck:
8080
return table, o.addCheckConstraint(ctx, conn, table.Name)
8181
case OpCreateConstraintTypeForeignKey:

pkg/migrations/op_create_constraint_test.go

Lines changed: 114 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -746,7 +746,7 @@ func TestCreateConstraintInMultiOperationMigrations(t *testing.T) {
746746

747747
ExecuteTests(t, TestCases{
748748
{
749-
name: "rename table, create constraint",
749+
name: "rename table, create check constraint",
750750
migrations: []migrations.Migration{
751751
{
752752
Name: "01_create_table",
@@ -854,7 +854,7 @@ func TestCreateConstraintInMultiOperationMigrations(t *testing.T) {
854854
},
855855
},
856856
{
857-
name: "rename table, rename column, create constraint",
857+
name: "rename table, rename column, create check constraint",
858858
migrations: []migrations.Migration{
859859
{
860860
Name: "01_create_table",
@@ -962,6 +962,118 @@ func TestCreateConstraintInMultiOperationMigrations(t *testing.T) {
962962
{"id": 3, "item_name": "banana"},
963963
}, rows)
964964

965+
// The table has been cleaned up
966+
TableMustBeCleanedUp(t, db, schema, "products", "name")
967+
},
968+
},
969+
{
970+
name: "rename table, rename column, create unique constraint",
971+
migrations: []migrations.Migration{
972+
{
973+
Name: "01_create_table",
974+
Operations: migrations.Operations{
975+
&migrations.OpCreateTable{
976+
Name: "items",
977+
Columns: []migrations.Column{
978+
{
979+
Name: "id",
980+
Type: "int",
981+
Pk: true,
982+
},
983+
{
984+
Name: "name",
985+
Type: "varchar(255)",
986+
Nullable: true,
987+
},
988+
},
989+
},
990+
},
991+
},
992+
{
993+
Name: "02_multi_operation",
994+
Operations: migrations.Operations{
995+
&migrations.OpRenameTable{
996+
From: "items",
997+
To: "products",
998+
},
999+
&migrations.OpRenameColumn{
1000+
Table: "products",
1001+
From: "name",
1002+
To: "item_name",
1003+
},
1004+
&migrations.OpCreateConstraint{
1005+
Table: "products",
1006+
Type: migrations.OpCreateConstraintTypeUnique,
1007+
Name: "unique_item_name",
1008+
Columns: []string{"item_name"},
1009+
Up: map[string]string{
1010+
"item_name": "item_name",
1011+
},
1012+
Down: map[string]string{
1013+
"item_name": "item_name",
1014+
},
1015+
},
1016+
},
1017+
},
1018+
},
1019+
afterStart: func(t *testing.T, db *sql.DB, schema string) {
1020+
// Can insert a row into the new schema
1021+
MustInsert(t, db, schema, "02_multi_operation", "products", map[string]string{
1022+
"id": "1",
1023+
"item_name": "apple",
1024+
})
1025+
1026+
// Can insert a row into the new schema that meets the constraint
1027+
MustInsert(t, db, schema, "02_multi_operation", "products", map[string]string{
1028+
"id": "2",
1029+
"item_name": "banana",
1030+
})
1031+
1032+
// Can't insert a row into the new schema that violates the constraint
1033+
MustNotInsert(t, db, schema, "02_multi_operation", "products", map[string]string{
1034+
"id": "3",
1035+
"item_name": "apple",
1036+
}, testutils.UniqueViolationErrorCode)
1037+
1038+
// The new view has the expected rows
1039+
rows := MustSelect(t, db, schema, "02_multi_operation", "products")
1040+
assert.Equal(t, []map[string]any{
1041+
{"id": 1, "item_name": "apple"},
1042+
{"id": 2, "item_name": "banana"},
1043+
}, rows)
1044+
1045+
// The old view has the expected rows
1046+
rows = MustSelect(t, db, schema, "01_create_table", "items")
1047+
assert.Equal(t, []map[string]any{
1048+
{"id": 1, "name": "apple"},
1049+
{"id": 2, "name": "banana"},
1050+
}, rows)
1051+
},
1052+
afterRollback: func(t *testing.T, db *sql.DB, schema string) {
1053+
// The table has been cleaned up
1054+
TableMustBeCleanedUp(t, db, schema, "items", "name")
1055+
},
1056+
afterComplete: func(t *testing.T, db *sql.DB, schema string) {
1057+
// Can insert a row into the new schema that meets the constraint
1058+
MustInsert(t, db, schema, "02_multi_operation", "products", map[string]string{
1059+
"id": "3",
1060+
"item_name": "carrot",
1061+
})
1062+
1063+
// Can't insert a row into the new schema that violates the constraint
1064+
MustNotInsert(t, db, schema, "02_multi_operation", "products", map[string]string{
1065+
"id": "4",
1066+
"item_name": "carrot",
1067+
}, testutils.UniqueViolationErrorCode)
1068+
1069+
// The new view has the expected rows
1070+
rows := MustSelect(t, db, schema, "02_multi_operation", "products")
1071+
assert.Equal(t, []map[string]any{
1072+
{"id": 1, "item_name": "apple"},
1073+
{"id": 2, "item_name": "banana"},
1074+
{"id": 3, "item_name": "carrot"},
1075+
}, rows)
1076+
9651077
// The table has been cleaned up
9661078
TableMustBeCleanedUp(t, db, schema, "products", "name")
9671079
},

0 commit comments

Comments
 (0)