Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adding extra unique constraint to columns that are already unique #579

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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