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

Merge pull request #28926 from bogdanvlviv/fix-destroy-with-locking_column-value-null

Fix destroy with locking_column value null
This commit is contained in:
Rafael França 2017-06-20 17:41:00 -04:00 committed by GitHub
commit 890de77052
3 changed files with 31 additions and 1 deletions

View file

@ -1,3 +1,8 @@
* Fix destroying existing object does not work well when optimistic locking enabled and
`locking column` is null in the database.
*bogdanvlviv*
* Use bulk INSERT to insert fixtures for better performance.
*Kir Shatrov*

View file

@ -128,7 +128,7 @@ module ActiveRecord
if locking_enabled?
locking_column = self.class.locking_column
relation = relation.where(locking_column => _read_attribute(locking_column))
relation = relation.where(locking_column => read_attribute_before_type_cast(locking_column))
end
relation

View file

@ -453,6 +453,31 @@ class OptimisticLockingWithSchemaChangeTest < ActiveRecord::TestCase
PersonalLegacyThing.reset_column_information
end
def test_destroy_existing_object_with_locking_column_value_null_in_the_database
ActiveRecord::Base.connection.execute("INSERT INTO lock_without_defaults(title) VALUES('title1')")
t1 = LockWithoutDefault.last
assert_equal 0, t1.lock_version
assert_nil t1.lock_version_before_type_cast
t1.destroy
assert t1.destroyed?
end
def test_destroy_stale_object
t1 = LockWithoutDefault.create!(title: "title1")
stale_object = LockWithoutDefault.find(t1.id)
t1.update!(title: "title2")
assert_raises(ActiveRecord::StaleObjectError) do
stale_object.destroy!
end
refute stale_object.destroyed?
end
private
def add_counter_column_to(model, col = "test_count")