diff options
author | Stan Hu <stanhu@gmail.com> | 2019-05-06 14:42:21 -0700 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2019-05-06 21:21:56 -0700 |
commit | b7b852c250a95648d5954e0ead6c37b5ff8b35d9 (patch) | |
tree | b022cb42e2a902d09b5874469412cab53893c041 | |
parent | e1aa7f5074aab8190d16a6f25121d33b99a07238 (diff) | |
download | gitlab-ce-sh-fix-activerecord-patch-mark2.tar.gz |
Additional fix to handle NULL lock_versionsh-fix-activerecord-patch-mark2
If the UI sends a string value for lock_version (e.g. "0"), then the
previous monkey patch did not properly handle that properly. This
commit casts the value to an integer to determine whether to look for
NULL lock_versions.
For merge requests, GitLab sends a POST request to
`namespace/project/merge_requests/:iid` with the
`merge_request[lock_version]` parameter with a string `0`. The string
value comes from the form field, which explains why
https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/28145 wasn't
sufficient.
-rw-r--r-- | config/initializers/config_initializers_active_record_locking.rb | 6 | ||||
-rw-r--r-- | spec/models/issue_spec.rb | 21 | ||||
-rw-r--r-- | spec/models/merge_request_spec.rb | 21 |
3 files changed, 37 insertions, 11 deletions
diff --git a/config/initializers/config_initializers_active_record_locking.rb b/config/initializers/config_initializers_active_record_locking.rb index 1c4352b135d..608d63223a3 100644 --- a/config/initializers/config_initializers_active_record_locking.rb +++ b/config/initializers/config_initializers_active_record_locking.rb @@ -1,4 +1,8 @@ # frozen_string_literal: true + +# ensure ActiveRecord's version has been required already +require 'active_record/locking/optimistic' + # rubocop:disable Lint/RescueException module ActiveRecord module Locking @@ -16,7 +20,7 @@ module ActiveRecord 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 + possible_previous_lock_value = previous_lock_value.to_i == 0 ? [nil, 0] : previous_lock_value affected_rows = self.class.unscoped._update_record( arel_attributes_with_values(attribute_names), diff --git a/spec/models/issue_spec.rb b/spec/models/issue_spec.rb index 0ce4add5669..cc777cbf749 100644 --- a/spec/models/issue_spec.rb +++ b/spec/models/issue_spec.rb @@ -56,14 +56,25 @@ describe Issue do end describe 'locking' do - it 'works when an issue has a NULL lock_version' do - issue = create(:issue) + using RSpec::Parameterized::TableSyntax - 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 diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb index ec2aef6f815..c72b6e9033d 100644 --- a/spec/models/merge_request_spec.rb +++ b/spec/models/merge_request_spec.rb @@ -32,14 +32,25 @@ describe MergeRequest do end describe 'locking' do - it 'works when a merge request has a NULL lock_version' do - merge_request = create(:merge_request) + using RSpec::Parameterized::TableSyntax - 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 |