Merge branch 'add-spec-for-optimistic-locking-error' into 'master'

Fix editing issues and MRs with NULL lock_version

See merge request gitlab-org/gitlab-ce!28145
This commit is contained in:
Mayra Cabrera 2019-05-06 20:24:24 +00:00
commit 863f2bcfb6
3 changed files with 65 additions and 0 deletions

View file

@ -0,0 +1,41 @@
# frozen_string_literal: true
# rubocop:disable Lint/RescueException
module ActiveRecord
module Locking
module Optimistic
private
def _update_row(attribute_names, attempted_action = "update")
return super unless locking_enabled?
begin
locking_column = self.class.locking_column
previous_lock_value = read_attribute_before_type_cast(locking_column)
attribute_names << locking_column
self[locking_column] += 1
# Patched because when `lock_version` is read as `0`, it may actually be `NULL` in the DB.
possible_previous_lock_value = previous_lock_value == 0 ? [nil, 0] : previous_lock_value
affected_rows = self.class.unscoped._update_record(
arel_attributes_with_values(attribute_names),
self.class.primary_key => id_in_database,
locking_column => possible_previous_lock_value
)
if affected_rows != 1
raise ActiveRecord::StaleObjectError.new(self, attempted_action)
end
affected_rows
# If something went wrong, revert the locking_column value.
rescue Exception
self[locking_column] = previous_lock_value.to_i
raise
end
end
end
end
end

View file

@ -55,6 +55,18 @@ describe Issue do
end
end
describe 'locking' do
it 'works when an issue has a NULL lock_version' do
issue = create(:issue)
described_class.where(id: issue.id).update_all('lock_version = NULL')
issue.update!(lock_version: 0, title: 'locking test')
expect(issue.reload.title).to eq('locking test')
end
end
describe '#order_by_position_and_priority' do
let(:project) { create :project }
let(:p1) { create(:label, title: 'P1', project: project, priority: 1) }

View file

@ -31,6 +31,18 @@ describe MergeRequest do
end
end
describe 'locking' do
it 'works when a merge request has a NULL lock_version' do
merge_request = create(:merge_request)
described_class.where(id: merge_request.id).update_all('lock_version = NULL')
merge_request.update!(lock_version: 0, title: 'locking test')
expect(merge_request.reload.title).to eq('locking test')
end
end
describe '#squash_in_progress?' do
let(:repo_path) do
Gitlab::GitalyClient::StorageSettings.allow_disk_access do