Skip to content

Commit 703d4b2

Browse files
authored
Merge pull request rails#43079 from Shopify/ar-query-logs-instance-exec
Pass `ActiveRecord::QueryLogs` as argument rather than `instance_exec`
2 parents 61e6515 + b9635cc commit 703d4b2

File tree

5 files changed

+31
-31
lines changed

5 files changed

+31
-31
lines changed

actionpack/lib/action_controller/railtie.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,9 +115,9 @@ class Railtie < Rails::Railtie # :nodoc:
115115

116116
ActiveSupport.on_load(:active_record) do
117117
ActiveRecord::QueryLogs.taggings.merge!(
118-
controller: -> { context[:controller].controller_name },
119-
action: -> { context[:controller].action_name },
120-
namespaced_controller: -> { context[:controller].class.name }
118+
controller: ->(context) { context[:controller].controller_name },
119+
action: ->(context) { context[:controller].action_name },
120+
namespaced_controller: ->(context) { context[:controller].class.name }
121121
)
122122
end
123123
end

activejob/lib/active_job/railtie.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Railtie < Rails::Railtie # :nodoc:
7070
end
7171

7272
ActiveSupport.on_load(:active_record) do
73-
ActiveRecord::QueryLogs.taggings[:job] = -> { context[:job].class.name }
73+
ActiveRecord::QueryLogs.taggings[:job] = ->(context) { context[:job].class.name }
7474
end
7575
end
7676
end

activerecord/lib/active_record/query_logs.rb

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,10 @@ module ActiveRecord
3737
#
3838
# tags = [
3939
# :application,
40-
# { custom_tag: -> { context[:controller].controller_name } }
40+
# {
41+
# custom_tag: ->(context) { context[:controller].controller_name },
42+
# custom_value: -> { Custom.value },
43+
# }
4144
# ]
4245
# ActiveRecord::QueryLogs.tags = tags
4346
#
@@ -173,30 +176,24 @@ def escape_sql_comment(content)
173176

174177
def tag_content
175178
tags.flat_map { |i| [*i] }.filter_map do |tag|
176-
key, value_input = tag
177-
178-
val = case value_input
179-
when nil then tag_value(key)
180-
when Proc then instance_exec(&value_input)
181-
else value_input
179+
key, handler = tag
180+
handler ||= taggings[key]
181+
182+
val = if handler.nil?
183+
context[key]
184+
elsif handler.respond_to?(:call)
185+
if handler.arity == 0
186+
handler.call
187+
else
188+
handler.call(context)
189+
end
190+
else
191+
handler
182192
end
183-
184193
"#{key}:#{val}" unless val.nil?
185194
end.join(",")
186195
end
187196

188-
def tag_value(key)
189-
if value = taggings[key]
190-
if value.respond_to?(:call)
191-
instance_exec(&taggings[key])
192-
else
193-
value
194-
end
195-
else
196-
context[key]
197-
end
198-
end
199-
200197
def inline_tag_content
201198
inline_tags.join
202199
end

activerecord/test/cases/query_logs_test.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_retrieves_comment_from_cache_when_enabled_and_set
113113
def test_resets_cache_on_context_update
114114
ActiveRecord::QueryLogs.cache_query_log_tags = true
115115
ActiveRecord::QueryLogs.update_context(temporary: "value")
116-
ActiveRecord::QueryLogs.tags = [ temporary_tag: -> { context[:temporary] } ]
116+
ActiveRecord::QueryLogs.tags = [ temporary_tag: ->(context) { context[:temporary] } ]
117117

118118
assert_equal " /*temporary_tag:value*/", ActiveRecord::QueryLogs.add_query_log_tags_to_sql("")
119119

@@ -127,7 +127,7 @@ def test_resets_cache_on_context_update
127127
end
128128

129129
def test_ensure_context_has_symbol_keys
130-
ActiveRecord::QueryLogs.tags = [ new_key: -> { context[:symbol_key] } ]
130+
ActiveRecord::QueryLogs.tags = [ new_key: ->(context) { context[:symbol_key] } ]
131131
ActiveRecord::QueryLogs.update_context("symbol_key" => "symbolized")
132132

133133
assert_sql(%r{/\*new_key:symbolized}) do
@@ -244,7 +244,10 @@ def test_custom_proc_tags
244244

245245
def test_multiple_custom_tags
246246
original_tags = ActiveRecord::QueryLogs.tags
247-
ActiveRecord::QueryLogs.tags = [ :application, { custom_proc: -> { "test content" }, another_proc: -> { "more test content" } } ]
247+
ActiveRecord::QueryLogs.tags = [
248+
:application,
249+
{ custom_proc: -> { "test content" }, another_proc: -> { "more test content" } },
250+
]
248251

249252
assert_sql(%r{/\*application:active_record,custom_proc:test content,another_proc:more test content\*/$}) do
250253
Dashboard.first
@@ -256,7 +259,7 @@ def test_multiple_custom_tags
256259
def test_custom_proc_context_tags
257260
original_tags = ActiveRecord::QueryLogs.tags
258261
ActiveRecord::QueryLogs.update_context(foo: "bar")
259-
ActiveRecord::QueryLogs.tags = [ :application, { custom_context_proc: -> { context[:foo] } } ]
262+
ActiveRecord::QueryLogs.tags = [ :application, { custom_context_proc: ->(context) { context[:foo] } } ]
260263

261264
assert_sql(%r{/\*application:active_record,custom_context_proc:bar\*/$}) do
262265
Dashboard.first
@@ -268,7 +271,7 @@ def test_custom_proc_context_tags
268271

269272
def test_set_context_restore_state
270273
original_tags = ActiveRecord::QueryLogs.tags
271-
ActiveRecord::QueryLogs.tags = [foo: -> { context[:foo] }]
274+
ActiveRecord::QueryLogs.tags = [foo: ->(context) { context[:foo] }]
272275
ActiveRecord::QueryLogs.set_context(foo: "bar") do
273276
assert_sql(%r{/\*foo:bar\*/$}) { Dashboard.first }
274277
ActiveRecord::QueryLogs.set_context(foo: "plop") do

railties/test/application/query_logs_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def app
123123
test "query cache is cleared between requests" do
124124
add_to_config "config.active_record.query_log_tags_enabled = true"
125125
add_to_config "config.active_record.cache_query_log_tags = true"
126-
add_to_config "config.active_record.query_log_tags = [ { dynamic: -> { context[:controller].dynamic_content } } ]"
126+
add_to_config "config.active_record.query_log_tags = [ { dynamic: ->(context) { context[:controller].dynamic_content } } ]"
127127

128128
boot_app
129129

@@ -141,7 +141,7 @@ def app
141141
test "query cache is cleared between job executions" do
142142
add_to_config "config.active_record.query_log_tags_enabled = true"
143143
add_to_config "config.active_record.cache_query_log_tags = true"
144-
add_to_config "config.active_record.query_log_tags = [ { dynamic: -> { context[:job].dynamic_content } } ]"
144+
add_to_config "config.active_record.query_log_tags = [ { dynamic: ->(context) { context[:job].dynamic_content } } ]"
145145

146146
boot_app
147147

0 commit comments

Comments
 (0)