Skip to content
This repository
Browse code

disable automatic explain if there is no logger [closes #4671]

  • Loading branch information...
commit f251437415f7b803f7a84118fcc970a111ace266 1 parent d11347d
Xavier Noria authored January 26, 2012
2  activerecord/lib/active_record/explain.rb
@@ -19,6 +19,8 @@ def self.extended(base)
19 19
     # currently collected. A false value indicates collecting is turned
20 20
     # off. Otherwise it is an array of queries.
21 21
     def logging_query_plan # :nodoc:
  22
+      return yield unless logger
  23
+
22 24
       threshold = auto_explain_threshold_in_seconds
23 25
       current   = Thread.current
24 26
       if threshold && current[:available_queries_for_explain].nil?
16  activerecord/test/cases/explain_test.rb
@@ -14,7 +14,7 @@ def connection
14 14
       base.connection
15 15
     end
16 16
 
17  
-    def test_logging_query_plan
  17
+    def test_logging_query_plan_with_logger
18 18
       base.logger.expects(:warn).with do |message|
19 19
         message.starts_with?('EXPLAIN for:')
20 20
       end
@@ -24,6 +24,20 @@ def test_logging_query_plan
24 24
       end
25 25
     end
26 26
 
  27
+    def test_logging_query_plan_without_logger
  28
+      original = base.logger
  29
+      base.logger = nil
  30
+
  31
+      base.logger.expects(:warn).never
  32
+
  33
+      with_threshold(0) do
  34
+        car = Car.where(:name => 'honda').first
  35
+        assert_equal 'honda', car.name
  36
+      end
  37
+    ensure
  38
+      base.logger = original
  39
+    end
  40
+
27 41
     def test_collect_queries_for_explain
28 42
       base.auto_explain_threshold_in_seconds = nil
29 43
       queries = Thread.current[:available_queries_for_explain] = []
3  railties/guides/source/active_record_querying.textile
Source Rendered
@@ -1400,6 +1400,9 @@ A threshold of +nil+ disables automatic EXPLAINs.
1400 1400
 The default threshold in development mode is 0.5 seconds, and +nil+ in test and
1401 1401
 production modes.
1402 1402
 
  1403
+INFO. Automatic EXPLAIN gets disabled if Active Record has no logger, regardless
  1404
+of the value of the threshold.
  1405
+
1403 1406
 h5. Disabling Automatic EXPLAIN
1404 1407
 
1405 1408
 Automatic EXPLAIN can be selectively silenced with +ActiveRecord::Base.silence_auto_explain+:

0 notes on commit f251437

Please sign in to comment.
Something went wrong with that request. Please try again.