bibendi / activerecord-postgres_enum

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

Add :force option to create_enum #29

Closed nirvdrum closed 4 years ago

nirvdrum commented 4 years ago

Context

This PR adds a new :force option to create_enum that's intended to follow what create_table does in ActiveRecord. Notably, this work fixes #22 and allows the rake db:setup task to succeed. However, existing schema.rb files need to be updated to take advantage of the new option. Running rake db:schema:dump should be enough to update existing create_enum calls. Newly run migrations will have :force set to :cascade by default.

Related tickets

22 db:setup fails with ActiveRecord::StatementInvalid: PG::DuplicateObject

What's inside

There is a new option to create_enum: :force.

When set to any truthy value, create_enum will be preceded by a drop_enum call. This call will do an IF EXISTS check, but will not cascade default unless the :force value is set to :cascade. As far as I can tell, this mirrors what ActiveRecord does for create_table.

Additionally, I've updated the schema dumper to set force: :cascade for any new entries to the schema.rb. I believe this matches how ActiveRecord handles newly created tables and, as such, matches what many developers expect. But, I'm not an ActiveRecord internals expert so I may have misinterpreted some of the existing code.

I should note that I couldn't figure out when CommandRecorder is invoked, so I didn't modify it. Please provide guidance there if changes are necessary.

Checklist:

I've added tests for code that already had tests. There were no tests for the schema dumper and I didn't know how to get started with that, so there are no new tests for this functionality either.

I don't think any documentation is warranted, but please let me know if you'd like something.

bibendi commented 4 years ago

Released 1.4.0