From 5993661de820210b1b48ebfd050f4d2ba08389f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?No=C3=A9mi=20V=C3=A1nyi?= Date: Thu, 9 Jan 2025 14:02:30 +0100 Subject: [PATCH] fix adding extra unique constraint to existing unique constraint --- examples/.ledger | 1 + examples/08_create_fruits_table.json | 4 +++ ...add_extra_unique_constraint_to_fruits.json | 24 +++++++++++++++++ pkg/migrations/rename.go | 27 +++++++++---------- 4 files changed, 41 insertions(+), 15 deletions(-) create mode 100644 examples/50_add_extra_unique_constraint_to_fruits.json diff --git a/examples/.ledger b/examples/.ledger index c59c07f57..223447cb5 100644 --- a/examples/.ledger +++ b/examples/.ledger @@ -47,3 +47,4 @@ 47_add_table_foreign_key_constraint.json 48_drop_tickets_check.json 49_unset_not_null_on_indexed_column.json +50_add_extra_unique_constraint_to_fruits.json diff --git a/examples/08_create_fruits_table.json b/examples/08_create_fruits_table.json index fb034d187..f9acc5c45 100644 --- a/examples/08_create_fruits_table.json +++ b/examples/08_create_fruits_table.json @@ -18,6 +18,10 @@ { "name": "price", "type": "decimal(10,2)" + }, + { + "name": "producer", + "type": "varchar(255)" } ] } diff --git a/examples/50_add_extra_unique_constraint_to_fruits.json b/examples/50_add_extra_unique_constraint_to_fruits.json new file mode 100644 index 000000000..281210116 --- /dev/null +++ b/examples/50_add_extra_unique_constraint_to_fruits.json @@ -0,0 +1,24 @@ +{ + "name": "44_add_extra_unique_constraint_to_fruits", + "operations": [ + { + "create_constraint": { + "type": "unique", + "table": "fruits", + "name": "unique_fruit_and_producer", + "columns": [ + "name", + "producer" + ], + "up": { + "name": "name", + "producer": "producer" + }, + "down": { + "name": "name", + "producer": "producer" + } + } + } + ] +} diff --git a/pkg/migrations/rename.go b/pkg/migrations/rename.go index 90a0adf56..aa61a0bab 100644 --- a/pkg/migrations/rename.go +++ b/pkg/migrations/rename.go @@ -123,26 +123,24 @@ func RenameDuplicatedColumn(ctx context.Context, conn db.DB, table *schema.Table // Rename any indexes on the duplicated column and use unique indexes to // create `UNIQUE` constraints. for _, idx := range table.Indexes { - if !IsDuplicatedName(idx.Name) { + if !IsDuplicatedName(idx.Name) || !slices.Contains(idx.Columns, TemporaryName(column.Name)) { continue } - if slices.Contains(idx.Columns, TemporaryName(column.Name)) { - // Rename the index to its original name - renameIndexSQL := fmt.Sprintf(cRenameIndexSQL, - pq.QuoteIdentifier(idx.Name), - pq.QuoteIdentifier(StripDuplicationPrefix(idx.Name)), - ) + // Rename the index to its original name + renameIndexSQL := fmt.Sprintf(cRenameIndexSQL, + pq.QuoteIdentifier(idx.Name), + pq.QuoteIdentifier(StripDuplicationPrefix(idx.Name)), + ) - _, err = conn.ExecContext(ctx, renameIndexSQL) - if err != nil { - return fmt.Errorf("failed to rename index %q: %w", idx.Name, err) - } - - // Index no longer exists, remove it from the table - delete(table.Indexes, idx.Name) + _, err = conn.ExecContext(ctx, renameIndexSQL) + if err != nil { + return fmt.Errorf("failed to rename index %q: %w", idx.Name, err) } + // Index no longer exists, remove it from the table + delete(table.Indexes, idx.Name) + if _, ok := table.UniqueConstraints[StripDuplicationPrefix(idx.Name)]; idx.Unique && ok { // Create a unique constraint using the unique index createUniqueConstraintSQL := fmt.Sprintf(cCreateUniqueConstraintSQL, @@ -156,7 +154,6 @@ func RenameDuplicatedColumn(ctx context.Context, conn db.DB, table *schema.Table return fmt.Errorf("failed to create unique constraint from index %q: %w", idx.Name, err) } } - } return nil