mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Remove ability update locking_column value
This commit is contained in:
parent
f38de5a7b4
commit
5a072ba317
5 changed files with 4 additions and 49 deletions
|
@ -495,11 +495,6 @@
|
||||||
|
|
||||||
*Prathamesh Sonpatki*
|
*Prathamesh Sonpatki*
|
||||||
|
|
||||||
* Optimistic locking: Added ability to update `locking_column` value.
|
|
||||||
Ignore optimistic locking if trying to update with new `locking_column` value.
|
|
||||||
|
|
||||||
*bogdanvlviv*
|
|
||||||
|
|
||||||
* Fixed: Optimistic locking does not work well with `null` in the database.
|
* Fixed: Optimistic locking does not work well with `null` in the database.
|
||||||
|
|
||||||
Fixes #26024.
|
Fixes #26024.
|
||||||
|
|
|
@ -47,8 +47,6 @@ module ActiveRecord
|
||||||
# self.locking_column = :lock_person
|
# self.locking_column = :lock_person
|
||||||
# end
|
# end
|
||||||
#
|
#
|
||||||
# Please note that the optimistic locking will be ignored if you update the
|
|
||||||
# locking column's value.
|
|
||||||
module Optimistic
|
module Optimistic
|
||||||
extend ActiveSupport::Concern
|
extend ActiveSupport::Concern
|
||||||
|
|
||||||
|
@ -80,13 +78,11 @@ module ActiveRecord
|
||||||
|
|
||||||
def _update_record(attribute_names = self.attribute_names)
|
def _update_record(attribute_names = self.attribute_names)
|
||||||
return super unless locking_enabled?
|
return super unless locking_enabled?
|
||||||
|
|
||||||
lock_col = self.class.locking_column
|
|
||||||
|
|
||||||
return super if attribute_names.include?(lock_col)
|
|
||||||
return 0 if attribute_names.empty?
|
return 0 if attribute_names.empty?
|
||||||
|
|
||||||
begin
|
begin
|
||||||
|
lock_col = self.class.locking_column
|
||||||
|
|
||||||
previous_lock_value = read_attribute_before_type_cast(lock_col)
|
previous_lock_value = read_attribute_before_type_cast(lock_col)
|
||||||
|
|
||||||
increment_lock
|
increment_lock
|
||||||
|
|
|
@ -349,13 +349,14 @@ class DirtyTest < ActiveRecord::TestCase
|
||||||
|
|
||||||
def test_partial_update_with_optimistic_locking
|
def test_partial_update_with_optimistic_locking
|
||||||
person = Person.new(first_name: "foo")
|
person = Person.new(first_name: "foo")
|
||||||
old_lock_version = person.lock_version
|
|
||||||
|
|
||||||
with_partial_writes Person, false do
|
with_partial_writes Person, false do
|
||||||
assert_queries(2) { 2.times { person.save! } }
|
assert_queries(2) { 2.times { person.save! } }
|
||||||
Person.where(id: person.id).update_all(first_name: "baz")
|
Person.where(id: person.id).update_all(first_name: "baz")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
old_lock_version = person.lock_version
|
||||||
|
|
||||||
with_partial_writes Person, true do
|
with_partial_writes Person, true do
|
||||||
assert_queries(0) { 2.times { person.save! } }
|
assert_queries(0) { 2.times { person.save! } }
|
||||||
assert_equal old_lock_version, person.reload.lock_version
|
assert_equal old_lock_version, person.reload.lock_version
|
||||||
|
|
|
@ -247,17 +247,6 @@ class OptimisticLockingTest < ActiveRecord::TestCase
|
||||||
assert_equal "new title2", t2.title
|
assert_equal "new title2", t2.title
|
||||||
end
|
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
|
def test_lock_without_default_queries_count
|
||||||
t1 = LockWithoutDefault.create(title: "title1")
|
t1 = LockWithoutDefault.create(title: "title1")
|
||||||
|
|
||||||
|
@ -270,12 +259,6 @@ class OptimisticLockingTest < ActiveRecord::TestCase
|
||||||
assert_equal "title2", t1.title
|
assert_equal "title2", t1.title
|
||||||
assert_equal 1, t1.lock_version
|
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")
|
t2 = LockWithoutDefault.new(title: "title1")
|
||||||
|
|
||||||
assert_queries(1) { t2.save! }
|
assert_queries(1) { t2.save! }
|
||||||
|
@ -321,17 +304,6 @@ class OptimisticLockingTest < ActiveRecord::TestCase
|
||||||
assert_equal "new title2", t2.title
|
assert_equal "new title2", t2.title
|
||||||
end
|
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
|
def test_lock_with_custom_column_without_default_queries_count
|
||||||
t1 = LockWithCustomColumnWithoutDefault.create(title: "title1")
|
t1 = LockWithCustomColumnWithoutDefault.create(title: "title1")
|
||||||
|
|
||||||
|
@ -344,12 +316,6 @@ class OptimisticLockingTest < ActiveRecord::TestCase
|
||||||
assert_equal "title2", t1.title
|
assert_equal "title2", t1.title
|
||||||
assert_equal 1, t1.custom_lock_version
|
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")
|
t2 = LockWithCustomColumnWithoutDefault.new(title: "title1")
|
||||||
|
|
||||||
assert_queries(1) { t2.save! }
|
assert_queries(1) { t2.save! }
|
||||||
|
|
|
@ -953,9 +953,6 @@ class Client < ApplicationRecord
|
||||||
end
|
end
|
||||||
```
|
```
|
||||||
|
|
||||||
NOTE: Please note that the optimistic locking will be ignored if you update the
|
|
||||||
locking column's value.
|
|
||||||
|
|
||||||
### Pessimistic Locking
|
### Pessimistic Locking
|
||||||
|
|
||||||
Pessimistic locking uses a locking mechanism provided by the underlying database. Using `lock` when building a relation obtains an exclusive lock on the selected rows. Relations using `lock` are usually wrapped inside a transaction for preventing deadlock conditions.
|
Pessimistic locking uses a locking mechanism provided by the underlying database. Using `lock` when building a relation obtains an exclusive lock on the selected rows. Relations using `lock` are usually wrapped inside a transaction for preventing deadlock conditions.
|
||||||
|
|
Loading…
Reference in a new issue