recover from RecordNotUnique race condition when creating new legacy appeal #15391
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.
Description
If
LegacyAppeal.find_or_create_by_vacols_id
is hit multiple times for an appeal that doesn't yet exist, it's possible for a race condition to arise where the appeal is saved by one process in between the time another process initializes and attempts to save it.I observed this happening when loading the case details page for appeals that exist in VACOLS but not Caseflow, when
find_or_create_by_vacols_id
is invoked from the appeals controller and the tasks controller in quick succession. When the second process attempts to save its version of the appeal, it fails withPG::UniqueViolation
/ActiveRecord::RecordNotUnique
.I changed the method to capture the
ActiveRecord::RecordNotUnique
and return the existing record viafind_by!
(which'll raise aActiveRecord::RecordNotFound
if it's unsuccessful), following this pattern, from the Rails 6create_or_find_by
method.It's not ideal to use an exception as flow control, but I think it's okay in this case to let the database constraint determine the action.