Merge pull request #41049 from rails/revert-39321-fix_update_with_dirty_locking_column

Revert "Fix update with dirty locking column to not match latest object accidentally"
This commit is contained in:
Ryuta Kamizono 2021-01-08 18:19:52 +09:00 committed by GitHub
commit b5b4bdaeac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 33 deletions

View File

@ -133,13 +133,14 @@ module ActiveModel
coder["value"] = value if defined?(@value)
end
def original_value_for_database
if assigned?
original_attribute.original_value_for_database
else
_original_value_for_database
protected
def original_value_for_database
if assigned?
original_attribute.original_value_for_database
else
_original_value_for_database
end
end
end
private
attr_reader :original_attribute
@ -167,7 +168,6 @@ module ActiveModel
def _original_value_for_database
value_before_type_cast
end
private :_original_value_for_database
end
class FromUser < Attribute # :nodoc:

View File

@ -98,7 +98,7 @@ module ActiveRecord
affected_rows = self.class._update_record(
attributes_with_values(attribute_names),
@primary_key => id_in_database,
locking_column => @attributes[locking_column].original_value_for_database
locking_column => previous_lock_value
)
if affected_rows != 1

View File

@ -238,37 +238,14 @@ class OptimisticLockingTest < ActiveRecord::TestCase
end
end
def test_update_with_dirty_locking_column
person = Person.find(1)
person.first_name = "Douglas Adams"
person.lock_version = 42
changes = {
"first_name" => ["Michael", "Douglas Adams"],
"lock_version" => [0, 42],
}
assert_equal changes, person.changes
assert person.save!
assert_empty person.changes
end
def test_explicit_update_lock_column_raise_error
person = Person.find(1)
person2 = Person.find(1)
person2.lock_version = 42
person2.save!
assert_raises(ActiveRecord::StaleObjectError) do
person.first_name = "Douglas Adams"
person.lock_version = person2.lock_version
person.lock_version = 42
changes = {
"first_name" => ["Michael", "Douglas Adams"],
"lock_version" => [0, 43],
}
assert_equal changes, person.changes
assert_predicate person, :lock_version_changed?
person.save
end