diff options
author | Andreas Brandl <abrandl@gitlab.com> | 2018-03-08 19:24:09 +0100 |
---|---|---|
committer | Andreas Brandl <abrandl@gitlab.com> | 2018-03-08 19:24:09 +0100 |
commit | 0c2085331829f65078207fe95f9784b9c1469281 (patch) | |
tree | 94567b74058e750ae0f9bfced7b62d2b185ef061 | |
parent | 9852ef8d3bdcf07224540be3949188c55fa84c67 (diff) | |
download | gitlab-ce-31114-internal-ids-are-not-atomic-2.tar.gz |
SAVEPOINT31114-internal-ids-are-not-atomic-2
-rw-r--r-- | app/models/internal_id.rb | 13 | ||||
-rw-r--r-- | app/models/issue.rb | 2 | ||||
-rw-r--r-- | config/environments/development.rb | 2 | ||||
-rw-r--r-- | lib/tasks/dev.rake | 11 |
4 files changed, 14 insertions, 14 deletions
diff --git a/app/models/internal_id.rb b/app/models/internal_id.rb index 687145d4475..fa3d63e6350 100644 --- a/app/models/internal_id.rb +++ b/app/models/internal_id.rb @@ -25,14 +25,8 @@ class InternalId < ActiveRecord::Base raise "Unknown through '#{through}' for '#{scope}'" unless scope.respond_to?(through) scope.transaction do - # Reload scope so we can check if the foreign key to InternalId is present already - scope.reload - # Create a record in internal_ids if one does not yet exist - id = get(scope, through) || create_record(subject, scope, through, init).tap do |id| - scope.public_send("#{through}=".to_sym, id) - scope.save! - end + id = get(scope, through) || create_record(subject, scope, through, init) # This will lock the InternalId record with ROW SHARE # and increment #last_value @@ -60,7 +54,10 @@ class InternalId < ActiveRecord::Base # create a new InternalId record each and update the `through` # column (one record will be lost in the process). scope.lock! - get(scope, through) || new(last_value: init.call(subject) || 0) + get(scope, through) || create!(last_value: init.call(subject) || 0).tap do |id| + scope.public_send("#{through}=".to_sym, id) # rubocop:disable GitlabSecurity/PublicSend + scope.save! + end end end end diff --git a/app/models/issue.rb b/app/models/issue.rb index 8e19be423b3..6c368e070b8 100644 --- a/app/models/issue.rb +++ b/app/models/issue.rb @@ -24,7 +24,7 @@ class Issue < ActiveRecord::Base belongs_to :project belongs_to :moved_to, class_name: 'Issue' - has_internal_id :iid, scope: :project, through: :issues_iid, init: ->(o) { o.project.issues.maximum(:iid) } + #has_internal_id :iid, scope: :project, through: :issues_iid, init: ->(o) { o.project.issues.maximum(:iid) } has_many :events, as: :target, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent diff --git a/config/environments/development.rb b/config/environments/development.rb index 45a8c1add3e..aeec03dabf9 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -16,6 +16,8 @@ Rails.application.configure do # Print deprecation notices to the Rails logger config.active_support.deprecation = :log + config.log_level = :debug + # Raise an error on page load if there are pending migrations config.active_record.migration_error = :page_load diff --git a/lib/tasks/dev.rake b/lib/tasks/dev.rake index 2db9e3e0b99..39c7d1a8527 100644 --- a/lib/tasks/dev.rake +++ b/lib/tasks/dev.rake @@ -14,13 +14,14 @@ namespace :dev do end task idtest: :environment do - project = Project.where(name: 'lkjlkjlkjlkj').first - user = User.second + ActiveRecord::Base.logger = Logger.new(STDOUT) i = 0 begin - Issue.create(author: user, title: "Issue #{i}", project: project) + project = Project.first + user = User.second + issue = Issue.create!(author: user, title: "Issue #{i}", project: project) i += 1 - puts i - end while true + puts issue.iid + end while i < 10 end end |