Skip to content

Commit

Permalink
Add missing index renaming when completing a migration (#577)
Browse files Browse the repository at this point in the history
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)

```
  • Loading branch information
kvch authored Jan 8, 2025
1 parent bd0fa1c commit baa4513
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/migrations/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ func RenameDuplicatedColumn(ctx context.Context, conn db.DB, table *schema.Table
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 {
Expand All @@ -154,8 +157,6 @@ func RenameDuplicatedColumn(ctx context.Context, conn db.DB, table *schema.Table
}
}

// Index no longer exists, remove it from the table
delete(table.Indexes, idx.Name)
}

return nil
Expand Down

0 comments on commit baa4513

Please sign in to comment.