提交 4fbd8adf 编写于 作者: C Christopher Meiklejohn 提交者: Jon Leighton

Don't quote ID's as Arel will quote them -- follow same conventions as the delete method.

上级 a45f300e
...@@ -94,7 +94,7 @@ def update(attribute_names = @attributes.keys) #:nodoc: ...@@ -94,7 +94,7 @@ def update(attribute_names = @attributes.keys) #:nodoc:
relation = self.class.unscoped relation = self.class.unscoped
stmt = relation.where( stmt = relation.where(
relation.table[self.class.primary_key].eq(quoted_id).and( relation.table[self.class.primary_key].eq(id).and(
relation.table[lock_col].eq(quote_value(previous_lock_value)) relation.table[lock_col].eq(quote_value(previous_lock_value))
) )
).arel.compile_update(arel_attributes_values(false, false, attribute_names)) ).arel.compile_update(arel_attributes_values(false, false, attribute_names))
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
require 'models/reader' require 'models/reader'
require 'models/legacy_thing' require 'models/legacy_thing'
require 'models/reference' require 'models/reference'
require 'models/string_key_object'
class LockWithoutDefault < ActiveRecord::Base; end class LockWithoutDefault < ActiveRecord::Base; end
...@@ -18,7 +19,40 @@ class ReadonlyFirstNamePerson < Person ...@@ -18,7 +19,40 @@ class ReadonlyFirstNamePerson < Person
end end
class OptimisticLockingTest < ActiveRecord::TestCase class OptimisticLockingTest < ActiveRecord::TestCase
fixtures :people, :legacy_things, :references fixtures :people, :legacy_things, :references, :string_key_objects
def test_non_integer_lock_existing
s1 = StringKeyObject.find("record1")
s2 = StringKeyObject.find("record1")
assert_equal 0, s1.lock_version
assert_equal 0, s2.lock_version
s1.name = 'updated record'
s1.save!
assert_equal 1, s1.lock_version
assert_equal 0, s2.lock_version
s2.name = 'doubly updated record'
assert_raise(ActiveRecord::StaleObjectError) { s2.save! }
end
def test_non_integer_lock_destroy
s1 = StringKeyObject.find("record1")
s2 = StringKeyObject.find("record1")
assert_equal 0, s1.lock_version
assert_equal 0, s2.lock_version
s1.name = 'updated record'
s1.save!
assert_equal 1, s1.lock_version
assert_equal 0, s2.lock_version
assert_raise(ActiveRecord::StaleObjectError) { s2.destroy }
assert s1.destroy
assert s1.frozen?
assert s1.destroyed?
assert_raises(ActiveRecord::RecordNotFound) { StringKeyObject.find("record1") }
end
def test_lock_existing def test_lock_existing
p1 = Person.find(1) p1 = Person.find(1)
......
first:
id: record1
name: first record
second:
id: record2
name: second record
class StringKeyObject < ActiveRecord::Base
set_primary_key :id
end
...@@ -543,6 +543,12 @@ def create_table(*args, &block) ...@@ -543,6 +543,12 @@ def create_table(*args, &block)
t.string :sponsorable_type t.string :sponsorable_type
end end
create_table :string_key_objects, :id => false, :primary_key => :id, :force => true do |t|
t.string :id
t.string :name
t.integer :lock_version, :null => false, :default => 0
end
create_table :students, :force => true do |t| create_table :students, :force => true do |t|
t.string :name t.string :name
end end
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册