Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 4958

Show
Ignore:
Timestamp:
09/04/06 00:02:38 (2 years ago)
Author:
bitsweat
Message:

Optimistic locking: gracefully handle nil versions, treat as zero. Closes #5908.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/activerecord/CHANGELOG

    r4917 r4958  
    11*SVN* 
     2 
     3* Optimistic locking: gracefully handle nil versions, treat as zero.  #5908 [Tom Ward] 
    24 
    35* validates_confirmation_of only kicks in when the attribute, rather than its confirmation, is present.  #785 [z@wzph.com] 
  • trunk/activerecord/lib/active_record/locking/optimistic.rb

    r4462 r4958  
    3131 
    3232        base.alias_method_chain :update, :lock 
     33        base.alias_method_chain :attributes_from_column_definition, :lock 
     34         
    3335        class << base 
    3436          alias_method :locking_column=, :set_locking_column 
     
    3840      def locking_enabled? #:nodoc: 
    3941        lock_optimistically && respond_to?(self.class.locking_column) 
     42      end 
     43 
     44      def attributes_from_column_definition_with_lock 
     45        result = attributes_from_column_definition_without_lock 
     46         
     47        # If the locking column has no default value set, 
     48        # start the lock version at zero.  Note we can't use 
     49        # locking_enabled? at this point as @attributes may 
     50        # not have been initialized yet 
     51         
     52        if lock_optimistically && result.include?(self.class.locking_column) 
     53          result[self.class.locking_column] ||= 0 
     54        end 
     55         
     56        return result 
    4057      end 
    4158 
  • trunk/activerecord/test/fixtures/db_definitions/schema.rb

    r4777 r4958  
    5050  end 
    5151  add_index :edges, [:source_id, :sink_id], :unique => true, :name => 'unique_edge_index' 
     52 
     53  create_table :lock_without_defaults, :force => true do |t| 
     54    t.column :lock_version, :integer 
     55  end 
     56   
     57  create_table :lock_with_custom_column_without_defaults, :force => true do |t| 
     58    t.column :custom_lock_version, :integer 
     59  end 
    5260end 
  • trunk/activerecord/test/locking_test.rb

    r4601 r4958  
    22require 'fixtures/person' 
    33require 'fixtures/legacy_thing' 
     4 
     5class LockWithoutDefault < ActiveRecord::Base; end 
     6 
     7class LockWithCustomColumnWithoutDefault < ActiveRecord::Base 
     8  set_locking_column :custom_lock_version 
     9end 
    410 
    511class OptimisticLockingTest < Test::Unit::TestCase 
     
    5662    assert_equal 1, p1.lock_version 
    5763    assert_equal p1.lock_version, Person.new(p1.attributes).lock_version 
     64  end 
     65   
     66  def test_lock_without_default_sets_version_to_zero 
     67    t1 = LockWithoutDefault.new 
     68    assert_equal 0, t1.lock_version 
     69  end 
     70   
     71  def test_lock_with_custom_column_without_default_sets_version_to_zero 
     72    t1 = LockWithCustomColumnWithoutDefault.new 
     73    assert_equal 0, t1.custom_lock_version 
    5874  end 
    5975end