Support both 0 and NULL lock_versions

This commit is contained in:
Douwe Maan 2018-12-27 15:46:57 +01:00
parent fd27be9348
commit 8237034530
No known key found for this signature in database
GPG key ID: 5976703F65143D36
2 changed files with 46 additions and 6 deletions

View file

@ -19,12 +19,7 @@ module ActiveRecord
return 0 if attribute_names.empty?
lock_col = self.class.locking_column
previous_lock_value = send(lock_col).to_i
# This line is added as a patch
previous_lock_value = nil if previous_lock_value == '0' || previous_lock_value == 0
increment_lock
attribute_names += [lock_col]
@ -35,7 +30,8 @@ module ActiveRecord
affected_rows = relation.where(
self.class.primary_key => id,
lock_col => previous_lock_value
# Patched because when `lock_version` is read as `0`, it may actually be `NULL` in the DB.
lock_col => previous_lock_value == 0 ? [nil, 0] : previous_lock_value
).update_all(
attributes_for_update(attribute_names).map do |name|
[name, _read_attribute(name)]

View file

@ -0,0 +1,44 @@
# frozen_string_literal: true
require 'spec_helper'
describe 'ActiveRecord locking' do
let(:issue) { create(:issue) }
shared_examples 'locked model' do
before do
issue.update_column(:lock_version, start_lock_version)
end
it 'can be updated' do
issue.update(title: "New title")
expect(issue.reload.lock_version).to eq(new_lock_version)
end
it 'can be deleted' do
expect { issue.destroy }.to change { Issue.count }.by(-1)
end
end
context 'when lock_version is NULL' do
let(:start_lock_version) { nil }
let(:new_lock_version) { 1 }
it_behaves_like 'locked model'
end
context 'when lock_version is 0' do
let(:start_lock_version) { 0 }
let(:new_lock_version) { 1 }
it_behaves_like 'locked model'
end
context 'when lock_version is 1' do
let(:start_lock_version) { 1 }
let(:new_lock_version) { 2 }
it_behaves_like 'locked model'
end
end