Skip to content

Commit 9f035e2

Browse files
committed
Make ActiveRecord::QueryLogs default behavior to return the context content
This should cover the vast majority of use cases, e.g. ```ruby config.active_record.query_log_tags = [:api_client_id] ActiveRecord::QueryLogs.set_context(api_client_id: api_client.id) do # ... end ```
1 parent 9615e21 commit 9f035e2

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

activerecord/lib/active_record/query_logs.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ def tag_content
174174
key, value_input = tag
175175

176176
val = case value_input
177-
when nil then tag_value(key) if taggings.has_key? key
177+
when nil then tag_value(key)
178178
when Proc then instance_exec(&value_input)
179179
else value_input
180180
end
@@ -184,12 +184,14 @@ def tag_content
184184
end
185185

186186
def tag_value(key)
187-
value = taggings[key]
188-
189-
if value.respond_to?(:call)
190-
instance_exec(&taggings[key])
187+
if value = taggings[key]
188+
if value.respond_to?(:call)
189+
instance_exec(&taggings[key])
190+
else
191+
value
192+
end
191193
else
192-
value
194+
context[key]
193195
end
194196
end
195197

activerecord/test/cases/query_logs_test.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,18 @@ def test_ensure_context_has_symbol_keys
137137
ActiveRecord::QueryLogs.update_context(application_name: nil)
138138
end
139139

140+
def test_default_tag_behavior
141+
ActiveRecord::QueryLogs.tags = [:application, :foo]
142+
ActiveRecord::QueryLogs.set_context(foo: "bar") do
143+
assert_sql(%r{/\*application:active_record,foo:bar\*/}) do
144+
Dashboard.first
145+
end
146+
end
147+
assert_sql(%r{/\*application:active_record\*/}) do
148+
Dashboard.first
149+
end
150+
end
151+
140152
def test_inline_tags_only_affect_block
141153
# disable regular comment tags
142154
ActiveRecord::QueryLogs.tags = []

0 commit comments

Comments
 (0)