Skip to content

Commit

Permalink
workaround deserialize on save - rails/rails#44317
Browse files Browse the repository at this point in the history
  • Loading branch information
akostadinov committed Feb 3, 2022
1 parent dc42c57 commit a040493
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
12 changes: 10 additions & 2 deletions lib/active_record/type/oracle_enhanced/raw.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,21 @@ def type
:raw
end

# see ActiveModel::ActiveModel#forget_attribute_assignments
# workaround for https://github.com/rails/rails/issues/44317
def deserialize(value)
value.respond_to?(:enhanced_deserialized) ? value.enhanced_deserialized : super
end

def serialize(value)
# Encode a string or byte array as string of hex codes
if value.nil?
super
else
value = value.unpack("C*")
value.map { |x| "%02X" % x }.join
value.unpack("C*").map { |x| "%02X" % x }.join.tap do |serialized|
serialized.singleton_class.attr_accessor :enhanced_deserialized
serialized.enhanced_deserialized = value
end
end
end
end
Expand Down
24 changes: 21 additions & 3 deletions spec/active_record/oracle_enhanced/type/raw_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
t.raw :binary_data, limit: 1024
end
end
@binary_data = "\0\1\2\3\4\5\6\7\8\9" * 100
@binary_data = ("\0\1\2\3\4\5\6\7\8\9" * 100).freeze
@binary_data2 = "\1\2\3\4\5\6\7\8\9\0" * 100
end

Expand All @@ -38,6 +38,7 @@ class ::TestEmployee < ActiveRecord::Base
last_name: "Last",
binary_data: @binary_data
)
expect(@employee.binary_data).to eq(@binary_data)
@employee.reload
expect(@employee.binary_data).to eq(@binary_data)
end
Expand All @@ -51,6 +52,7 @@ class ::TestEmployee < ActiveRecord::Base
expect(@employee.binary_data).to be_nil
@employee.binary_data = @binary_data
@employee.save!
expect(@employee.binary_data).to eq(@binary_data)
@employee.reload
expect(@employee.binary_data).to eq(@binary_data)
end
Expand All @@ -77,6 +79,7 @@ class ::TestEmployee < ActiveRecord::Base
@employee.reload
@employee.binary_data = @binary_data2
@employee.save!
expect(@employee.binary_data).to eq(@binary_data2)
@employee.reload
expect(@employee.binary_data).to eq(@binary_data2)
end
Expand Down Expand Up @@ -116,13 +119,14 @@ class ::TestEmployee < ActiveRecord::Base
@employee.reload
@employee.binary_data = @binary_data
@employee.save!
expect(@employee.binary_data).to eq(@binary_data)
@employee.reload
expect(@employee.binary_data).to eq(@binary_data)
end

it "should allow equality on select" do
TestEmployee.delete_all
TestEmployee.create!(
employee = TestEmployee.create!(
first_name: "First",
last_name: "Last",
binary_data: @binary_data,
Expand All @@ -132,6 +136,20 @@ class ::TestEmployee < ActiveRecord::Base
last_name: "Last1",
binary_data: @binary_data2,
)
expect(TestEmployee.where(binary_data: @binary_data)).to have_attributes(count: 1)
expect(TestEmployee.where(binary_data: @binary_data).to_a).to eq([employee])
end

it "should allow equality on select with NULL value" do
TestEmployee.delete_all
employee = TestEmployee.create!(
first_name: "First",
last_name: "Last",
)
TestEmployee.create!(
first_name: "First1",
last_name: "Last1",
binary_data: @binary_data2,
)
expect(TestEmployee.where(binary_data: nil).to_a).to eq([employee])
end
end

0 comments on commit a040493

Please sign in to comment.