1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Go through normal update_all logic when updating lock columns

This commit is contained in:
Sean Griffin 2014-12-26 17:36:47 -07:00
parent 0520e6559e
commit 15e2d5043f
2 changed files with 7 additions and 19 deletions

View file

@ -80,24 +80,14 @@ module ActiveRecord
begin
relation = self.class.unscoped
# FIXME: Remove the Arel::Nodes::Quoted when we remove type casting
# from Arel (Rails 5.1)
quoted_lock_value = Arel::Nodes::Quoted.new(
self.class.quote_value(
previous_lock_value,
column_for_attribute(lock_col),
)
affected_rows = relation.where(
self.class.primary_key => id,
lock_col => previous_lock_value,
).update_all(
attribute_names.map do |name|
[name, _read_attribute(name)]
end.to_h
)
quoted_id = Arel::Nodes::Quoted.new(id)
stmt = relation.where(
relation.table[self.class.primary_key].eq(quoted_id).and(
relation.table[lock_col].eq(quoted_lock_value))
).arel.compile_update(
arel_attributes_with_values_for_update(attribute_names),
self.class.primary_key
)
affected_rows = self.class.connection.update stmt
unless affected_rows == 1
raise ActiveRecord::StaleObjectError.new(self, "update")

View file

@ -33,8 +33,6 @@ class OptimisticLockingTest < ActiveRecord::TestCase
p1 = Person.find(1)
assert_equal 0, p1.lock_version
Person.expects(:quote_value).with(0, Person.columns_hash[Person.locking_column]).returns('0').once
p1.first_name = 'anika2'
p1.save!