-
Notifications
You must be signed in to change notification settings - Fork 19
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
Idempotency bugs in add_reference_concurrently
#56
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ghiculescu
force-pushed
the
idempotency-bugs
branch
from
June 24, 2024 05:38
a02d891
to
03da3de
Compare
ghiculescu
commented
Jun 24, 2024
assert_empty @connection.foreign_keys(:projects) | ||
|
||
migrate AddReferenceForeignKeyNoValidate | ||
assert_raises_with_message(StandardError, /column "user_id" of relation "projects" already exists/) do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this is expected behaviour, so I added this as a passing test (though I guess it would be nice if it was consistent with add_reference_concurrently
).
ghiculescu
commented
Jun 24, 2024
ghiculescu
commented
Jun 24, 2024
Thank you for the PR! |
Thanks for the fix! |
We should have included this test too: def test_add_reference_concurrently_with_unvalidated_foreign_key_is_idempotent
assert_empty connection.foreign_keys(:milestones)
connection.add_reference_concurrently :milestones, :project, foreign_key: { validate: false }
connection.add_reference_concurrently :milestones, :project, foreign_key: { validate: false }
assert_equal 1, connection.foreign_keys(:milestones).size
end It passes with this fix, so all good. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The adding of foreign keys in
add_reference_concurrently
is not idempotent. Instead of being safely skipped here, an error is raised.The root cause is the merging in of
validate: false
here. That gets passed through to the check here. It means that if you useadd_reference_concurrently
to create a validated foreign key, and then calladd_reference_concurrently
again, it will always raise, because the matching foreign key won't be found.I'm not sure what the right approach to fixing this is yet but I wanted to add some failing tests to demonstrate the issue.