1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
|
# frozen_string_literal: true
require "cases/helper"
require "models/dashboard"
class QueryLogsTest < ActiveRecord::TestCase
fixtures :dashboards
def setup
# ActiveSupport::ExecutionContext context is automatically reset in Rails app via an executor hooks set in railtie
# But not in Active Record's own test suite.
ActiveSupport::ExecutionContext.clear
# Enable the query tags logging
@original_transformers = ActiveRecord.query_transformers
@original_prepend = ActiveRecord::QueryLogs.prepend_comment
@original_tags = ActiveRecord::QueryLogs.tags
@original_taggings = ActiveRecord::QueryLogs.taggings
ActiveRecord.query_transformers += [ActiveRecord::QueryLogs]
ActiveRecord::QueryLogs.prepend_comment = false
ActiveRecord::QueryLogs.cache_query_log_tags = false
ActiveRecord::QueryLogs.cached_comment = nil
ActiveRecord::QueryLogs.taggings[:application] = -> {
"active_record"
}
end
def teardown
ActiveRecord.query_transformers = @original_transformers
ActiveRecord::QueryLogs.prepend_comment = @original_prepend
ActiveRecord::QueryLogs.tags = @original_tags
ActiveRecord::QueryLogs.taggings = @original_taggings
ActiveRecord::QueryLogs.prepend_comment = false
ActiveRecord::QueryLogs.cache_query_log_tags = false
ActiveRecord::QueryLogs.clear_cache
ActiveRecord::QueryLogs.update_formatter(:legacy)
# ActiveSupport::ExecutionContext context is automatically reset in Rails app via an executor hooks set in railtie
# But not in Active Record's own test suite.
ActiveSupport::ExecutionContext.clear
end
def test_escaping_good_comment
assert_equal "app:foo", ActiveRecord::QueryLogs.send(:escape_sql_comment, "app:foo")
end
def test_escaping_good_comment_with_custom_separator
ActiveRecord::QueryLogs.update_formatter(:sqlcommenter)
assert_equal "app='foo'", ActiveRecord::QueryLogs.send(:escape_sql_comment, "app='foo'")
end
def test_escaping_bad_comments
assert_equal "* /; DROP TABLE USERS;/ *", ActiveRecord::QueryLogs.send(:escape_sql_comment, "*/; DROP TABLE USERS;/*")
assert_equal "** //; DROP TABLE USERS;/ *", ActiveRecord::QueryLogs.send(:escape_sql_comment, "**//; DROP TABLE USERS;/*")
assert_equal "* * //; DROP TABLE USERS;// * *", ActiveRecord::QueryLogs.send(:escape_sql_comment, "* *//; DROP TABLE USERS;//* *")
end
def test_basic_commenting
ActiveRecord::QueryLogs.tags = [ :application ]
assert_queries_match(%r{select id from posts /\*application:active_record\*/$}) do
ActiveRecord::Base.lease_connection.execute "select id from posts"
end
end
def test_add_comments_to_beginning_of_query
ActiveRecord::QueryLogs.tags = [ :application ]
ActiveRecord::QueryLogs.prepend_comment = true
assert_queries_match(%r{/\*application:active_record\*/ select id from posts$}) do
ActiveRecord::Base.lease_connection.execute "select id from posts"
end
end
def test_exists_is_commented
ActiveRecord::QueryLogs.tags = [ :application ]
assert_queries_match(%r{/\*application:active_record\*/}) do
Dashboard.exists?
end
end
def test_delete_is_commented
ActiveRecord::QueryLogs.tags = [ :application ]
record = Dashboard.first
assert_queries_match(%r{/\*application:active_record\*/}) do
record.destroy
end
end
def test_update_is_commented
ActiveRecord::QueryLogs.tags = [ :application ]
assert_queries_match(%r{/\*application:active_record\*/}) do
dash = Dashboard.first
dash.name = "New name"
dash.save
end
end
def test_create_is_commented
ActiveRecord::QueryLogs.tags = [ :application ]
assert_queries_match(%r{/\*application:active_record\*/}) do
Dashboard.create(name: "Another dashboard")
end
end
def test_select_is_commented
ActiveRecord::QueryLogs.tags = [ :application ]
assert_queries_match(%r{/\*application:active_record\*/}) do
Dashboard.all.to_a
end
end
def test_retrieves_comment_from_cache_when_enabled_and_set
ActiveRecord::QueryLogs.cache_query_log_tags = true
i = 0
ActiveRecord::QueryLogs.tags = [ { query_counter: -> { i += 1 } } ]
assert_queries_match("SELECT 1 /*query_counter:1*/") do
ActiveRecord::Base.lease_connection.execute "SELECT 1"
end
assert_queries_match("SELECT 1 /*query_counter:1*/") do
ActiveRecord::Base.lease_connection.execute "SELECT 1"
end
end
def test_resets_cache_on_context_update
ActiveRecord::QueryLogs.cache_query_log_tags = true
ActiveSupport::ExecutionContext[:temporary] = "value"
ActiveRecord::QueryLogs.tags = [ temporary_tag: ->(context) { context[:temporary] } ]
assert_queries_match("SELECT 1 /*temporary_tag:value*/") do
ActiveRecord::Base.lease_connection.execute "SELECT 1"
end
ActiveSupport::ExecutionContext[:temporary] = "new_value"
assert_queries_match("SELECT 1 /*temporary_tag:new_value*/") do
ActiveRecord::Base.lease_connection.execute "SELECT 1"
end
end
def test_default_tag_behavior
ActiveRecord::QueryLogs.tags = [:application, :foo]
ActiveSupport::ExecutionContext.set(foo: "bar") do
assert_queries_match(%r{/\*application:active_record,foo:bar\*/}) do
Dashboard.first
end
end
assert_queries_match(%r{/\*application:active_record\*/}) do
Dashboard.first
end
end
def test_connection_is_passed_to_tagging_proc
connection = ActiveRecord::Base.lease_connection
ActiveRecord::QueryLogs.tags = [ same_connection: ->(context) { context[:connection] == connection } ]
assert_queries_match("SELECT 1 /*same_connection:true*/") do
connection.execute "SELECT 1"
end
end
def test_connection_does_not_override_already_existing_connection_in_context
fake_connection = Object.new
ActiveSupport::ExecutionContext[:connection] = fake_connection
ActiveRecord::QueryLogs.tags = [ fake_connection: ->(context) { context[:connection] == fake_connection } ]
assert_queries_match("SELECT 1 /*fake_connection:true*/") do
ActiveRecord::Base.lease_connection.execute "SELECT 1"
end
end
def test_empty_comments_are_not_added
ActiveRecord::QueryLogs.tags = [ empty: -> { nil } ]
assert_queries_match(%r{select id from posts$}) do
ActiveRecord::Base.lease_connection.execute "select id from posts"
end
end
def test_sql_commenter_format
ActiveRecord::QueryLogs.update_formatter(:sqlcommenter)
assert_queries_match(%r{/\*application='active_record'\*/}) do
Dashboard.first
end
end
def test_custom_basic_tags
ActiveRecord::QueryLogs.tags = [ :application, { custom_string: "test content" } ]
assert_queries_match(%r{/\*application:active_record,custom_string:test content\*/}) do
Dashboard.first
end
end
def test_custom_proc_tags
ActiveRecord::QueryLogs.tags = [ :application, { custom_proc: -> { "test content" } } ]
assert_queries_match(%r{/\*application:active_record,custom_proc:test content\*/}) do
Dashboard.first
end
end
def test_multiple_custom_tags
ActiveRecord::QueryLogs.tags = [
:application,
{ custom_proc: -> { "test content" }, another_proc: -> { "more test content" } },
]
assert_queries_match(%r{/\*application:active_record,custom_proc:test content,another_proc:more test content\*/}) do
Dashboard.first
end
end
def test_sqlcommenter_format_value
ActiveRecord::QueryLogs.update_formatter(:sqlcommenter)
ActiveRecord::QueryLogs.tags = [
:application,
{ tracestate: "congo=t61rcWkgMzE,rojo=00f067aa0ba902b7", custom_proc: -> { "Joe's Shack" } },
]
assert_queries_match(%r{custom_proc='Joe%27s%20Shack',tracestate='congo%3Dt61rcWkgMzE%2Crojo%3D00f067aa0ba902b7'\*/}) do
Dashboard.first
end
end
def test_sqlcommenter_format_allows_string_keys
ActiveRecord::QueryLogs.update_formatter(:sqlcommenter)
ActiveRecord::QueryLogs.tags = [
:application,
{
"string" => "value",
tracestate: "congo=t61rcWkgMzE,rojo=00f067aa0ba902b7",
custom_proc: -> { "Joe's Shack" }
},
]
assert_queries_match(%r{custom_proc='Joe%27s%20Shack',string='value',tracestate='congo%3Dt61rcWkgMzE%2Crojo%3D00f067aa0ba902b7'\*/}) do
Dashboard.first
end
end
def test_sqlcommenter_format_value_string_coercible
ActiveRecord::QueryLogs.update_formatter(:sqlcommenter)
ActiveRecord::QueryLogs.tags = [
:application,
{ custom_proc: -> { 1234 } },
]
assert_queries_match(%r{custom_proc='1234'\*/}) do
Dashboard.first
end
end
# PostgreSQL does validate the query encoding. Other adapters don't care.
unless current_adapter?(:PostgreSQLAdapter)
def test_invalid_encoding_query
ActiveRecord::QueryLogs.tags = [ :application ]
assert_nothing_raised do
ActiveRecord::Base.lease_connection.execute "select 1 as '\xFF'"
end
end
end
def test_custom_proc_context_tags
ActiveSupport::ExecutionContext[:foo] = "bar"
ActiveRecord::QueryLogs.tags = [ :application, { custom_context_proc: ->(context) { context[:foo] } } ]
assert_queries_match(%r{/\*application:active_record,custom_context_proc:bar\*/}) do
Dashboard.first
end
end
end
|