Skip to content

Commit 0591b7a

Browse files
authored
Merge pull request rails#43541 from keeran/keeran/remove-with-tag
Remove with_tag from QueryLogs
2 parents 6e83d06 + 9c6b343 commit 0591b7a

File tree

2 files changed

+7
-94
lines changed

2 files changed

+7
-94
lines changed

activerecord/lib/active_record/query_logs.rb

Lines changed: 3 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -110,31 +110,15 @@ def set_context(**options)
110110
update_context(**previous_context)
111111
end
112112

113-
# Temporarily tag any query executed within `&block`. Can be nested.
114-
def with_tag(tag, &block)
115-
inline_tags.push(tag)
116-
yield if block_given?
117-
ensure
118-
inline_tags.pop
119-
end
120-
121113
def call(sql) # :nodoc:
122-
parts = self.comments
123114
if prepend_comment
124-
parts << sql
115+
"#{self.comment} #{sql}"
125116
else
126-
parts.unshift(sql)
127-
end
128-
parts.join(" ")
117+
"#{sql} #{self.comment}"
118+
end.strip
129119
end
130120

131121
private
132-
# Returns an array of comments which need to be added to the query, comprised
133-
# of configured and inline tags.
134-
def comments
135-
[ comment, inline_comment ].compact
136-
end
137-
138122
# Returns an SQL comment +String+ containing the query log tags.
139123
# Sets and returns a cached comment if <tt>cache_query_log_tags</tt> is +true+.
140124
def comment
@@ -152,21 +136,6 @@ def uncached_comment
152136
end
153137
end
154138

155-
# Returns a +String+ containing any inline comments from +with_tag+.
156-
def inline_comment
157-
return nil unless inline_tags.present?
158-
"/*#{escape_sql_comment(inline_tag_content)}*/"
159-
end
160-
161-
# Return the set of active inline tags from +with_tag+.
162-
def inline_tags
163-
if context[:inline_tags].nil?
164-
context[:inline_tags] = []
165-
else
166-
context[:inline_tags]
167-
end
168-
end
169-
170139
def context
171140
Thread.current[:active_record_query_log_tags_context] ||= Hash.new { NullObject.new }
172141
end
@@ -194,10 +163,6 @@ def tag_content
194163
"#{key}:#{val}" unless val.nil?
195164
end.join(",")
196165
end
197-
198-
def inline_tag_content
199-
inline_tags.join
200-
end
201166
end
202167
end
203168
end

activerecord/test/cases/query_logs_test.rb

Lines changed: 4 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ def test_retrieves_comment_from_cache_when_enabled_and_set
100100
ActiveRecord::QueryLogs.cache_query_log_tags = true
101101
ActiveRecord::QueryLogs.tags = [ :application ]
102102

103-
assert_equal " /*application:active_record*/", ActiveRecord::QueryLogs.call("")
103+
assert_equal "/*application:active_record*/", ActiveRecord::QueryLogs.call("")
104104

105105
ActiveRecord::QueryLogs.stub(:cached_comment, "/*cached_comment*/") do
106-
assert_equal " /*cached_comment*/", ActiveRecord::QueryLogs.call("")
106+
assert_equal "/*cached_comment*/", ActiveRecord::QueryLogs.call("")
107107
end
108108
ensure
109109
ActiveRecord::QueryLogs.cached_comment = nil
@@ -115,12 +115,12 @@ def test_resets_cache_on_context_update
115115
ActiveRecord::QueryLogs.update_context(temporary: "value")
116116
ActiveRecord::QueryLogs.tags = [ temporary_tag: ->(context) { context[:temporary] } ]
117117

118-
assert_equal " /*temporary_tag:value*/", ActiveRecord::QueryLogs.call("")
118+
assert_equal "/*temporary_tag:value*/", ActiveRecord::QueryLogs.call("")
119119

120120
ActiveRecord::QueryLogs.update_context(temporary: "new_value")
121121

122122
assert_nil ActiveRecord::QueryLogs.cached_comment
123-
assert_equal " /*temporary_tag:new_value*/", ActiveRecord::QueryLogs.call("")
123+
assert_equal "/*temporary_tag:new_value*/", ActiveRecord::QueryLogs.call("")
124124
ensure
125125
ActiveRecord::QueryLogs.cached_comment = nil
126126
ActiveRecord::QueryLogs.cache_query_log_tags = false
@@ -149,58 +149,6 @@ def test_default_tag_behavior
149149
end
150150
end
151151

152-
def test_inline_tags_only_affect_block
153-
# disable regular comment tags
154-
ActiveRecord::QueryLogs.tags = []
155-
156-
# confirm single inline tag
157-
assert_sql(%r{/\*foo\*/$}) do
158-
ActiveRecord::QueryLogs.with_tag("foo") do
159-
Dashboard.first
160-
end
161-
end
162-
163-
# confirm different inline tag
164-
assert_sql(%r{/\*bar\*/$}) do
165-
ActiveRecord::QueryLogs.with_tag("bar") do
166-
Dashboard.first
167-
end
168-
end
169-
170-
# confirm no tags are persisted
171-
ActiveRecord::QueryLogs.tags = [ :application ]
172-
173-
assert_sql(%r{/\*application:active_record\*/$}) do
174-
Dashboard.first
175-
end
176-
ensure
177-
ActiveRecord::QueryLogs.tags = [ :application ]
178-
end
179-
180-
def test_nested_inline_tags
181-
assert_sql(%r{/\*foobar\*/$}) do
182-
ActiveRecord::QueryLogs.with_tag("foo") do
183-
ActiveRecord::QueryLogs.with_tag("bar") do
184-
Dashboard.first
185-
end
186-
end
187-
end
188-
end
189-
190-
def test_bad_inline_tags
191-
assert_sql(%r{/\*; DROP TABLE USERS;\*/$}) do
192-
ActiveRecord::QueryLogs.with_tag("*/; DROP TABLE USERS;/*") do
193-
Dashboard.first
194-
end
195-
end
196-
197-
assert_sql(%r{/\*; DROP TABLE USERS;\*/$}) do
198-
ActiveRecord::QueryLogs.with_tag("**//; DROP TABLE USERS;//**") do
199-
Dashboard.first
200-
end
201-
end
202-
end
203-
204152
def test_empty_comments_are_not_added
205153
original_tags = ActiveRecord::QueryLogs.tags
206154
ActiveRecord::QueryLogs.tags = [ empty: -> { nil } ]

0 commit comments

Comments
 (0)