gjaldon / ecto_enum

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

Rollback doesn't seem to work with `.create_type` #82

Closed youroff closed 5 years ago

youroff commented 5 years ago

Here's what my migration looks like:

  def change do
    RoleEnum.create_type

    create table("users") do
      add :first_name,    :string
      add :last_name,     :string
      add :role,          RoleEnum.type()
      add :email,         :string
      add :salt,          :string
      add :password_hash, :string

      timestamps()
    end
  end

Here's the output on rollback:

10:32:29.331 [info]  drop table users
** (Ecto.MigrationError) cannot reverse migration command: execute "CREATE TYPE public.role AS ENUM ('admin', 'customer', 'vendor')". You will need to explicitly define up/0 and down/0 in your migration
    (ecto_sql) lib/ecto/migration/runner.ex:215: Ecto.Migration.Runner.execute_in_direction/5
    (ecto_sql) lib/ecto/migration/runner.ex:119: anonymous fn/6 in Ecto.Migration.Runner.flush/0
    (elixir) lib/enum.ex:1948: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto_sql) lib/ecto/migration/runner.ex:118: Ecto.Migration.Runner.flush/0
    (stdlib) timer.erl:166: :timer.tc/1
    (ecto_sql) lib/ecto/migration/runner.ex:27: Ecto.Migration.Runner.run/7
    (ecto_sql) lib/ecto/migrator.ex:276: Ecto.Migrator.attempt/7
    (ecto_sql) lib/ecto/migrator.ex:211: anonymous fn/4 in Ecto.Migrator.do_down/4

Should creation of type and first use be separated in different migrations then?

youroff commented 5 years ago

Ah, it seems like it wasn't supposed to work, this example (with change) is a bit misleading: https://github.com/gjaldon/ecto_enum#using-postgress-enum-type

youroff commented 5 years ago

Temporarily fixed this by importing Ecto.Migration in the same file where enums are defined, so that check in create_type generation succeeded and execute/2 were used, but in might need a proper fix, which I'm not sure what should be. Leaving this open until reaction from the maintainers.

gjaldon commented 5 years ago

Thanks for reporting the bug @youroff! 💜