moves some auto explain logic to the subscriber [José Valim & Xavier Noria]

This commit is contained in:
Xavier Noria 2011-12-04 13:53:26 -08:00
parent 7f3ce35e19
commit 4e74bd194b
2 changed files with 12 additions and 19 deletions

View File

@ -28,16 +28,6 @@ module ActiveRecord
end
end
# This method receives payloads from the explain subscriber and is
# responsible for collecting or ignoring them.
def collect_queries_for_explain(payload) # :nodoc:
if queries = Thread.current[:available_queries_for_explain]
unless ignore_payload_for_explain?(payload)
queries << payload.values_at(:sql, :binds)
end
end
end
# Relation#explain needs to be able to collect the queries regardless of
# whether auto explain is enabled. This method serves that purpose.
def collecting_queries_for_explain # :nodoc:
@ -49,13 +39,6 @@ module ActiveRecord
current[:available_queries_for_explain] = original
end
# SCHEMA queries cannot be EXPLAINed, also we do not want to run EXPLAIN on
# our own EXPLAINs now matter how loopingly beautiful that would be.
SKIP_EXPLAIN_FOR = %w(SCHEMA EXPLAIN)
def ignore_payload_for_explain?(payload) # :nodoc:
payload[:exception] || SKIP_EXPLAIN_FOR.include?(payload[:name])
end
# Makes the adapter execute EXPLAIN for the tuples of queries and bindings.
# Returns a formatted string ready to be logged.
def exec_explain(queries) # :nodoc:

View File

@ -1,9 +1,19 @@
require 'active_support/notifications'
module ActiveRecord
class ExplainSubscriber
class ExplainSubscriber # :nodoc:
def call(*args)
ActiveRecord::Base.collect_queries_for_explain(args.last)
if queries = Thread.current[:available_queries_for_explain]
payload = args.last
queries << payload.values_at(:sql, :binds) unless ignore_payload?(payload)
end
end
# SCHEMA queries cannot be EXPLAINed, also we do not want to run EXPLAIN on
# our own EXPLAINs now matter how loopingly beautiful that would be.
IGNORED_PAYLOADS = %w(SCHEMA EXPLAIN)
def ignore_payload?(payload)
payload[:exception] || IGNORED_PAYLOADS.include?(payload[:name])
end
ActiveSupport::Notifications.subscribe("sql.active_record", new)