mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Don't quote ID's as Arel will quote them -- follow same conventions as the delete method.
This commit is contained in:
parent
a45f300e5f
commit
4fbd8adf48
5 changed files with 52 additions and 2 deletions
|
@ -94,7 +94,7 @@ module ActiveRecord
|
|||
relation = self.class.unscoped
|
||||
|
||||
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))
|
||||
)
|
||||
).arel.compile_update(arel_attributes_values(false, false, attribute_names))
|
||||
|
|
|
@ -5,6 +5,7 @@ require 'models/job'
|
|||
require 'models/reader'
|
||||
require 'models/legacy_thing'
|
||||
require 'models/reference'
|
||||
require 'models/string_key_object'
|
||||
|
||||
class LockWithoutDefault < ActiveRecord::Base; end
|
||||
|
||||
|
@ -18,7 +19,40 @@ class ReadonlyFirstNamePerson < Person
|
|||
end
|
||||
|
||||
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
|
||||
p1 = Person.find(1)
|
||||
|
|
7
activerecord/test/fixtures/string_key_objects.yml
vendored
Normal file
7
activerecord/test/fixtures/string_key_objects.yml
vendored
Normal file
|
@ -0,0 +1,7 @@
|
|||
first:
|
||||
id: record1
|
||||
name: first record
|
||||
|
||||
second:
|
||||
id: record2
|
||||
name: second record
|
3
activerecord/test/models/string_key_object.rb
Normal file
3
activerecord/test/models/string_key_object.rb
Normal file
|
@ -0,0 +1,3 @@
|
|||
class StringKeyObject < ActiveRecord::Base
|
||||
set_primary_key :id
|
||||
end
|
|
@ -543,6 +543,12 @@ ActiveRecord::Schema.define do
|
|||
t.string :sponsorable_type
|
||||
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|
|
||||
t.string :name
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue