-
-
Notifications
You must be signed in to change notification settings - Fork 14
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
Enforcing the not_valid: true
option when creating constraints, including foreign keys
#36
base: master
Are you sure you want to change the base?
Conversation
not_valid: true
option when creating constraints, including foreign keys
@cyberdelia Is there any chance for this PR to get some attention? |
@bbatsov can I ask you to look at this PR and see if you can maybe have it merged? |
This seems odd to require your constraints to not be valid by default across all your migrations. Feels like we should maybe promote the reverse and explicitly allow |
I'll defer to @cyberdelia here. |
@cyberdelia Could you clarify what you mean by "promoting the reverse"? Specifically, I'm interested in understanding the benefits you see in preferring immediate constraint validation (i.e., not using By setting Conversely, For example, this approach is also recommended by strong_migrations gem, emphasizing its importance in preventing migration-related breakage. However, I am open to modifying the cop to only check for the presence of the |
Speaking from the same team as @numbata is on - we're happy making revisions to this PR to upstream this functionality, or have this live as a custom rubocop rule in our project. We were bitten by this very configuration option, and thought others could benefit from that learning too. ❤️ |
Promoting the use of So here is what I suggest, we can add this cop but not enable it by default. So anyone can pick and choose what fits their team process and database. |
@cyberdelia The step in have a clear and transparent default settings #41 If the PR with default works for you, then after merging, I can explicitly set |
…tion Scanning a large table to verify a new foreign key or check constraint can take a long time, and other updates to the table are locked out until the `ALTER TABLE ADD CONSTRAINT` command is committed. Using `NOT VALID` constraint option is reduce the imact of adding a new constraint on concurrent updates. With `NOT VALID`, the `ADD CONSTRAINT` command does not scan the table and can be committed immediately.
@numbata I'd love to get this merged, if you still have some time to work on this. |
@cyberdelia Thanks for the ping! We have reimplemented the cop internally, so I would be happy to check what could be updated here and ping you back. |
This pull request introduces a new rule that requires the use of the
not_valid: true
option when creating general and foreign key constraints. By using thenot_valid: true
option, we can enhance database performance and ensure stability during schema changes that involve foreign keys and other constraints.The Cop also includes test scenarios to ensure accurate detection when the
not_valid: true
option is omitted from bothadd_constraint
andadd_foreign_key
methods. The tests confirm that no offenses occur when the option is used correctly.Background and Motivation
Introducing foreign key constraints on large tables can adversely affect database operations due to prolonged locking and the need for immediate validation. These effects are magnified when multiple constraints are added or during periods of high database activity. By encouraging the use of the
NOT VALID
option, this rule update makes it easier to apply constraints without immediate validation, thereby reducing lock duration and improving overall performance.