Skip to content

Commit

Permalink
Merge pull request #29609 from tsukasaoishi/query_cache_from_beginning
Browse files Browse the repository at this point in the history
Enable query cache if set a configurations
  • Loading branch information
rafaelfranca authored and kamipo committed Oct 29, 2017
1 parent cf8a02a commit 9f2532b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
5 changes: 5 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
* Query cache was unavailable when entering the `ActiveRecord::Base.cache` block
without being connected.

*Tsukasa Oishi*

* Fix `bin/rails db:setup` and `bin/rails db:test:prepare` create wrong
ar_internal_metadata's data for a test database.

Expand Down
12 changes: 6 additions & 6 deletions activerecord/lib/active_record/query_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@ 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 connected?
connection.cache(&block)
else
if configurations.empty?
yield
else
connection.cache(&block)
end
end

# 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 connected?
connection.uncached(&block)
else
if configurations.empty?
yield
else
connection.uncached(&block)
end
end
end
Expand Down
16 changes: 2 additions & 14 deletions activerecord/test/cases/query_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,7 @@ def test_cache_is_ignored_for_locked_relations
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

def test_cache_is_not_available_when_using_a_not_connected_connection
def test_cache_is_available_when_using_a_not_connected_connection
with_temporary_connection_pool do
spec_name = Task.connection_specification_name
resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(ActiveRecord::Base.configurations)
Expand All @@ -265,8 +254,7 @@ def test_cache_is_not_available_when_using_a_not_connected_connection
end
ActiveRecord::FixtureSet.create_fixtures(self.class.fixture_path, ["tasks"], {}, ActiveRecord::Base)
end
Task.connection # warmup postgresql connection setup queries
assert_queries(2) { Task.find(1); Task.find(1) }
assert_queries(1) { Task.find(1); Task.find(1) }
ensure
ActiveRecord::Base.connection_handler.remove_connection(Task.connection_specification_name)
Task.connection_specification_name = spec_name
Expand Down

0 comments on commit 9f2532b

Please sign in to comment.