Skip to content

Commit a321cb4

Browse files
authored
Merge pull request rails#43544 from Shopify/active-record-query-logs-reset
Properly clear the ActiveRecord::QueryLogs context
2 parents b3df083 + 34f6bf2 commit a321cb4

File tree

4 files changed

+30
-0
lines changed

4 files changed

+30
-0
lines changed

activerecord/lib/active_record/query_logs.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ def set_context(**options)
9696
end
9797
end
9898

99+
def clear_context # :nodoc:
100+
context.clear
101+
end
102+
99103
def call(sql) # :nodoc:
100104
if prepend_comment
101105
"#{self.comment} #{sql}"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# frozen_string_literal: true
2+
3+
module ActiveRecord::QueryLogs::TestHelper # :nodoc:
4+
def before_setup
5+
ActiveRecord::QueryLogs.clear_context
6+
super
7+
end
8+
9+
def after_teardown
10+
super
11+
ActiveRecord::QueryLogs.clear_context
12+
end
13+
end

activerecord/lib/active_record/railtie.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,15 @@ class Railtie < Rails::Railtie # :nodoc:
376376
if app.config.active_record.cache_query_log_tags
377377
ActiveRecord::QueryLogs.cache_query_log_tags = true
378378
end
379+
380+
app.reloader.before_class_unload { ActiveRecord::QueryLogs.clear_context }
381+
app.executor.to_run { ActiveRecord::QueryLogs.clear_context }
382+
app.executor.to_complete { ActiveRecord::QueryLogs.clear_context }
383+
384+
ActiveSupport.on_load(:active_support_test_case) do
385+
require "active_record/query_logs/test_helper"
386+
include ActiveRecord::QueryLogs::TestHelper
387+
end
379388
end
380389
end
381390
end

activerecord/test/cases/query_logs_test.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
require "cases/helper"
44
require "models/dashboard"
5+
require "active_record/query_logs/test_helper"
56

67
class QueryLogsTest < ActiveRecord::TestCase
8+
# Automatically included in Rails apps via railtie but that don't run here.
9+
include ActiveRecord::QueryLogs::TestHelper
10+
711
fixtures :dashboards
812

913
ActiveRecord::QueryLogs.taggings[:application] = -> {

0 commit comments

Comments
 (0)