alassek / activerecord-pg_enum

Integrate PostgreSQL's enumerated types with the Rails enum feature
MIT License
168 stars 10 forks source link

Cannot add new enum value #20

Closed DavidBuyCo closed 3 years ago

DavidBuyCo commented 3 years ago

Hi, thanks for your work!

I try to add a new enum value on an existing enum like the documentation

class AddNewValue < ActiveRecord::Migration[5.1]
  def change
    add_enum_value :my_test_enum, 'FOO', before: 'BAR'
  end
end

But when i run migrations, i got this error

Caused by:
ActiveRecord::StatementInvalid: PG::ActiveSqlTransaction: ERROR:  ALTER TYPE ... ADD cannot run inside a transaction block
: ALTER TYPE my_test_enum ADD VALUE 'FOO' BEFORE 'BAR'

I use this gem on a rails 5.1 application

alassek commented 3 years ago

Hi! The error message is telling you whats wrong. Here's the important bit: cannot run inside a transaction block

It may not be obvious if you're not familiar with Rails migrations. Every migration runs inside a DB transaction by default. Sometimes that's invalid, like when you're building a concurrent index or in this case altering an enum.

Here's the relevant documentation from PostgreSQL:

ALTER TYPE ... ADD VALUE (the form that adds a new value to an enum type) cannot be executed inside a transaction block.

Here's what you should do:

class AddNewValue < ActiveRecord::Migration[5.1]
  disable_ddl_transaction!

  def change
    add_enum_value :my_test_enum, 'FOO', before: 'BAR'
  end
end

I'll update the documentation to make this more clear.

DavidBuyCo commented 3 years ago

Nice, it work! thank for your answer!