Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Brandl <abrandl@gitlab.com>2018-03-05 19:12:22 +0300
committerAndreas Brandl <abrandl@gitlab.com>2018-03-06 14:53:15 +0300
commitd270b857341efe6d878f3c23ca8707608c97d9d9 (patch)
tree6cc617d5283a88ac1b18bfb0a2c6314357c887ef /app/models/user_interacted_project.rb
parente83e85ce11ecb6db1707b9fddcace5d9ec62f123 (diff)
Nested transaction for find_or_create_by! queries.
For background see https://apidock.com/rails/v4.2.7/ActiveRecord/Relation/find_or_create_by.
Diffstat (limited to 'app/models/user_interacted_project.rb')
-rw-r--r--app/models/user_interacted_project.rb16
1 files changed, 11 insertions, 5 deletions
diff --git a/app/models/user_interacted_project.rb b/app/models/user_interacted_project.rb
index 064746457cf..68cb3c01c50 100644
--- a/app/models/user_interacted_project.rb
+++ b/app/models/user_interacted_project.rb
@@ -23,11 +23,17 @@ class UserInteractedProject < ActiveRecord::Base
}
cached_exists?(attributes) do
- begin
- find_or_create_by!(attributes)
- true # not caching the whole record here for now
- rescue ActiveRecord::RecordNotUnique
- retry
+ transaction(requires_new: true) do
+ begin
+ where(attributes).select(1).first || create!(attributes)
+ true # not caching the whole record here for now
+ rescue ActiveRecord::RecordNotUnique
+ # Note, above queries are not atomic and prone
+ # to race conditions (similar like #find_or_create!).
+ # We retry and make sure the outer transaction (if any)
+ # is not aborted because of this.
+ retry
+ end
end
end
end