aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorArthur Neves <arthurnn@gmail.com>2016-06-14 12:31:08 -0400
committerArthur Neves <arthurnn@gmail.com>2016-06-14 12:37:50 -0400
commit009b2a54344eb2463fa4018d9b20cdfc4c0495ac (patch)
treec68c9d5ba83aee03675a564cbb2df572489a63ed
parent596669cfac8e63d14f905bc1a8891796d3c00b02 (diff)
downloadrails-009b2a54344eb2463fa4018d9b20cdfc4c0495ac.tar.gz
rails-009b2a54344eb2463fa4018d9b20cdfc4c0495ac.tar.bz2
rails-009b2a54344eb2463fa4018d9b20cdfc4c0495ac.zip
Respect the current `connected?` method when calling `cache`
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.
-rw-r--r--activerecord/lib/active_record/query_cache.rb4
-rw-r--r--activerecord/test/cases/query_cache_test.rb15
2 files changed, 17 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb
index ca12a603da..07d863d15e 100644
--- a/activerecord/lib/active_record/query_cache.rb
+++ b/activerecord/lib/active_record/query_cache.rb
@@ -5,7 +5,7 @@ module ActiveRecord
# 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
@@ -15,7 +15,7 @@ module ActiveRecord
# 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
diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb
index 199a41d428..25b16ae142 100644
--- a/activerecord/test/cases/query_cache_test.rb
+++ b/activerecord/test/cases/query_cache_test.rb
@@ -174,6 +174,21 @@ class QueryCacheTest < ActiveRecord::TestCase
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