Skip to content

Commit

Permalink
Respect the current connected? method when calling cache
Browse files Browse the repository at this point in the history
Before we enable query caching we check if the connection is
connected. Before this fix we were always checking against the main
connection, and not the model connection.
  • Loading branch information
arthurnn committed Jun 14, 2016
1 parent 596669c commit 009b2a5
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/query_cache.rb
Expand Up @@ -5,7 +5,7 @@ module ClassMethods
# Enable the query cache within the block if Active Record is configured.
# If it's not, it will execute the given block.
def cache(&block)
if ActiveRecord::Base.connected?
if connected?
connection.cache(&block)
else
yield
Expand All @@ -15,7 +15,7 @@ def cache(&block)
# Disable the query cache within the block if Active Record is configured.
# If it's not, it will execute the given block.
def uncached(&block)
if ActiveRecord::Base.connected?
if connected?
connection.uncached(&block)
else
yield
Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/cases/query_cache_test.rb
Expand Up @@ -174,6 +174,21 @@ def test_cache_is_available_when_connection_is_connected
ActiveRecord::Base.configurations = conf
end

def test_cache_is_not_available_when_using_a_not_connected_connection
spec_name = Task.connection_specification_name
conf = ActiveRecord::Base.configurations['arunit'].merge('name' => 'test2')
ActiveRecord::Base.connection_handler.establish_connection(conf)
Task.connection_specification_name = "test2"
refute Task.connected?

Task.cache do
assert_queries(2) { Task.find(1); Task.find(1) }
end
ensure
ActiveRecord::Base.connection_handler.remove_connection(Task.connection_specification_name)
Task.connection_specification_name = spec_name
end

def test_query_cache_doesnt_leak_cached_results_of_rolled_back_queries
ActiveRecord::Base.connection.enable_query_cache!
post = Post.first
Expand Down

0 comments on commit 009b2a5

Please sign in to comment.