Commit 5925b5bb authored by Stan Hu's avatar Stan Hu

Merge branch 'sh-fix-activerecord-patch-mark2' into 'master'

Additional fix to handle NULL lock_version

See merge request gitlab-org/gitlab-ce!28153
parents 6424a3ec b7b852c2
# frozen_string_literal: true # frozen_string_literal: true
# ensure ActiveRecord's version has been required already
require 'active_record/locking/optimistic'
# rubocop:disable Lint/RescueException # rubocop:disable Lint/RescueException
module ActiveRecord module ActiveRecord
module Locking module Locking
...@@ -16,7 +20,7 @@ module ActiveRecord ...@@ -16,7 +20,7 @@ module ActiveRecord
self[locking_column] += 1 self[locking_column] += 1
# Patched because when `lock_version` is read as `0`, it may actually be `NULL` in the DB. # 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 possible_previous_lock_value = previous_lock_value.to_i == 0 ? [nil, 0] : previous_lock_value
affected_rows = self.class.unscoped._update_record( affected_rows = self.class.unscoped._update_record(
arel_attributes_with_values(attribute_names), arel_attributes_with_values(attribute_names),
......
...@@ -56,14 +56,25 @@ describe Issue do ...@@ -56,14 +56,25 @@ describe Issue do
end end
describe 'locking' do describe 'locking' do
it 'works when an issue has a NULL lock_version' do using RSpec::Parameterized::TableSyntax
issue = create(:issue)
described_class.where(id: issue.id).update_all('lock_version = NULL') where(:lock_version) do
[
[0],
["0"]
]
end
issue.update!(lock_version: 0, title: 'locking test') with_them do
it 'works when an issue has a NULL lock_version' do
issue = create(:issue)
expect(issue.reload.title).to eq('locking test') described_class.where(id: issue.id).update_all('lock_version = NULL')
issue.update!(lock_version: lock_version, title: 'locking test')
expect(issue.reload.title).to eq('locking test')
end
end end
end end
......
...@@ -32,14 +32,25 @@ describe MergeRequest do ...@@ -32,14 +32,25 @@ describe MergeRequest do
end end
describe 'locking' do describe 'locking' do
it 'works when a merge request has a NULL lock_version' do using RSpec::Parameterized::TableSyntax
merge_request = create(:merge_request)
described_class.where(id: merge_request.id).update_all('lock_version = NULL') where(:lock_version) do
[
[0],
["0"]
]
end
merge_request.update!(lock_version: 0, title: 'locking test') with_them do
it 'works when a merge request has a NULL lock_version' do
merge_request = create(:merge_request)
expect(merge_request.reload.title).to eq('locking test') described_class.where(id: merge_request.id).update_all('lock_version = NULL')
merge_request.update!(lock_version: lock_version, title: 'locking test')
expect(merge_request.reload.title).to eq('locking test')
end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment