Skip to content

Freeze modifications to ActiveRecord::QueryLogs #52410

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class Railtie < Rails::Railtie # :nodoc:
app.config.active_record.query_log_tags |= [:action]

ActiveSupport.on_load(:active_record) do
ActiveRecord::QueryLogs.taggings.merge!(
ActiveRecord::QueryLogs.taggings = ActiveRecord::QueryLogs.taggings.merge(
controller: ->(context) { context[:controller]&.controller_name },
action: ->(context) { context[:controller]&.action_name },
namespaced_controller: ->(context) {
Expand Down
4 changes: 3 additions & 1 deletion activejob/lib/active_job/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,9 @@ class Railtie < Rails::Railtie # :nodoc:
app.config.active_record.query_log_tags |= [:job]

ActiveSupport.on_load(:active_record) do
ActiveRecord::QueryLogs.taggings[:job] = ->(context) { context[:job].class.name if context[:job] }
ActiveRecord::QueryLogs.taggings = ActiveRecord::QueryLogs.taggings.merge(
job: ->(context) { context[:job].class.name if context[:job] }
)
end
end
end
Expand Down
10 changes: 5 additions & 5 deletions activerecord/lib/active_record/query_logs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ def call(_context)
end
end

@taggings = {}
@tags = [ :application ]
@taggings = {}.freeze
@tags = [ :application ].freeze
@prepend_comment = false
@cache_query_log_tags = false
@tags_formatter = false
Expand All @@ -115,17 +115,16 @@ class << self
attr_accessor :prepend_comment, :cache_query_log_tags # :nodoc:

def taggings=(taggings) # :nodoc:
@taggings = taggings
@taggings = taggings.freeze
@handlers = rebuild_handlers
end

def tags=(tags) # :nodoc:
@tags = tags
@tags = tags.freeze
@handlers = rebuild_handlers
end

def tags_formatter=(format) # :nodoc:
@tags_formatter = format
@formatter = case format
when :legacy
LegacyFormatter
Expand All @@ -134,6 +133,7 @@ def tags_formatter=(format) # :nodoc:
else
raise ArgumentError, "Formatter is unsupported: #{format}"
end
@tags_formatter = format
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather this setter be here, after @formatter, so that the exception that can be raised above prevents the assignment.

end

def call(sql, connection) # :nodoc:
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/railtie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ class Railtie < Rails::Railtie # :nodoc:
config.after_initialize do
if app.config.active_record.query_log_tags_enabled
ActiveRecord.query_transformers << ActiveRecord::QueryLogs
ActiveRecord::QueryLogs.taggings.merge!(
ActiveRecord::QueryLogs.taggings = ActiveRecord::QueryLogs.taggings.merge(
application: Rails.application.class.name.split("::").first,
pid: -> { Process.pid.to_s },
socket: ->(context) { context[:connection].pool.db_config.socket },
Expand Down
6 changes: 3 additions & 3 deletions activerecord/test/cases/query_logs_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ def setup
ActiveRecord::QueryLogs.prepend_comment = false
ActiveRecord::QueryLogs.cache_query_log_tags = false
ActiveRecord::QueryLogs.cached_comment = nil
ActiveRecord::QueryLogs.taggings[:application] = -> {
"active_record"
ActiveRecord::QueryLogs.taggings = {
application: -> { "active_record" }
}
end

Expand Down Expand Up @@ -185,7 +185,7 @@ def test_empty_comments_are_not_added

def test_sql_commenter_format
ActiveRecord::QueryLogs.tags_formatter = :sqlcommenter
ActiveRecord::QueryLogs.tags = [:application, {}]
ActiveRecord::QueryLogs.tags = [:application]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this change we could remove this line, but I think it's worth setting the value we're testing for


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