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

Changeset 7238

Show
Ignore:
Timestamp:
07/25/07 03:48:30 (1 year ago)
Author:
rick
Message:

Perform a deep #dup on query cache results so that modifying activerecord attributes does not modify the cached attributes. [Rick]

Files:

Legend:

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

    r7237 r7238  
    11*SVN* 
     2 
     3* Perform a deep #dup on query cache results so that modifying activerecord attributes does not modify the cached attributes.  [Rick] 
    24 
    35# Ensure that has_many :through associations use a count query instead of loading the target when #size is called.  Closes #8800 [lifo] 
  • trunk/activerecord/lib/active_record/query_cache.rb

    r6202 r7238  
    5959          @query_cache[sql] = yield 
    6060        end 
    61          
    62         result ? result.dup : nil 
     61 
     62        if result 
     63          # perform a deep #dup in case result is an array 
     64          result = result.collect { |row| row.dup } if result.respond_to?(:collect) 
     65          result.dup 
     66        else 
     67          nil 
     68        end 
    6369      end 
    6470     
  • trunk/activerecord/test/query_cache_test.rb

    r6195 r7238  
    99   
    1010  def test_find_queries 
    11     assert_queries(2) {  Task.find(1); Task.find(1) }         
     11    assert_queries(2) {  Task.find(1); Task.find(1) } 
    1212  end 
    1313 
    1414  def test_find_queries_with_cache 
    1515    Task.cache do 
    16       assert_queries(1) {  Task.find(1); Task.find(1) }     
     16      assert_queries(1) {  Task.find(1); Task.find(1) } 
    1717    end 
    1818  end 
    1919   
    20   def test_find_queries_with_cache 
     20  def test_query_cache_returned 
     21    assert_not_equal ActiveRecord::QueryCache, Task.connection.class 
    2122    Task.cache do 
    22       assert_queries(1) {  Task.find(1); Task.find(1) }     
     23      assert_equal ActiveRecord::QueryCache, Task.connection.class 
     24    end     
     25  end 
     26 
     27  def test_query_cache_dups_results_correctly 
     28    Task.cache do 
     29      now  = Time.now.utc 
     30      task = Task.find 1 
     31      assert_not_equal now, task.starting 
     32      task.starting = now 
     33      task.reload 
     34      assert_not_equal now, task.starting 
     35    end 
     36  end 
     37 
     38  def test_cache_is_scoped_on_actual_class_only 
     39    Task.cache do 
     40      Topic.columns # don't count this query 
     41      assert_queries(2) {  Topic.find(1); Topic.find(1); } 
    2342    end 
    2443  end 
    2544   
    26   def test_query_cache_returned         
    27     assert_not_equal ActiveRecord::QueryCache, Task.connection.class 
    28     Task.cache do  
    29       assert_equal ActiveRecord::QueryCache, Task.connection.class       
    30     end     
    31   end 
    32    
    33  
    34   def test_cache_is_scoped_on_actual_class_only 
    35     Task.cache do 
    36       assert_queries(2) {  Topic.find(1); Topic.find(1) }     
    37     end 
    38   end 
    39    
    40    
    4145  def test_cache_is_scoped_on_all_descending_classes 
    4246    ActiveRecord::Base.cache do 
    43       assert_queries(1) {  Task.find(1); Task.find(1) }     
     47      assert_queries(1) {  Task.find(1); Task.find(1) } 
    4448    end 
    4549  end 
     
    5458    end 
    5559  end 
    56      
    57    
    5860end 
    59  
    6061 
    6162uses_mocha('QueryCacheExpiryTest') do