gjaldon / ecto_enum

Ecto extension to support enums in models
MIT License
562 stars 131 forks source link

Reversible `create_type/0` makes it impossible to drop type #109

Open lleger opened 4 years ago

lleger commented 4 years ago

When create_type/0 was made to be reversible, it removed the ability to do a migration where you drop the type on the way up and recreate it on the way down.

Example:

defmodule Migration do
  use Ecto.Migration

  def up do
    SampleEnum.drop_type()
  end

  def down do
    SampleEnum.create_type()
  end
end

This throws an error:

$ mix ecto.migrate
# succeeds

$ mix ecto.rollback
** (Postgrex.Error) ERROR 42704 (undefined_object)

This is because when rolling back, create_type/0 tries to run the DROP SQL when in reality we want the CREATE SQL.

One fix, perhaps is to just make drop_type/0 reversible as well. Then you'd call drop_type/0 in both up/down and it should work.

Another fix would be to change the API so that create_type/0 and drop_type/0 do what they say on the tin, and introduce a different function, e.g. migrate/0, that does a reversible action.

TL;DR it's impossible to recreate the enum type in a drop migration.