Skip to content

Commit 1e6790a

Browse files
authored
Merge pull request #972 from yellowspot/support-query-logs
[Rails 7] Transform queries to add comments
2 parents 0fc3c40 + aa72c34 commit 1e6790a

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@
1010

1111
#### Added
1212

13-
...
13+
- [#972](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/pull/972) Support `ActiveRecord::QueryLogs`
1414

1515
Please check [6-1-stable](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/blob/6-1-stable/CHANGELOG.md) for previous changes.

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ def write_query?(sql) # :nodoc:
1212
end
1313

1414
def execute(sql, name = nil)
15+
sql = transform_query(sql)
1516
if preventing_writes? && write_query?(sql)
1617
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
1718
end
@@ -27,6 +28,7 @@ def execute(sql, name = nil)
2728
end
2829

2930
def exec_query(sql, name = "SQL", binds = [], prepare: false, async: false)
31+
sql = transform_query(sql)
3032
if preventing_writes? && write_query?(sql)
3133
raise ActiveRecord::ReadOnlyError, "Write query attempted while in readonly mode: #{sql}"
3234
end

test/cases/coerced_tests.rb

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1961,3 +1961,45 @@ def test_in_order_of_with_enums_keys_coerced
19611961
Book.connection.add_index(:books, [:author_id, :name], unique: true)
19621962
end
19631963
end
1964+
1965+
require "models/dashboard"
1966+
class QueryLogsTest < ActiveRecord::TestCase
1967+
# Same as original coerced test except our SQL ends with binding.
1968+
# TODO: Remove coerce after Rails 7.1.0 (see https://github.com/rails/rails/pull/44053)
1969+
coerce_tests! :test_custom_basic_tags, :test_custom_proc_tags, :test_multiple_custom_tags, :test_custom_proc_context_tags
1970+
def test_custom_basic_tags_coerced
1971+
ActiveRecord::QueryLogs.tags = [ :application, { custom_string: "test content" } ]
1972+
1973+
assert_sql(%r{/\*application:active_record,custom_string:test content\*/}) do
1974+
Dashboard.first
1975+
end
1976+
end
1977+
1978+
def test_custom_proc_tags_coerced
1979+
ActiveRecord::QueryLogs.tags = [ :application, { custom_proc: -> { "test content" } } ]
1980+
1981+
assert_sql(%r{/\*application:active_record,custom_proc:test content\*/}) do
1982+
Dashboard.first
1983+
end
1984+
end
1985+
1986+
def test_multiple_custom_tags_coerced
1987+
ActiveRecord::QueryLogs.tags = [
1988+
:application,
1989+
{ custom_proc: -> { "test content" }, another_proc: -> { "more test content" } },
1990+
]
1991+
1992+
assert_sql(%r{/\*application:active_record,custom_proc:test content,another_proc:more test content\*/}) do
1993+
Dashboard.first
1994+
end
1995+
end
1996+
1997+
def test_custom_proc_context_tags_coerced
1998+
ActiveSupport::ExecutionContext[:foo] = "bar"
1999+
ActiveRecord::QueryLogs.tags = [ :application, { custom_context_proc: ->(context) { context[:foo] } } ]
2000+
2001+
assert_sql(%r{/\*application:active_record,custom_context_proc:bar\*/}) do
2002+
Dashboard.first
2003+
end
2004+
end
2005+
end

0 commit comments

Comments
 (0)