mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
explain listener does not care about time, so use evented listener
This commit is contained in:
parent
b9f9951d5f
commit
8edd6deae8
2 changed files with 10 additions and 7 deletions
|
@ -2,9 +2,12 @@ require 'active_support/notifications'
|
||||||
|
|
||||||
module ActiveRecord
|
module ActiveRecord
|
||||||
class ExplainSubscriber # :nodoc:
|
class ExplainSubscriber # :nodoc:
|
||||||
def call(*args)
|
def start(name, id, payload)
|
||||||
|
# unused
|
||||||
|
end
|
||||||
|
|
||||||
|
def finish(name, id, payload)
|
||||||
if queries = Thread.current[:available_queries_for_explain]
|
if queries = Thread.current[:available_queries_for_explain]
|
||||||
payload = args.last
|
|
||||||
queries << payload.values_at(:sql, :binds) unless ignore_payload?(payload)
|
queries << payload.values_at(:sql, :binds) unless ignore_payload?(payload)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,14 +6,14 @@ if ActiveRecord::Base.connection.supports_explain?
|
||||||
|
|
||||||
def test_collects_nothing_if_available_queries_for_explain_is_nil
|
def test_collects_nothing_if_available_queries_for_explain_is_nil
|
||||||
with_queries(nil) do
|
with_queries(nil) do
|
||||||
SUBSCRIBER.call
|
SUBSCRIBER.finish(nil, nil, {})
|
||||||
assert_nil Thread.current[:available_queries_for_explain]
|
assert_nil Thread.current[:available_queries_for_explain]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_collects_nothing_if_the_payload_has_an_exception
|
def test_collects_nothing_if_the_payload_has_an_exception
|
||||||
with_queries([]) do |queries|
|
with_queries([]) do |queries|
|
||||||
SUBSCRIBER.call(:exception => Exception.new)
|
SUBSCRIBER.finish(nil, nil, :exception => Exception.new)
|
||||||
assert queries.empty?
|
assert queries.empty?
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -21,7 +21,7 @@ if ActiveRecord::Base.connection.supports_explain?
|
||||||
def test_collects_nothing_for_ignored_payloads
|
def test_collects_nothing_for_ignored_payloads
|
||||||
with_queries([]) do |queries|
|
with_queries([]) do |queries|
|
||||||
ActiveRecord::ExplainSubscriber::IGNORED_PAYLOADS.each do |ip|
|
ActiveRecord::ExplainSubscriber::IGNORED_PAYLOADS.each do |ip|
|
||||||
SUBSCRIBER.call(:name => ip)
|
SUBSCRIBER.finish(nil, nil, :name => ip)
|
||||||
end
|
end
|
||||||
assert queries.empty?
|
assert queries.empty?
|
||||||
end
|
end
|
||||||
|
@ -31,7 +31,7 @@ if ActiveRecord::Base.connection.supports_explain?
|
||||||
sql = 'select 1 from users'
|
sql = 'select 1 from users'
|
||||||
binds = [1, 2]
|
binds = [1, 2]
|
||||||
with_queries([]) do |queries|
|
with_queries([]) do |queries|
|
||||||
SUBSCRIBER.call(:name => 'SQL', :sql => sql, :binds => binds)
|
SUBSCRIBER.finish(nil, nil, :name => 'SQL', :sql => sql, :binds => binds)
|
||||||
assert_equal 1, queries.size
|
assert_equal 1, queries.size
|
||||||
assert_equal sql, queries[0][0]
|
assert_equal sql, queries[0][0]
|
||||||
assert_equal binds, queries[0][1]
|
assert_equal binds, queries[0][1]
|
||||||
|
@ -45,4 +45,4 @@ if ActiveRecord::Base.connection.supports_explain?
|
||||||
Thread.current[:available_queries_for_explain] = nil
|
Thread.current[:available_queries_for_explain] = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue