Skip to content

Commit

Permalink
Merge pull request #29623 from kamipo/should_use_same_connection_in_q…
Browse files Browse the repository at this point in the history
…uery_cache

Should use the same connection in using query cache
  • Loading branch information
rafaelfranca authored and kamipo committed Oct 29, 2017
1 parent 9f2532b commit 9c446b0
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,14 @@ def select_all(arel, name = nil, binds = [], preparable: nil)
def cache_sql(sql, binds)
result =
if @query_cache[sql].key?(binds)
ActiveSupport::Notifications.instrument("sql.active_record",
:sql => sql, :binds => binds, :name => "CACHE", :connection_id => object_id)
ActiveSupport::Notifications.instrument(
"sql.active_record",
sql: sql,
binds: binds,
type_casted_binds: -> { type_casted_binds(binds) },
name: "CACHE",
connection_id: object_id,
)
@query_cache[sql][binds]
else
@query_cache[sql][binds] = yield
Expand Down
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def sql(event)
binds = nil

unless (payload[:binds] || []).empty?
casted_params = type_casted_binds(payload[:binds], payload[:type_casted_binds])
casted_params = type_casted_binds(payload[:type_casted_binds])
binds = " " + payload[:binds].zip(casted_params).map { |attr, value|
render_bind(attr, value)
}.inspect
Expand All @@ -47,8 +47,8 @@ def sql(event)

private

def type_casted_binds(binds, casted_binds)
casted_binds || ActiveRecord::Base.connection.type_casted_binds(binds)
def type_casted_binds(casted_binds)
casted_binds.respond_to?(:call) ? casted_binds.call : casted_binds
end

def render_bind(attr, value)
Expand Down
8 changes: 1 addition & 7 deletions activerecord/test/cases/query_cache_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ def test_cache_is_ignored_for_locked_relations
end

def test_cache_is_available_when_using_a_not_connected_connection
skip "In-Memory DB can't test for using a not connected connection" if in_memory_db?
with_temporary_connection_pool do
spec_name = Task.connection_specification_name
resolver = ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.new(ActiveRecord::Base.configurations)
Expand All @@ -247,13 +248,6 @@ def test_cache_is_available_when_using_a_not_connected_connection

Task.cache do
begin
if in_memory_db?
Task.connection.create_table :tasks do |t|
t.datetime :starting
t.datetime :ending
end
ActiveRecord::FixtureSet.create_fixtures(self.class.fixture_path, ["tasks"], {}, ActiveRecord::Base)
end
assert_queries(1) { Task.find(1); Task.find(1) }
ensure
ActiveRecord::Base.connection_handler.remove_connection(Task.connection_specification_name)
Expand Down

0 comments on commit 9c446b0

Please sign in to comment.