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

Add :force option to create_enum #29

Merged
merged 4 commits into from Aug 21, 2020
Merged

Add :force option to create_enum #29

merged 4 commits into from Aug 21, 2020

Conversation

nirvdrum
Copy link
Contributor

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 have added tests

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 have made corresponding changes to the documentation

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

@nirvdrum nirvdrum changed the title Add force option Add :force option to create_enum Aug 20, 2020
@bibendi bibendi added the enhancement New feature or request label Aug 21, 2020
Copy link
Owner

@bibendi bibendi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you!

@bibendi bibendi merged commit 11e8556 into bibendi:master Aug 21, 2020
@bibendi
Copy link
Owner

bibendi commented Aug 21, 2020

Released 1.4.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

db:setup fails with ActiveRecord::StatementInvalid: PG::DuplicateObject
2 participants