Skip to content

Commit

Permalink
Don't bust AR query cache when types are not same object
Browse files Browse the repository at this point in the history
AR's query cache currently depends on the type objects being not only
equivalent but also to be the same object. Either reloading the type
information or using custom type objects that aren't cached will
unnecessarily bust the cache.

This is fixed in 5.0 as an ancillary part of 574f255
but here I also add a test for the condition.
  • Loading branch information
jcoleman committed Aug 23, 2016
1 parent 2509b24 commit fce3dbf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
@@ -1,3 +1,9 @@
* Fix query caching when type information is reset

Backports ancillary fix in 5.0.

*James Coleman*

* Allow `joins` to be unscoped.

Fixes #13775.
Expand Down
5 changes: 5 additions & 0 deletions activerecord/lib/active_record/type/value.rb
Expand Up @@ -86,6 +86,11 @@ def ==(other)
scale == other.scale &&
limit == other.limit
end
alias eql? ==

def hash
[self.class, precision, scale, limit].hash
end

private

Expand Down
20 changes: 20 additions & 0 deletions activerecord/test/cases/query_cache_test.rb
Expand Up @@ -243,6 +243,26 @@ def test_query_cache_doesnt_leak_cached_results_of_rolled_back_queries
assert_equal 0, Post.where(title: 'rollback').to_a.count
end
end

def test_query_cached_even_when_types_are_reset
Task.cache do
# Warm the cache
task = Task.find(1)

Task.connection.type_map.clear

# Preload the type cache again (so we don't have those queries issued during our assertions)
Task.connection.send(:initialize_type_map, Task.connection.type_map)

# Clear places where type information is cached
Task.reset_column_information
Task.find_by_statement_cache.clear

assert_queries(0) do
Task.find(1)
end
end
end
end

class QueryCacheExpiryTest < ActiveRecord::TestCase
Expand Down

0 comments on commit fce3dbf

Please sign in to comment.