Skip to content

Commit eb9ab54

Browse files
authored
Merge pull request rails#54707 from fatkodima/query_log_tags_prepend_comment
Expose ability to prepend query log tags via application configuration
2 parents b4f4615 + 1600a0c commit eb9ab54

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

activerecord/lib/active_record/query_logs.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ module ActiveRecord
6565
#
6666
# Tag comments can be prepended to the query:
6767
#
68-
# ActiveRecord::QueryLogs.prepend_comment = true
68+
# config.active_record.query_log_tags_prepend_comment = true
6969
#
7070
# For applications where the content will not change during the lifetime of
7171
# the request or job execution, the tags can be cached for reuse in every query:

activerecord/lib/active_record/railtie.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class Railtie < Rails::Railtie # :nodoc:
3535
config.active_record.query_log_tags = [ :application ]
3636
config.active_record.query_log_tags_format = :legacy
3737
config.active_record.cache_query_log_tags = false
38+
config.active_record.query_log_tags_prepend_comment = false
3839
config.active_record.raise_on_assign_to_attr_readonly = false
3940
config.active_record.belongs_to_required_validates_foreign_key = true
4041
config.active_record.generate_secure_token_on = :create
@@ -230,6 +231,7 @@ class Railtie < Rails::Railtie # :nodoc:
230231
:query_log_tags,
231232
:query_log_tags_format,
232233
:cache_query_log_tags,
234+
:query_log_tags_prepend_comment,
233235
:sqlite3_adapter_strict_strings_by_default,
234236
:check_schema_cache_dump_version,
235237
:use_schema_cache_dump,
@@ -398,6 +400,10 @@ class Railtie < Rails::Railtie # :nodoc:
398400
if app.config.active_record.cache_query_log_tags
399401
ActiveRecord::QueryLogs.cache_query_log_tags = true
400402
end
403+
404+
if app.config.active_record.query_log_tags_prepend_comment
405+
ActiveRecord::QueryLogs.prepend_comment = true
406+
end
401407
end
402408
end
403409
end

guides/source/configuring.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,6 +1540,17 @@ that have a large number of queries, caching query log tags can provide a
15401540
performance benefit when the context does not change during the lifetime of the
15411541
request or job execution. Defaults to `false`.
15421542
1543+
#### `config.active_record.query_log_tags_prepend_comment`
1544+
1545+
Specifies whether or not to prepend query log tags comment to the query.
1546+
1547+
By default comments are appended at the end of the query. Certain databases, such as MySQL will
1548+
truncate the query text. This is the case for slow query logs and the results of querying
1549+
some InnoDB internal tables where the length of the query is more than 1024 bytes.
1550+
In order to not lose the log tags comments from the queries, you can prepend the comments using this option.
1551+
1552+
Defaults to `false`.
1553+
15431554
#### `config.active_record.schema_cache_ignored_tables`
15441555
15451556
Define the list of table that should be ignored when generating the schema

railties/test/application/query_logs_test.rb

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class User < ActiveRecord::Base
1919
app_file "app/controllers/users_controller.rb", <<-RUBY
2020
class UsersController < ApplicationController
2121
def index
22-
render inline: ActiveRecord::QueryLogs.call("", Pet.connection)
22+
render inline: ActiveRecord::QueryLogs.call("SELECT 1", Pet.connection)
2323
end
2424
2525
def dynamic_content
@@ -146,7 +146,7 @@ def app
146146
get "/", {}, { "HTTPS" => "on" }
147147
comment = last_response.body.strip
148148

149-
assert_equal("/*action='index',controller='users',database='storage%2Fproduction_animals.sqlite3'*/", comment)
149+
assert_equal("SELECT 1 /*action='index',controller='users',database='storage%2Fproduction_animals.sqlite3'*/", comment)
150150
end
151151

152152
test "source_location information is added if enabled" do
@@ -166,6 +166,19 @@ def app
166166
assert_match(/source_location='.*\d+'/, comment)
167167
end
168168

169+
test "prepending tags comment" do
170+
add_to_config "config.active_record.query_log_tags_enabled = true"
171+
add_to_config "config.active_record.query_log_tags = [ :action, :controller ]"
172+
add_to_config "config.active_record.query_log_tags_prepend_comment = true"
173+
174+
boot_app
175+
176+
get "/", {}, { "HTTPS" => "on" }
177+
comment = last_response.body.strip
178+
179+
assert_match(/\A\/\*action='index',controller='users'\*\/ SELECT 1/, comment)
180+
end
181+
169182
test "controller tags are not doubled up if already configured" do
170183
add_to_config "config.active_record.query_log_tags_enabled = true"
171184
add_to_config "config.active_record.query_log_tags = [ :action, :job, :controller, :pid ]"
@@ -261,7 +274,7 @@ def app
261274

262275
get "/", {}, { "HTTPS" => "on" }
263276
comment = last_response.body.strip
264-
assert_equal %(/*action='index',controller='users',namespaced_controller='users'*/), comment
277+
assert_equal %(SELECT 1 /*action='index',controller='users',namespaced_controller='users'*/), comment
265278

266279
get "/namespaced/users", {}, { "HTTPS" => "on" }
267280
comment = last_response.body.strip

0 commit comments

Comments
 (0)