Commit 82370345 authored by Douwe Maan's avatar Douwe Maan

Support both 0 and NULL lock_versions

parent fd27be93
...@@ -19,12 +19,7 @@ module ActiveRecord ...@@ -19,12 +19,7 @@ module ActiveRecord
return 0 if attribute_names.empty? return 0 if attribute_names.empty?
lock_col = self.class.locking_column lock_col = self.class.locking_column
previous_lock_value = send(lock_col).to_i 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 increment_lock
attribute_names += [lock_col] attribute_names += [lock_col]
...@@ -35,7 +30,8 @@ module ActiveRecord ...@@ -35,7 +30,8 @@ module ActiveRecord
affected_rows = relation.where( affected_rows = relation.where(
self.class.primary_key => id, 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( ).update_all(
attributes_for_update(attribute_names).map do |name| attributes_for_update(attribute_names).map do |name|
[name, _read_attribute(name)] [name, _read_attribute(name)]
......
# 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
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