diff options
-rw-r--r-- | config/initializers/config_initializers_active_record_locking.rb | 41 | ||||
-rw-r--r-- | spec/models/issue_spec.rb | 12 | ||||
-rw-r--r-- | spec/models/merge_request_spec.rb | 12 |
3 files changed, 65 insertions, 0 deletions
diff --git a/config/initializers/config_initializers_active_record_locking.rb b/config/initializers/config_initializers_active_record_locking.rb new file mode 100644 index 00000000000..1c4352b135d --- /dev/null +++ b/config/initializers/config_initializers_active_record_locking.rb @@ -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 diff --git a/spec/models/issue_spec.rb b/spec/models/issue_spec.rb index 0cd69cb4817..0ce4add5669 100644 --- a/spec/models/issue_spec.rb +++ b/spec/models/issue_spec.rb @@ -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) } diff --git a/spec/models/merge_request_spec.rb b/spec/models/merge_request_spec.rb index 7457efef013..ec2aef6f815 100644 --- a/spec/models/merge_request_spec.rb +++ b/spec/models/merge_request_spec.rb @@ -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 |