From 12ff63b227e7ef01c7e57302c9999151dca157f1 Mon Sep 17 00:00:00 2001 From: James Coleman Date: Fri, 2 May 2014 12:32:49 -0400 Subject: Fix exception when logging SQL w/ nil binary value. --- activerecord/CHANGELOG.md | 12 ++++++++++++ activerecord/lib/active_record/log_subscriber.rb | 2 +- activerecord/test/cases/log_subscriber_test.rb | 7 +++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index df73eb4484..0b27aabf63 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,15 @@ +* Log nil binary column values correctly. + + When an object with a binary column is updated with a nil value + in that column, the SQL logger would throw an exception when trying + to log that nil value. This only occurs when updating a record + that already has a non-nil value in that column since an initial nil + value isn't included in the SQL anyway (at least, when dirty checking + is enabled.) The column's new value will now be logged as `` + to parallel the existing `` for non-nil values. + + *James Coleman* + * Stringify all variable keys of mysql connection configuration. When the `sql_mode` variable for mysql adapters is set in the configuration diff --git a/activerecord/lib/active_record/log_subscriber.rb b/activerecord/lib/active_record/log_subscriber.rb index 654ef21b07..17d0c52e47 100644 --- a/activerecord/lib/active_record/log_subscriber.rb +++ b/activerecord/lib/active_record/log_subscriber.rb @@ -25,7 +25,7 @@ module ActiveRecord if column.binary? # This specifically deals with the PG adapter that casts bytea columns into a Hash. value = value[:value] if value.is_a?(Hash) - value = "<#{value.bytesize} bytes of binary data>" + value = value.nil? ? "" : "<#{value.bytesize} bytes of binary data>" end [column.name, value] diff --git a/activerecord/test/cases/log_subscriber_test.rb b/activerecord/test/cases/log_subscriber_test.rb index 97c0350911..a578e81844 100644 --- a/activerecord/test/cases/log_subscriber_test.rb +++ b/activerecord/test/cases/log_subscriber_test.rb @@ -125,5 +125,12 @@ class LogSubscriberTest < ActiveRecord::TestCase wait assert_match(/<16 bytes of binary data>/, @logger.logged(:debug).join) end + + def test_nil_binary_data_is_logged + binary = Binary.create(data: "") + binary.update_attributes(data: nil) + wait + assert_match(//, @logger.logged(:debug).join) + end end end -- cgit v1.2.3