aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorkennyj <kennyj@gmail.com>2012-10-31 22:27:57 +0900
committerkennyj <kennyj@gmail.com>2012-11-14 00:22:33 +0900
commitfda90755301553e184f625f3ba27d34888dbfad4 (patch)
treee6f454a3553e05b5a01c199b0b0bafe7f5eb06cf /activerecord
parent267fb61277ac960ad443d239febcb3136a007d24 (diff)
downloadrails-fda90755301553e184f625f3ba27d34888dbfad4.tar.gz
rails-fda90755301553e184f625f3ba27d34888dbfad4.tar.bz2
rails-fda90755301553e184f625f3ba27d34888dbfad4.zip
Backport #8074 to 3-2-stable. Use query cache/uncache, when using not only database.yml but also DATABASE_URL.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/CHANGELOG.md5
-rw-r--r--activerecord/lib/active_record/query_cache.rb12
-rw-r--r--activerecord/test/cases/query_cache_test.rb11
3 files changed, 22 insertions, 6 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md
index d8493205d7..36f628ee43 100644
--- a/activerecord/CHANGELOG.md
+++ b/activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 3.2.10 (unreleased)
+* Use query cache/uncache when using ENV["DATABASE_URL"].
+ Fixes #6951. [Backport #8074]
+
+ *kennyj*
+
* Do not create useless database transaction when building `has_one` association. [Backport #8154]
Example:
diff --git a/activerecord/lib/active_record/query_cache.rb b/activerecord/lib/active_record/query_cache.rb
index 466d148901..2156889a0f 100644
--- a/activerecord/lib/active_record/query_cache.rb
+++ b/activerecord/lib/active_record/query_cache.rb
@@ -6,19 +6,19 @@ module ActiveRecord
module ClassMethods
# Enable the query cache within the block if Active Record is configured.
def cache(&block)
- if ActiveRecord::Base.configurations.blank?
- yield
- else
+ if ActiveRecord::Base.connected?
connection.cache(&block)
+ else
+ yield
end
end
# Disable the query cache within the block if Active Record is configured.
def uncached(&block)
- if ActiveRecord::Base.configurations.blank?
- yield
- else
+ if ActiveRecord::Base.connected?
connection.uncached(&block)
+ else
+ yield
end
end
end
diff --git a/activerecord/test/cases/query_cache_test.rb b/activerecord/test/cases/query_cache_test.rb
index dd881f8230..dfc6ea2457 100644
--- a/activerecord/test/cases/query_cache_test.rb
+++ b/activerecord/test/cases/query_cache_test.rb
@@ -173,6 +173,17 @@ class QueryCacheTest < ActiveRecord::TestCase
assert_queries(2) { task.lock!; task.lock! }
end
end
+
+ def test_cache_is_available_when_connection_is_connected
+ conf = ActiveRecord::Base.configurations
+
+ ActiveRecord::Base.configurations = {}
+ Task.cache do
+ assert_queries(1) { Task.find(1); Task.find(1) }
+ end
+ ensure
+ ActiveRecord::Base.configurations = conf
+ end
end
class QueryCacheExpiryTest < ActiveRecord::TestCase