Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Thoughts on new feature: force cascade for create_enum #15

Closed
tommyh opened this issue Dec 30, 2020 · 2 comments
Closed

Thoughts on new feature: force cascade for create_enum #15

tommyh opened this issue Dec 30, 2020 · 2 comments

Comments

@tommyh
Copy link

tommyh commented Dec 30, 2020

When I run rails db:setup in my development environment and most the of the tables already exist, active record's create_table will drop the table for me by default because of the force: :cascade option in schema.rb. But when my schema.rb has instances of create_enum, they fail with the following error:

-- create_enum("boolean_operator_enum_type", ["any", "all"])
rails aborted!
ActiveRecord::StatementInvalid: PG::DuplicateObject: ERROR:  type "boolean_operator_enum_type" already exists
: CREATE TYPE boolean_operator_enum_type AS ENUM ('any', 'all')

What are your thoughts on adding a force: :cascade option to create_enum, so that the enum can be dropped and db:setup can be run easier? I'd be more than happy to create the PR, but wanted to check your thoughts on it first: good idea? bad idea?

Note: As a current solution, I temporarily change my schema.rb to this:

module ActiveRecord
  module PGEnum
    module SchemaStatements
      def drop_enum_if_exists_cascade(name, values_for_revert = nil)
        execute("DROP TYPE IF EXISTS #{name} CASCADE").tap {
          reload_type_map
        }
      end
    end
  end
end

ActiveRecord::Schema.define(version: 2020_12_22_214218) do
   drop_enum_if_exists_cascade "boolean_operator_enum_type"
   create_enum "boolean_operator_enum_type", ["any", "all"]
   # ... rails schema here
end
@alassek
Copy link
Owner

alassek commented Jan 3, 2021

Hmm, this sounds like the same problem as reported in #11, do you agree?

@tommyh
Copy link
Author

tommyh commented Jan 3, 2021

Yes, it does! Sorry I missed that. Closing out.

@tommyh tommyh closed this as completed Jan 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants