mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Added ability update locking_column value
This commit is contained in:
parent
22a822e581
commit
a60a20bb7f
4 changed files with 90 additions and 7 deletions
|
@ -1,3 +1,8 @@
|
|||
* Optimistic locking: Added ability update locking_column value.
|
||||
Ignore optimistic locking if update with new locking_column value.
|
||||
|
||||
*bogdanvlviv*
|
||||
|
||||
* Fixed: Optimistic locking does not work well with null in the database.
|
||||
|
||||
Fixes #26024
|
||||
|
|
|
@ -78,17 +78,19 @@ module ActiveRecord
|
|||
|
||||
def _update_record(attribute_names = self.attribute_names) #:nodoc:
|
||||
return super unless locking_enabled?
|
||||
return 0 if attribute_names.empty?
|
||||
|
||||
lock_col = self.class.locking_column
|
||||
|
||||
return super if attribute_names.include?(lock_col)
|
||||
return 0 if attribute_names.empty?
|
||||
|
||||
begin
|
||||
previous_lock_value = read_attribute_before_type_cast(lock_col)
|
||||
|
||||
increment_lock
|
||||
|
||||
attribute_names += [lock_col]
|
||||
attribute_names.uniq!
|
||||
attribute_names.push(lock_col)
|
||||
|
||||
begin
|
||||
relation = self.class.unscoped
|
||||
|
||||
affected_rows = relation.where(
|
||||
|
|
|
@ -341,7 +341,7 @@ class DirtyTest < ActiveRecord::TestCase
|
|||
|
||||
def test_partial_update_with_optimistic_locking
|
||||
person = Person.new(first_name: "foo")
|
||||
old_lock_version = 1
|
||||
old_lock_version = person.lock_version
|
||||
|
||||
with_partial_writes Person, false do
|
||||
assert_queries(2) { 2.times { person.save! } }
|
||||
|
|
|
@ -247,6 +247,44 @@ class OptimisticLockingTest < ActiveRecord::TestCase
|
|||
assert_equal "new title2", t2.title
|
||||
end
|
||||
|
||||
def test_lock_without_default_should_update_with_lock_col
|
||||
t1 = LockWithoutDefault.create(title: "title1", lock_version: 6)
|
||||
|
||||
assert_equal 6, t1.lock_version
|
||||
|
||||
t1.update(lock_version: 0)
|
||||
t1.reload
|
||||
|
||||
assert_equal 0, t1.lock_version
|
||||
end
|
||||
|
||||
def test_lock_without_default_queries_count
|
||||
t1 = LockWithoutDefault.create(title: "title1")
|
||||
|
||||
assert_equal "title1", t1.title
|
||||
assert_equal 0, t1.lock_version
|
||||
|
||||
assert_queries(1) { t1.update(title: "title2") }
|
||||
|
||||
t1.reload
|
||||
assert_equal "title2", t1.title
|
||||
assert_equal 1, t1.lock_version
|
||||
|
||||
assert_queries(1) { t1.update(title: "title3", lock_version: 6) }
|
||||
|
||||
t1.reload
|
||||
assert_equal "title3", t1.title
|
||||
assert_equal 6, t1.lock_version
|
||||
|
||||
t2 = LockWithoutDefault.new(title: "title1")
|
||||
|
||||
assert_queries(1) { t2.save! }
|
||||
|
||||
t2.reload
|
||||
assert_equal "title1", t2.title
|
||||
assert_equal 0, t2.lock_version
|
||||
end
|
||||
|
||||
def test_lock_with_custom_column_without_default_sets_version_to_zero
|
||||
t1 = LockWithCustomColumnWithoutDefault.new
|
||||
|
||||
|
@ -283,6 +321,44 @@ class OptimisticLockingTest < ActiveRecord::TestCase
|
|||
assert_equal "new title2", t2.title
|
||||
end
|
||||
|
||||
def test_lock_with_custom_column_without_default_should_update_with_lock_col
|
||||
t1 = LockWithCustomColumnWithoutDefault.create(title: "title1", custom_lock_version: 6)
|
||||
|
||||
assert_equal 6, t1.custom_lock_version
|
||||
|
||||
t1.update(custom_lock_version: 0)
|
||||
t1.reload
|
||||
|
||||
assert_equal 0, t1.custom_lock_version
|
||||
end
|
||||
|
||||
def test_lock_with_custom_column_without_default_queries_count
|
||||
t1 = LockWithCustomColumnWithoutDefault.create(title: "title1")
|
||||
|
||||
assert_equal "title1", t1.title
|
||||
assert_equal 0, t1.custom_lock_version
|
||||
|
||||
assert_queries(1) { t1.update(title: "title2") }
|
||||
|
||||
t1.reload
|
||||
assert_equal "title2", t1.title
|
||||
assert_equal 1, t1.custom_lock_version
|
||||
|
||||
assert_queries(1) { t1.update(title: "title3", custom_lock_version: 6) }
|
||||
|
||||
t1.reload
|
||||
assert_equal "title3", t1.title
|
||||
assert_equal 6, t1.custom_lock_version
|
||||
|
||||
t2 = LockWithCustomColumnWithoutDefault.new(title: "title1")
|
||||
|
||||
assert_queries(1) { t2.save! }
|
||||
|
||||
t2.reload
|
||||
assert_equal "title1", t2.title
|
||||
assert_equal 0, t2.custom_lock_version
|
||||
end
|
||||
|
||||
def test_readonly_attributes
|
||||
assert_equal Set.new([ "name" ]), ReadonlyNameShip.readonly_attributes
|
||||
|
||||
|
|
Loading…
Reference in a new issue