Modifying existing unique index on Ecto

Heelo:

I have this table migration:

create table(:products) do
      add :name, :string
      add :price, :integer
      add :sku, :string
end

I also created a unique index on:
create unique_index(:products, [:name, :price], name: :name_price_unique_index)

What I want to do is to add :sku to the unique index columns.

In Postgres it would be something like this:

CREATE UNIQUE INDEX CONCURRENTLY newidx ON products (name, price, sku);
DROP INDEX name_price_unique_index;
ALTER INDEX newidx RENAME TO name_price_sku_unique_index;

But how can I do this with an Ecto migration?

You could define an up and down function and just use execute/1 with raw SQL (then you also have to define the reverse operations in the down/0 function.
Or you skip the renaming and just drop the old index and create the new one in the migration.

3 Likes