diff --git a/examples/.ledger b/examples/.ledger index c59c07f5..223447cb 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 fb034d18..f9acc5c4 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 00000000..28121011 --- /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 90a0adf5..aa61a0ba 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