1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Ignore binds payload with nil column in AR log subscriber

Some tests were raising the following error:

    Could not log "sql.active_record" event. NoMethodError: undefined method
    `type' for nil:NilClass`

Due to the way binds were being logged, the column info was considered
always present, but that is not true for some of the tests listed in the
issue.

Closes #8806.
This commit is contained in:
Carlos Antonio da Silva 2013-01-08 01:28:23 -02:00
parent de21da5048
commit 77516a712b
2 changed files with 34 additions and 22 deletions

View file

@ -21,13 +21,15 @@ module ActiveRecord
end
def render_bind(column, value)
if column.type == :binary
rendered_value = "<#{value.bytesize} bytes of binary data>"
else
rendered_value = value
end
if column
if column.binary?
value = "<#{value.bytesize} bytes of binary data>"
end
[column.name, rendered_value]
[column.name, value]
else
[nil, value]
end
end
def sql(event)

View file

@ -8,6 +8,19 @@ class LogSubscriberTest < ActiveRecord::TestCase
include ActiveSupport::LogSubscriber::TestHelper
include ActiveSupport::Logger::Severity
class TestDebugLogSubscriber < ActiveRecord::LogSubscriber
attr_reader :debugs
def initialize
@debugs = []
super
end
def debug message
@debugs << message
end
end
fixtures :posts
def setup
@ -30,30 +43,27 @@ class LogSubscriberTest < ActiveRecord::TestCase
def test_schema_statements_are_ignored
event = Struct.new(:duration, :payload)
logger = Class.new(ActiveRecord::LogSubscriber) {
attr_accessor :debugs
def initialize
@debugs = []
super
end
def debug message
@debugs << message
end
}.new
logger = TestDebugLogSubscriber.new
assert_equal 0, logger.debugs.length
logger.sql(event.new(0, { :sql => 'hi mom!' }))
logger.sql(event.new(0, sql: 'hi mom!'))
assert_equal 1, logger.debugs.length
logger.sql(event.new(0, { :sql => 'hi mom!', :name => 'foo' }))
logger.sql(event.new(0, sql: 'hi mom!', name: 'foo'))
assert_equal 2, logger.debugs.length
logger.sql(event.new(0, { :sql => 'hi mom!', :name => 'SCHEMA' }))
logger.sql(event.new(0, sql: 'hi mom!', name: 'SCHEMA'))
assert_equal 2, logger.debugs.length
end
def test_ignore_binds_payload_with_nil_column
event = Struct.new(:duration, :payload)
logger = TestDebugLogSubscriber.new
logger.sql(event.new(0, sql: 'hi mom!', binds: [[nil, 1]]))
assert_equal 1, logger.debugs.length
end
def test_basic_query_logging
Developer.all.load
wait
@ -105,7 +115,7 @@ class LogSubscriberTest < ActiveRecord::TestCase
def test_binary_data_is_not_logged
skip if current_adapter?(:Mysql2Adapter)
Binary.create(:data => 'some binary data')
Binary.create(data: 'some binary data')
wait
assert_match(/<16 bytes of binary data>/, @logger.logged(:debug).join)
end