Hi. 

I'm facing a problem trying to add a new value for an existing enum in 
Postgres.

What I'm trying to do:

Sequel.extension :pg_enum

Sequel.migration do
  up do
    extension :pg_enum

    add_enum_value(:professional_status_enum, 'self_employed')
  end
end

Part of stacktrace:

Sequel::DatabaseError: PG::ActiveSqlTransaction: ERROR:  ALTER TYPE ... ADD 
cannot run inside a transaction block
/home/vagrant/.rvm/gems/ruby-2.3.0@bankfacil-core/gems/sequel-4.27.0/lib/sequel/adapters/postgres.rb:184:in
 
`async_exec'
/home/vagrant/.rvm/gems/ruby-2.3.0@bankfacil-core/gems/sequel-4.27.0/lib/sequel/adapters/postgres.rb:184:in
 
`block in execute_query'
/home/vagrant/.rvm/gems/ruby-2.3.0@bankfacil-core/gems/sequel-rails-0.9.11/lib/sequel_rails/sequel/database/active_support_notification.rb:16:in
 
`block in log_yield'
/home/vagrant/.rvm/gems/ruby-2.3.0@bankfacil-core/gems/activesupport-4.2.4/lib/active_support/notifications.rb:164:in
 
`block in instrument'
/home/vagrant/.rvm/gems/ruby-2.3.0@bankfacil-core/gems/activesupport-4.2.4/lib/active_support/notifications/instrumenter.rb:20:in
 
`instrument'
/home/vagrant/.rvm/gems/ruby-2.3.0@bankfacil-core/gems/activesupport-4.2.4/lib/active_support/notifications.rb:164:in
 
`instrument'

It seems that Postgres does not allow to alter type inside a transaction. 
By the other hand, I can do this:

Sequel.extension :pg_enum

Sequel.migration do
  up do
    extension :pg_enum

    # change type of column that use professional_status_enum. Otherwise, 
it is not possible to drop the enum
    run 'ALTER TABLE loan_home_refi_employments ALTER COLUMN 
professional_status TYPE varchar(255);'

    # drop enum
    drop_enum(:professional_status_enum)

    # recreate enum including the item(s) you want
    create_enum(:professional_status_enum, [
      'clt', 'freelancer', 'self_employed', 'civil_servant', 'retired', 
'businessman', 'other', nil
    ])

    # revert change in the first command
    run 'ALTER TABLE loan_home_refi_employments ALTER COLUMN 
professional_status TYPE professional_status_enum
      USING(trim(professional_status)::professional_status_enum);'
  end
end


Although the last snippet works, I'm not inclined to use it. That is 
another better way to do this? How can I use command "add_enum_value" from 
"pg_enum"?

Thanks in advance

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to