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
path: root/app
diff options
context:
space:
mode:
authorRémy Coutable <remy@rymai.me>2019-02-06 18:05:11 +0300
committerRémy Coutable <remy@rymai.me>2019-02-06 18:05:11 +0300
commitc1758be160084c5277e6057896ac9aad2811edec (patch)
tree03e0fcf3a7197bbfdeed7526c726b0f3e30c038b /app
parent8e9dd62233ac667281aca872c35be08fc213dfd5 (diff)
parentc982edfa1969770696c2e85a8e32160eb0304cbc (diff)
Merge branch 'bvl-fix-race-condition-creating-signature' into 'master'
Avoid race conditions when creating GpgSignature Closes #57304 See merge request gitlab-org/gitlab-ce!24939
Diffstat (limited to 'app')
-rw-r--r--app/models/application_record.rb6
-rw-r--r--app/models/gpg_signature.rb7
2 files changed, 12 insertions, 1 deletions
diff --git a/app/models/application_record.rb b/app/models/application_record.rb
index c4e310e638d..a3d662d8250 100644
--- a/app/models/application_record.rb
+++ b/app/models/application_record.rb
@@ -7,6 +7,12 @@ class ApplicationRecord < ActiveRecord::Base
where(id: ids)
end
+ def self.safe_find_or_create_by!(*args)
+ safe_find_or_create_by(*args).tap do |record|
+ record.validate! unless record.persisted?
+ end
+ end
+
def self.safe_find_or_create_by(*args)
transaction(requires_new: true) do
find_or_create_by(*args)
diff --git a/app/models/gpg_signature.rb b/app/models/gpg_signature.rb
index 0816778deae..7f9ff7bbda6 100644
--- a/app/models/gpg_signature.rb
+++ b/app/models/gpg_signature.rb
@@ -1,6 +1,6 @@
# frozen_string_literal: true
-class GpgSignature < ActiveRecord::Base
+class GpgSignature < ApplicationRecord
include ShaAttribute
sha_attribute :commit_sha
@@ -33,6 +33,11 @@ class GpgSignature < ActiveRecord::Base
)
end
+ def self.safe_create!(attributes)
+ create_with(attributes)
+ .safe_find_or_create_by!(commit_sha: attributes[:commit_sha])
+ end
+
def gpg_key=(model)
case model
when GpgKey