Skip to content

Commit

Permalink
fix adding extra unique constraint to existing unique constraint
Browse files Browse the repository at this point in the history
  • Loading branch information
kvch committed Jan 9, 2025
1 parent daba767 commit 5993661
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
1 change: 1 addition & 0 deletions examples/.ledger
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 4 additions & 0 deletions examples/08_create_fruits_table.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
{
"name": "price",
"type": "decimal(10,2)"
},
{
"name": "producer",
"type": "varchar(255)"
}
]
}
Expand Down
24 changes: 24 additions & 0 deletions examples/50_add_extra_unique_constraint_to_fruits.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
]
}
27 changes: 12 additions & 15 deletions pkg/migrations/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 5993661

Please sign in to comment.