Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

improve readability of AR explain result #6584

Merged
merged 1 commit into from

2 participants

@amatsuda
Collaborator

AR explain returns nicely formatted String, but it looks miserable on our Rails console.

> User.where(id: 2).explain
  User Load (0.1ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2
  EXPLAIN (0.1ms)  EXPLAIN QUERY PLAN SELECT "users".* FROM "users" WHERE "users"."id" = 2
 => "EXPLAIN for: SELECT \"users\".* FROM \"users\"  WHERE \"users\".\"id\" = 2\n0|0|0|SEARCH TABLE users USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)\n" 

With this patch, explain returns a p-friendly String instance.

> User.where(id: 2).explain
  User Load (0.2ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 2
  EXPLAIN (0.1ms)  EXPLAIN QUERY PLAN SELECT "users".* FROM "users" WHERE "users"."id" = 2
 => EXPLAIN for: SELECT "users".* FROM "users"  WHERE "users"."id" = 2
0|0|0|SEARCH TABLE users USING INTEGER PRIMARY KEY (rowid=?) (~1 rows)
@fxn
Owner
fxn commented

The EXPLAIN feature has the console among its natural use cases, I think this patch is good. Thanks!

@fxn fxn merged commit ec9636e into from
@amatsuda
Collaborator

@fxn Yes, that's what I meant. Thank you! :green_heart:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jun 1, 2012
  1. @amatsuda
This page is out of date. Refresh to see the latest.
Showing with 7 additions and 1 deletion.
  1. +7 −1 activerecord/lib/active_record/explain.rb
View
8 activerecord/lib/active_record/explain.rb
@@ -52,7 +52,7 @@ def collecting_queries_for_explain # :nodoc:
# 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:
- queries && queries.map do |sql, bind|
+ str = queries && queries.map do |sql, bind|
[].tap do |msg|
msg << "EXPLAIN for: #{sql}"
unless bind.empty?
@@ -62,6 +62,12 @@ def exec_explain(queries) # :nodoc:
msg << connection.explain(sql, bind)
end.join("\n")
end.join("\n")
+
+ # overriding inspect to be more human readable
+ def str.inspect
+ self
+ end
+ str
end
# Silences automatic EXPLAIN logging for the duration of the block.
Something went wrong with that request. Please try again.