How to do this migration if table is already on production?

I have a migration name user id as default.


create table(:user) do
      add(:name, :string, null: false)
      timestamps()
end

But I want to rename this table to something like this

table(:user, primary_key: false)

and also if I want to add the new field.

add :id, :uuid, primary_key: true

The data is already on production. So how can I do this efficiently?

Perhaps something like this

def change do
  alter table(:user) do
    add(:id, :bigserial)
  end

  flush()

  alter table(:user) do
    modify(:name, primary_key: false)
    modify(:id, primary_key: true)
  end
end

But then it’s giving me this error

(Postgrex.Error) ERROR 42701 (duplicate_column) column “id” of relation “user” already exists

is this name referring to table user?

Wouldn’t you need to drop the PK first then add the second one?

Ok, so you already have a migration which already puts an ID column by default.

Do you need to change it to :uuid?

If so you need to create a new column with that type, and then drop the old id column, and add the new uuid-column as primary key. I don’t know if uuid columns will auto-populate

Yeah I need to change the primary key as uuid. But if I drop it in my local and run it again and if it worked. Then what production there I can’t drop the table, right?

how do I do this?

I think have a look at the ecto migration documentation a bit and you would be able to answer your own question very quickly