Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix #6951. Use query cache/uncache, when using not only database.yml but also DATABASE_URL. #8074

Merged
merged 1 commit into from

4 participants

@kennyj
Collaborator

This PR closes #6951.

If we use DATABASE_URL, ActiveRecord::Base.configurations is not populated. Thus we can't use query cache with DATABASE_URL.

See https://github.com/rails/rails/blob/master/activerecord/lib/active_record/query_cache.rb#L7

@rafaelfranca

@kennyj could you add a CHANGELOG entry?

activerecord/lib/active_record/query_cache.rb
@@ -14,7 +14,7 @@ def cache(&block)
# Disable the query cache within the block if Active Record is configured.
def uncached(&block)
- if ActiveRecord::Base.configurations.blank?
+ unless ActiveRecord::Base.connected?
yield
else
connection.uncached(&block)

Perhaps we should invert the unless..else block here, wdyt?

@rafaelfranca Owner

:+1:

@steveklabnik Collaborator

Some people really really really hate unless/else, but it doesn't bother me.

Hehe.. I don't hate, but I try to avoid :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@kennyj
Collaborator

Thanks guys. I don't like unless .. else too :)
I update and added a CHANGELOG entry.

@rafaelfranca rafaelfranca merged commit 85039d4 into rails:master
@kennyj kennyj referenced this pull request from a commit in kennyj/rails
@kennyj kennyj Backport #8074 to 3-2-stable. Use query cache/uncache, when using not…
… only database.yml but also DATABASE_URL.
fda9075
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 31, 2012
  1. @kennyj
This page is out of date. Refresh to see the latest.
View
5 activerecord/CHANGELOG.md
@@ -1,5 +1,10 @@
## Rails 4.0.0 (unreleased) ##
+* Use query cache/uncache when using DATABASE_URL.
+ Fix #6951.
+
+ *kennyj*
+
* Added `#none!` method for mutating `ActiveRecord::Relation` objects to a NullRelation.
It acts like `#none` but modifies relation in place.
View
12 activerecord/lib/active_record/query_cache.rb
@@ -5,19 +5,19 @@ class QueryCache
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
View
11 activerecord/test/cases/query_cache_test.rb
@@ -184,6 +184,17 @@ def test_cache_is_ignored_for_locked_relations
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
Something went wrong with that request. Please try again.