Skip to content

Commit 146ae32

Browse files
committed
Freeze modifications to ActiveRecord::QueryLogs
Tags and taggings build a cache when they are assigned, which means we cannot support them being mutated. This freezes tags and taggings to ensure they aren't changed after assignment. This may not cover all cases, and per the previous PR we intend this to be configured mostly via application.config, but it covers a case we were making in our test suite and so should hopefully cover a mistake users are somewhat likely to make.
1 parent 8551d3d commit 146ae32

File tree

5 files changed

+13
-11
lines changed

5 files changed

+13
-11
lines changed

actionpack/lib/action_controller/railtie.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class Railtie < Rails::Railtie # :nodoc:
123123
app.config.active_record.query_log_tags |= [:action]
124124

125125
ActiveSupport.on_load(:active_record) do
126-
ActiveRecord::QueryLogs.taggings.merge!(
126+
ActiveRecord::QueryLogs.taggings = ActiveRecord::QueryLogs.taggings.merge(
127127
controller: ->(context) { context[:controller]&.controller_name },
128128
action: ->(context) { context[:controller]&.action_name },
129129
namespaced_controller: ->(context) {

activejob/lib/active_job/railtie.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ class Railtie < Rails::Railtie # :nodoc:
9393
app.config.active_record.query_log_tags |= [:job]
9494

9595
ActiveSupport.on_load(:active_record) do
96-
ActiveRecord::QueryLogs.taggings[:job] = ->(context) { context[:job].class.name if context[:job] }
96+
ActiveRecord::QueryLogs.taggings = ActiveRecord::QueryLogs.taggings.merge(
97+
job: ->(context) { context[:job].class.name if context[:job] }
98+
)
9799
end
98100
end
99101
end

activerecord/lib/active_record/query_logs.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ def call(_context)
102102
end
103103
end
104104

105-
@taggings = {}
106-
@tags = [ :application ]
105+
@taggings = {}.freeze
106+
@tags = [ :application ].freeze
107107
@prepend_comment = false
108108
@cache_query_log_tags = false
109109
@tags_formatter = false
@@ -115,17 +115,16 @@ class << self
115115
attr_accessor :prepend_comment, :cache_query_log_tags # :nodoc:
116116

117117
def taggings=(taggings) # :nodoc:
118-
@taggings = taggings
118+
@taggings = taggings.freeze
119119
@handlers = rebuild_handlers
120120
end
121121

122122
def tags=(tags) # :nodoc:
123-
@tags = tags
123+
@tags = tags.freeze
124124
@handlers = rebuild_handlers
125125
end
126126

127127
def tags_formatter=(format) # :nodoc:
128-
@tags_formatter = format
129128
@formatter = case format
130129
when :legacy
131130
LegacyFormatter
@@ -134,6 +133,7 @@ def tags_formatter=(format) # :nodoc:
134133
else
135134
raise ArgumentError, "Formatter is unsupported: #{format}"
136135
end
136+
@tags_formatter = format
137137
end
138138

139139
def call(sql, connection) # :nodoc:

activerecord/lib/active_record/railtie.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ class Railtie < Rails::Railtie # :nodoc:
385385
config.after_initialize do
386386
if app.config.active_record.query_log_tags_enabled
387387
ActiveRecord.query_transformers << ActiveRecord::QueryLogs
388-
ActiveRecord::QueryLogs.taggings.merge!(
388+
ActiveRecord::QueryLogs.taggings = ActiveRecord::QueryLogs.taggings.merge(
389389
application: Rails.application.class.name.split("::").first,
390390
pid: -> { Process.pid.to_s },
391391
socket: ->(context) { context[:connection].pool.db_config.socket },

activerecord/test/cases/query_logs_test.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ def setup
2020
ActiveRecord::QueryLogs.prepend_comment = false
2121
ActiveRecord::QueryLogs.cache_query_log_tags = false
2222
ActiveRecord::QueryLogs.cached_comment = nil
23-
ActiveRecord::QueryLogs.taggings[:application] = -> {
24-
"active_record"
23+
ActiveRecord::QueryLogs.taggings = {
24+
application: -> { "active_record" }
2525
}
2626
end
2727

@@ -185,7 +185,7 @@ def test_empty_comments_are_not_added
185185

186186
def test_sql_commenter_format
187187
ActiveRecord::QueryLogs.tags_formatter = :sqlcommenter
188-
ActiveRecord::QueryLogs.tags = [:application, {}]
188+
ActiveRecord::QueryLogs.tags = [:application]
189189

190190
assert_queries_match(%r{/\*application='active_record'\*/}) do
191191
Dashboard.first

0 commit comments

Comments
 (0)