aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRafael Mendonça França <rafaelmfranca@gmail.com>2012-07-19 08:06:15 -0700
committerRafael Mendonça França <rafaelmfranca@gmail.com>2012-07-19 08:06:15 -0700
commitfa3c84fabee01847c3468cd1e8e32003693bc629 (patch)
tree5c827a23cb8ddfd17976c10de26bb58a7623ddda
parent2897bf1fd774e4980202ac3fb76ff0036fb0ae4c (diff)
parent1b2528394385add26d5cb312891551d3c2914fe2 (diff)
downloadrails-fa3c84fabee01847c3468cd1e8e32003693bc629.tar.gz
rails-fa3c84fabee01847c3468cd1e8e32003693bc629.tar.bz2
rails-fa3c84fabee01847c3468cd1e8e32003693bc629.zip
Merge pull request #7078 from kennyj/logging_query_plan
Log query plan when we use count_by_sql method.
-rw-r--r--activerecord/lib/active_record/querying.rb6
-rw-r--r--activerecord/test/cases/explain_test.rb10
2 files changed, 14 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/querying.rb b/activerecord/lib/active_record/querying.rb
index 4d8283bcff..609d810654 100644
--- a/activerecord/lib/active_record/querying.rb
+++ b/activerecord/lib/active_record/querying.rb
@@ -62,8 +62,10 @@ module ActiveRecord
#
# Product.count_by_sql "SELECT COUNT(*) FROM sales s, customers c WHERE s.customer_id = c.id"
def count_by_sql(sql)
- sql = sanitize_conditions(sql)
- connection.select_value(sql, "#{name} Count").to_i
+ logging_query_plan do
+ sql = sanitize_conditions(sql)
+ connection.select_value(sql, "#{name} Count").to_i
+ end
end
end
end
diff --git a/activerecord/test/cases/explain_test.rb b/activerecord/test/cases/explain_test.rb
index cb7781f8e7..bcc488f7ee 100644
--- a/activerecord/test/cases/explain_test.rb
+++ b/activerecord/test/cases/explain_test.rb
@@ -68,6 +68,16 @@ if ActiveRecord::Base.connection.supports_explain?
assert_equal [cars(:honda)], result
end
+ def test_logging_query_plan_when_counting_by_sql
+ base.logger.expects(:warn).with do |message|
+ message.starts_with?('EXPLAIN for:')
+ end
+
+ with_threshold(0) do
+ Car.count_by_sql "SELECT COUNT(*) FROM cars WHERE name = 'honda'"
+ end
+ end
+
def test_exec_explain_with_no_binds
sqls = %w(foo bar)
binds = [[], []]