Skip to content

Commit baa4513

Browse files
authored
Add missing index renaming when completing a migration (#577)
During testing the faithful duplication of indexed columns I discovered that if a column is included in multiple indices the only one of the indices are renamed. It happened because the index was deleted from the bookkeeper in the table even if the index was not renamed. ``` postgres=# \d items Table "public.items" Column | Type | Collation | Nullable | Default --------+------------------------+-----------+----------+----------------------------------------------- id | integer | | not null | nextval('_pgroll_new_items_id_seq'::regclass) city | character varying(255) | | | name | character varying(255) | | | Indexes: "_pgroll_new_items_pkey" PRIMARY KEY, btree (id) "_pgroll_dup_idx_items_unique_name" UNIQUE, btree (name) "items_unique_name_id" UNIQUE CONSTRAINT, btree (city, name) ``` Correct output after my fix: ``` postgres=# \d items Table "public.items" Column | Type | Collation | Nullable | Default --------+------------------------+-----------+----------+----------------------------------------------- id | integer | | not null | nextval('_pgroll_new_items_id_seq'::regclass) city | character varying(255) | | | name | character varying(255) | | | Indexes: "_pgroll_new_items_pkey" PRIMARY KEY, btree (id) "idx_items_unique_name" UNIQUE, btree (name) "items_unique_name_id" UNIQUE CONSTRAINT, btree (city, name) ```
1 parent bd0fa1c commit baa4513

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

pkg/migrations/rename.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ func RenameDuplicatedColumn(ctx context.Context, conn db.DB, table *schema.Table
138138
if err != nil {
139139
return fmt.Errorf("failed to rename index %q: %w", idx.Name, err)
140140
}
141+
142+
// Index no longer exists, remove it from the table
143+
delete(table.Indexes, idx.Name)
141144
}
142145

143146
if _, ok := table.UniqueConstraints[StripDuplicationPrefix(idx.Name)]; idx.Unique && ok {
@@ -154,8 +157,6 @@ func RenameDuplicatedColumn(ctx context.Context, conn db.DB, table *schema.Table
154157
}
155158
}
156159

157-
// Index no longer exists, remove it from the table
158-
delete(table.Indexes, idx.Name)
159160
}
160161

161162
return nil

0 commit comments

Comments
 (0)