gjaldon / ecto_enum

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

`add_types()` gives a syntax error in PostgreSQL 13 #116

Open mrcasals opened 3 years ago

mrcasals commented 3 years ago

Hi!

Semi-related to #115: I used the cide directly from GitHub, but running this migration gives a syntax error under PostgreSQL 13:

defmodule MyApp.Repo.Migrations.AddMoreReportTypes do
  use Ecto.Migration

  def change do
    ReportTypeEnum.add_values()
  end
end

This is the output:

10:40:21.222 [info]  == Running 20210615081647 MyApp.Repo.Migrations.AddMoreReportTypes.change/0 forward

10:40:21.227 [info]  execute "ALTER TYPE public.report_type ADD VALUE IF NOT EXISTS 'spam'\nADD VALUE IF NOT EXISTS 'violence'\nADD VALUE IF NOT EXISTS 'nudity'\nADD VALUE IF NOT EXISTS 'extremist'\nADD VALUE IF NOT EXISTS 'other'\nADD VALUE IF NOT EXISTS 'fake'\nADD VALUE IF NOT EXISTS 'bullying'\nADD VALUE IF NOT EXISTS 'abusive'"
** (Postgrex.Error) ERROR 42601 (syntax_error) syntax error at or near "ADD"

    query: ALTER TYPE public.report_type ADD VALUE IF NOT EXISTS 'spam'
ADD VALUE IF NOT EXISTS 'violence'
ADD VALUE IF NOT EXISTS 'nudity'
ADD VALUE IF NOT EXISTS 'extremist'
ADD VALUE IF NOT EXISTS 'other'
ADD VALUE IF NOT EXISTS 'fake'
ADD VALUE IF NOT EXISTS 'bullying'
ADD VALUE IF NOT EXISTS 'abusive'
    (ecto_sql 3.6.2) lib/ecto/adapters/sql.ex:760: Ecto.Adapters.SQL.raise_sql_call_error/1
    (elixir 1.11.4) lib/enum.ex:1411: Enum."-map/2-lists^map/1-0-"/2
    (ecto_sql 3.6.2) lib/ecto/adapters/sql.ex:848: Ecto.Adapters.SQL.execute_ddl/4
    (ecto_sql 3.6.2) lib/ecto/migration/runner.ex:343: Ecto.Migration.Runner.log_and_execute_ddl/3
    (ecto_sql 3.6.2) lib/ecto/migration/runner.ex:117: anonymous fn/6 in Ecto.Migration.Runner.flush/0
    (elixir 1.11.4) lib/enum.ex:2193: Enum."-reduce/3-lists^foldl/2-0-"/3
    (ecto_sql 3.6.2) lib/ecto/migration/runner.ex:116: Ecto.Migration.Runner.flush/0
    (stdlib 3.12.1) timer.erl:166: :timer.tc/1
    (ecto_sql 3.6.2) lib/ecto/migration/runner.ex:25: Ecto.Migration.Runner.run/8
    (ecto_sql 3.6.2) lib/ecto/migrator.ex:324: Ecto.Migrator.attempt/8
    (ecto_sql 3.6.2) lib/ecto/migrator.ex:251: anonymous fn/5 in Ecto.Migrator.do_up/5
    (ecto_sql 3.6.2) lib/ecto/migrator.ex:295: anonymous fn/6 in Ecto.Migrator.async_migrate_maybe_in_transaction/7
    (ecto_sql 3.6.2) lib/ecto/migrator.ex:310: Ecto.Migrator.run_maybe_in_transaction/4
    (elixir 1.11.4) lib/task/supervised.ex:90: Task.Supervised.invoke_mfa/2
    (elixir 1.11.4) lib/task/supervised.ex:35: Task.Supervised.reply/5
    (stdlib 3.12.1) proc_lib.erl:249: :proc_lib.init_p_do_apply/3

Instead, I had to run the migration this way:

defmodule MyApp.Repo.Migrations.AddMoreReportTypes do
  use Ecto.Migration
  @disable_ddl_transaction true

  def up do
    ReportTypeEnum.__enums__() |> Enum.each(fn enum ->
      execute """
      ALTER TYPE #{ReportTypeEnum.type()} ADD VALUE IF NOT EXISTS '#{enum}';
      """
    end)
  end

  def down do
    nil
  end
end