Created
August 13, 2015 09:04
-
-
Save senny/dd6cfb12bee084f11bbb to your computer and use it in GitHub Desktop.
Spike to solve https://github.com/rails/rails/issues/21216
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/activerecord/lib/active_record/explain.rb b/activerecord/lib/active_record/explain.rb | |
index 727a9be..1d9dde0 100644 | |
--- a/activerecord/lib/active_record/explain.rb | |
+++ b/activerecord/lib/active_record/explain.rb | |
@@ -16,14 +16,14 @@ module ActiveRecord | |
# 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: | |
- str = queries.map do |sql, bind| | |
+ str = queries.map do |sql, bind, connection_id| | |
[].tap do |msg| | |
msg << "EXPLAIN for: #{sql}" | |
unless bind.empty? | |
bind_msg = bind.map {|col, val| [col.name, val]}.inspect | |
msg.last << " #{bind_msg}" | |
end | |
- msg << connection.explain(sql, bind) | |
+ msg << get_connection_for_explain(connection_id).explain(sql, bind) | |
end.join("\n") | |
end.join("\n") | |
@@ -34,5 +34,18 @@ module ActiveRecord | |
str | |
end | |
+ | |
+ private | |
+ def get_connection_for_explain(connection_id) | |
+ explain_connection = nil | |
+ ::ActiveRecord::Base.connection_handler.connection_pool_list.each do |handler| | |
+ explain_connection = handler.connections.detect do |conn| | |
+ conn.object_id == connection_id | |
+ end | |
+ | |
+ break if explain_connection | |
+ end | |
+ explain_connection || connection | |
+ end | |
end | |
end | |
diff --git a/activerecord/lib/active_record/explain_subscriber.rb b/activerecord/lib/active_record/explain_subscriber.rb | |
index 9adabd7..8a31fc4 100644 | |
--- a/activerecord/lib/active_record/explain_subscriber.rb | |
+++ b/activerecord/lib/active_record/explain_subscriber.rb | |
@@ -9,7 +9,7 @@ module ActiveRecord | |
def finish(name, id, payload) | |
if ExplainRegistry.collect? && !ignore_payload?(payload) | |
- ExplainRegistry.queries << payload.values_at(:sql, :binds) | |
+ ExplainRegistry.queries << payload.values_at(:sql, :binds, :connection_id) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment