bibendi / activerecord-postgres_enum

Integrate PostgreSQL's enum data type into ActiveRecord's schema and migrations.
MIT License
361 stars 24 forks source link

PG::ActiveSqlTransaction: ERROR: ALTER TYPE ... ADD cannot run inside a transaction block #36

Closed barash-asenov closed 3 years ago

barash-asenov commented 3 years ago

Describe the bug When I use the function add_enum_value in the migration like

add_enum_value :my_enum, 'extra_value', if_not_exists: true

I get the following error

StandardError: An error has occurred, this and all later migrations canceled:

PG::ActiveSqlTransaction: ERROR:  ALTER TYPE ... ADD cannot run inside a transaction block
/home/barash/Projects/Carl/api/db/migrate/20201203142007_update_status_enum_on_selling_profile_user_interest.rb:9:in `change'

Caused by:
ActiveRecord::StatementInvalid: PG::ActiveSqlTransaction: ERROR:  ALTER TYPE ... ADD cannot run inside a transaction block
/xxx/db/migrate/20201203142007_update_status_enum_on_model.rb:9:in `change'

Caused by:
PG::ActiveSqlTransaction: ERROR:  ALTER TYPE ... ADD cannot run inside a transaction block
/xxx/db/migrate/20201203142007_update_status_enum_on_model:9:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Context : psql (PostgreSQL) 12.5 (Ubuntu 12.5-0ubuntu0.20.04.1)

barash-asenov commented 3 years ago

I resolved the issue but I believe this is a general issue that is denoted here; https://github.com/rails/rails/issues/9483

Maybe we can add this part to documentation for people who are stuck like me? Like they can do;

class CreateUsers < ActiveRecord::Migration
  disable_ddl_transaction!

  def up
    add_enum_value :my_enum, 'extra_value', if_not_exists: true

    ActiveRecord::Base.transaction do
      # other migrations we want to keep safe.
    end
  end
end

So I believe this is a safe way to handle the situation.

bibendi commented 3 years ago

Maybe we can add this part to documentation for people who are stuck like me?

We have it at the bottom of the Usage section 🙂

barash-asenov commented 3 years ago

Yeah sorry about that... I realized and closed the PR