Skip to content
This repository has been archived by the owner on Aug 23, 2022. It is now read-only.

Commit

Permalink
log the sql that is actually sent to the database
Browse files Browse the repository at this point in the history
If I have a query that produces sql
`WHERE "users"."name" = 'a         b'` then in the log all the
whitespace is being squeezed. So the sql that is printed in the
log is `WHERE "users"."name" = 'a b'`.

This can be confusing. This commit fixes it by ensuring that
whitespace is not squeezed.

fixes rails#10982
  • Loading branch information
Neeraj Singh committed Jun 19, 2013
1 parent 615ad88 commit 6fb5f6f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
11 changes: 11 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
* Log the sql that is actually sent to the database.

If I have a query that produces sql
`WHERE "users"."name" = 'a b'` then in the log all the
whitespace is being squeezed. So the sql that is printed in the
log is `WHERE "users"."name" = 'a b'`.

Do not squeeze whitespace out of sql queries. Fixes #10982.

*Neeraj Singh*

* Do not load all child records for inverse case.

currently `post.comments.find(Comment.first.id)` would load all
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/log_subscriber.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def sql(event)
return if IGNORE_PAYLOAD_NAMES.include?(payload[:name])

name = "#{payload[:name]} (#{event.duration.round(1)}ms)"
sql = payload[:sql].squeeze(' ')
sql = payload[:sql]
binds = nil

unless (payload[:binds] || []).empty?
Expand Down
7 changes: 7 additions & 0 deletions activerecord/test/cases/log_subscriber_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,13 @@ def test_schema_statements_are_ignored
assert_equal 2, logger.debugs.length
end

def test_sql_statements_are_not_squeezed
event = Struct.new(:duration, :payload)
logger = TestDebugLogSubscriber.new
logger.sql(event.new(0, sql: 'ruby rails'))
assert_match(/ruby rails/, logger.debugs.first)
end

def test_ignore_binds_payload_with_nil_column
event = Struct.new(:duration, :payload)

Expand Down

0 comments on commit 6fb5f6f

Please sign in to comment.