diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-07-19 08:06:15 -0700 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-07-19 08:06:15 -0700 |
commit | fa3c84fabee01847c3468cd1e8e32003693bc629 (patch) | |
tree | 5c827a23cb8ddfd17976c10de26bb58a7623ddda | |
parent | 2897bf1fd774e4980202ac3fb76ff0036fb0ae4c (diff) | |
parent | 1b2528394385add26d5cb312891551d3c2914fe2 (diff) | |
download | rails-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.rb | 6 | ||||
-rw-r--r-- | activerecord/test/cases/explain_test.rb | 10 |
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 = [[], []] |