diff options
author | Franck Verrot <franck@verrot.fr> | 2010-11-27 11:14:26 +0100 |
---|---|---|
committer | Santiago Pastorino and José Ignacio Costa <santiago+jose@wyeworks.com> | 2011-02-09 18:27:40 -0200 |
commit | 8bc464c8090f2929917ecd2d9476c914aa6da787 (patch) | |
tree | 7d392467d5a10756edca0f302f2b518e4acb68de | |
parent | d3b2596884d884b72d50de2b7ced6097df670736 (diff) | |
download | rails-8bc464c8090f2929917ecd2d9476c914aa6da787.tar.gz rails-8bc464c8090f2929917ecd2d9476c914aa6da787.tar.bz2 rails-8bc464c8090f2929917ecd2d9476c914aa6da787.zip |
The optimistic lock column should be increased when calling touch
Signed-off-by: Santiago Pastorino and José Ignacio Costa <santiago+jose@wyeworks.com>
-rw-r--r-- | activerecord/lib/active_record/persistence.rb | 7 | ||||
-rw-r--r-- | activerecord/test/cases/locking_test.rb | 8 | ||||
-rw-r--r-- | activerecord/test/schema/schema.rb | 1 |
3 files changed, 16 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/persistence.rb b/activerecord/lib/active_record/persistence.rb index b05957981d..3fd67078b4 100644 --- a/activerecord/lib/active_record/persistence.rb +++ b/activerecord/lib/active_record/persistence.rb @@ -224,6 +224,7 @@ module ActiveRecord def touch(name = nil) attributes = timestamp_attributes_for_update_in_model attributes << name if name + unless attributes.empty? current_time = current_time_from_proper_timezone changes = {} @@ -232,6 +233,12 @@ module ActiveRecord changes[column.to_s] = write_attribute(column.to_s, current_time) end + if locking_enabled? + lock_col = self.class.locking_column.to_s + previous_value = send(lock_col).to_i + changes[lock_col] = write_attribute(lock_col, previous_value + 1) + end + @changed_attributes.except!(*changes.keys) primary_key = self.class.primary_key self.class.update_all(changes, { primary_key => self[primary_key] }) == 1 diff --git a/activerecord/test/cases/locking_test.rb b/activerecord/test/cases/locking_test.rb index 3ce65a5b6b..636a709924 100644 --- a/activerecord/test/cases/locking_test.rb +++ b/activerecord/test/cases/locking_test.rb @@ -99,6 +99,14 @@ class OptimisticLockingTest < ActiveRecord::TestCase assert_equal 1, p1.lock_version end + def test_touch_existing_lock + p1 = Person.find(1) + assert_equal 0, p1.lock_version + + p1.touch + assert_equal 1, p1.lock_version + end + def test_lock_column_name_existing t1 = LegacyThing.find(1) diff --git a/activerecord/test/schema/schema.rb b/activerecord/test/schema/schema.rb index b1763ff431..0b3865fc78 100644 --- a/activerecord/test/schema/schema.rb +++ b/activerecord/test/schema/schema.rb @@ -430,6 +430,7 @@ ActiveRecord::Schema.define do t.references :number1_fan t.integer :lock_version, :null => false, :default => 0 t.string :comments + t.timestamps end create_table :pets, :primary_key => :pet_id ,:force => true do |t| |