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:
authorPatrick Bajao <ebajao@gitlab.com>2019-04-12 07:19:45 +0300
committerPatrick Bajao <ebajao@gitlab.com>2019-04-12 07:19:47 +0300
commit208338fde6750ad327fb8ab57877869435520436 (patch)
treeb1386bbf36eb6b248f756b648db2c3238e213ee6 /app/models/application_record.rb
parentdc8848794bfd2f06345d4dbba8a918aa09ee07a8 (diff)
Add ApplicationRecord#safe_ensure_unique method
Port of https://dev.gitlab.org/gitlab/gitlab-ee/merge_requests/866 to CE excluding the migration and service changes as they don't apply to CE.
Diffstat (limited to 'app/models/application_record.rb')
-rw-r--r--app/models/application_record.rb17
1 files changed, 14 insertions, 3 deletions
diff --git a/app/models/application_record.rb b/app/models/application_record.rb
index 6976185264e..348468197bf 100644
--- a/app/models/application_record.rb
+++ b/app/models/application_record.rb
@@ -15,6 +15,19 @@ class ApplicationRecord < ActiveRecord::Base
where(nil).pluck(self.primary_key)
end
+ def self.safe_ensure_unique(retries: 0)
+ transaction(requires_new: true) do
+ yield
+ end
+ rescue ActiveRecord::RecordNotUnique
+ if retries > 0
+ retries -= 1
+ retry
+ end
+
+ false
+ end
+
def self.safe_find_or_create_by!(*args)
safe_find_or_create_by(*args).tap do |record|
record.validate! unless record.persisted?
@@ -22,10 +35,8 @@ class ApplicationRecord < ActiveRecord::Base
end
def self.safe_find_or_create_by(*args)
- transaction(requires_new: true) do
+ safe_ensure_unique(retries: 1) do
find_or_create_by(*args)
end
- rescue ActiveRecord::RecordNotUnique
- retry
end
end