Skip to content

Commit

Permalink
Prevent type casting for binary columns on PG to fix zdennis#443
Browse files Browse the repository at this point in the history
  • Loading branch information
mwalsher committed Sep 6, 2017
1 parent 3246575 commit ae13e26
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/activerecord-import/import.rb
Expand Up @@ -772,7 +772,9 @@ def values_sql_for_columns_and_attributes(columns, array_of_attributes) # :nodoc
if serialized_attributes.include?(column.name)
val = serialized_attributes[column.name].dump(val)
end
connection_memo.quote(column.type_cast(val), column)
# Fixes #443 to support binary (i.e. bytea) columns on PG
val = column.type_cast(val) unless column.type.to_sym == :binary
connection_memo.quote(val, column)
end
end
end
Expand Down
1 change: 1 addition & 0 deletions test/schema/postgresql_schema.rb
Expand Up @@ -36,6 +36,7 @@
t.column :alarm_type, :integer, null: false
t.column :status, :integer, null: false
t.column :metadata, :text
t.column :secret_key, :binary
t.datetime :created_at
t.datetime :updated_at
end
Expand Down
11 changes: 11 additions & 0 deletions test/support/postgresql/import_examples.rb
Expand Up @@ -231,6 +231,17 @@ def should_support_postgresql_import_functionality
end
end

describe "with binary field" do
let(:binary_value) { "\xE0'c\xB2\xB0\xB3Bh\\\xC2M\xB1m\\I\xC4r".force_encoding('ASCII-8BIT') }
it "imports the correct values for binary fields" do
alarms = [Alarm.new(device_id: 1, alarm_type: 1, status: 1, secret_key: binary_value)]
assert_difference "Alarm.count", +1 do
Alarm.import alarms
end
assert_equal(binary_value, Alarm.first.secret_key)
end
end

def should_support_postgresql_upsert_functionality
should_support_basic_on_duplicate_key_update
should_support_on_duplicate_key_ignore
Expand Down

0 comments on commit ae13e26

Please sign in to comment.