From ce102e3af9ccdb5015e20ab7155d2a13c7832a9a Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Sat, 15 Dec 2007 01:45:30 +0000 Subject: Ensure optimistic locking handles nil #lock_version values properly. Closes #10510 [rick] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8395 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/CHANGELOG | 2 ++ activerecord/lib/active_record/locking/optimistic.rb | 2 +- activerecord/test/locking_test.rb | 9 +++++++++ 3 files changed, 12 insertions(+), 1 deletion(-) (limited to 'activerecord') diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index f0bf07e65b..90885b18c0 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Ensure optimistic locking handles nil #lock_version values properly. Closes #10510 [rick] + * Make the Fixtures Test::Unit enhancements more supporting for double-loaded test cases. Closes #10379 [brynary] * Fix that validates_acceptance_of still works for non-existent tables (useful for bootstrapping new databases). Closes #10474 [hasmanyjosh] diff --git a/activerecord/lib/active_record/locking/optimistic.rb b/activerecord/lib/active_record/locking/optimistic.rb index ecd6a2b258..799309c17b 100644 --- a/activerecord/lib/active_record/locking/optimistic.rb +++ b/activerecord/lib/active_record/locking/optimistic.rb @@ -70,7 +70,7 @@ module ActiveRecord return update_without_lock unless locking_enabled? lock_col = self.class.locking_column - previous_value = send(lock_col) + previous_value = send(lock_col).to_i send(lock_col + '=', previous_value + 1) begin diff --git a/activerecord/test/locking_test.rb b/activerecord/test/locking_test.rb index 97fbfaaeb1..9a3b76e810 100644 --- a/activerecord/test/locking_test.rb +++ b/activerecord/test/locking_test.rb @@ -64,6 +64,15 @@ class OptimisticLockingTest < Test::Unit::TestCase assert_raises(ActiveRecord::StaleObjectError) { p2.save! } end + + def test_lock_new_with_nil + p1 = Person.new(:first_name => 'anika') + p1.save! + p1.lock_version = nil # simulate bad fixture or column with no default + p1.save! + assert_equal 1, p1.lock_version + end + def test_lock_column_name_existing t1 = LegacyThing.find(1) -- cgit v1.2.3