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:
parent
de21da5048
commit
77516a712b
2 changed files with 34 additions and 22 deletions
|
@ -21,13 +21,15 @@ module ActiveRecord
|
||||||
end
|
end
|
||||||
|
|
||||||
def render_bind(column, value)
|
def render_bind(column, value)
|
||||||
if column.type == :binary
|
if column
|
||||||
rendered_value = "<#{value.bytesize} bytes of binary data>"
|
if column.binary?
|
||||||
else
|
value = "<#{value.bytesize} bytes of binary data>"
|
||||||
rendered_value = value
|
|
||||||
end
|
end
|
||||||
|
|
||||||
[column.name, rendered_value]
|
[column.name, value]
|
||||||
|
else
|
||||||
|
[nil, value]
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def sql(event)
|
def sql(event)
|
||||||
|
|
|
@ -8,6 +8,19 @@ class LogSubscriberTest < ActiveRecord::TestCase
|
||||||
include ActiveSupport::LogSubscriber::TestHelper
|
include ActiveSupport::LogSubscriber::TestHelper
|
||||||
include ActiveSupport::Logger::Severity
|
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
|
fixtures :posts
|
||||||
|
|
||||||
def setup
|
def setup
|
||||||
|
@ -30,30 +43,27 @@ class LogSubscriberTest < ActiveRecord::TestCase
|
||||||
def test_schema_statements_are_ignored
|
def test_schema_statements_are_ignored
|
||||||
event = Struct.new(:duration, :payload)
|
event = Struct.new(:duration, :payload)
|
||||||
|
|
||||||
logger = Class.new(ActiveRecord::LogSubscriber) {
|
logger = TestDebugLogSubscriber.new
|
||||||
attr_accessor :debugs
|
|
||||||
|
|
||||||
def initialize
|
|
||||||
@debugs = []
|
|
||||||
super
|
|
||||||
end
|
|
||||||
|
|
||||||
def debug message
|
|
||||||
@debugs << message
|
|
||||||
end
|
|
||||||
}.new
|
|
||||||
assert_equal 0, logger.debugs.length
|
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
|
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
|
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
|
assert_equal 2, logger.debugs.length
|
||||||
end
|
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
|
def test_basic_query_logging
|
||||||
Developer.all.load
|
Developer.all.load
|
||||||
wait
|
wait
|
||||||
|
@ -105,7 +115,7 @@ class LogSubscriberTest < ActiveRecord::TestCase
|
||||||
def test_binary_data_is_not_logged
|
def test_binary_data_is_not_logged
|
||||||
skip if current_adapter?(:Mysql2Adapter)
|
skip if current_adapter?(:Mysql2Adapter)
|
||||||
|
|
||||||
Binary.create(:data => 'some binary data')
|
Binary.create(data: 'some binary data')
|
||||||
wait
|
wait
|
||||||
assert_match(/<16 bytes of binary data>/, @logger.logged(:debug).join)
|
assert_match(/<16 bytes of binary data>/, @logger.logged(:debug).join)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue