mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Correctly detect mutation on serialized columns mapping to binary
Fixes #16701
This commit is contained in:
parent
49b27dba28
commit
990322b298
3 changed files with 31 additions and 0 deletions
|
@ -22,6 +22,11 @@ module ActiveRecord
|
|||
Data.new(super)
|
||||
end
|
||||
|
||||
def changed_in_place?(raw_old_value, value)
|
||||
old_value = type_cast_from_database(raw_old_value)
|
||||
old_value != value
|
||||
end
|
||||
|
||||
class Data # :nodoc:
|
||||
def initialize(value)
|
||||
@value = value.to_s
|
||||
|
@ -30,10 +35,15 @@ module ActiveRecord
|
|||
def to_s
|
||||
@value
|
||||
end
|
||||
alias_method :to_str, :to_s
|
||||
|
||||
def hex
|
||||
@value.unpack('H*')[0]
|
||||
end
|
||||
|
||||
def ==(other)
|
||||
other == to_s || super
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -26,6 +26,11 @@ module ActiveRecord
|
|||
end
|
||||
end
|
||||
|
||||
def changed_in_place?(raw_old_value, value)
|
||||
return false if value.nil?
|
||||
subtype.changed_in_place?(raw_old_value, coder.dump(value))
|
||||
end
|
||||
|
||||
def accessor
|
||||
ActiveRecord::Store::IndifferentHashAccessor
|
||||
end
|
||||
|
|
|
@ -682,6 +682,22 @@ class DirtyTest < ActiveRecord::TestCase
|
|||
assert_not pirate.changed?
|
||||
end
|
||||
|
||||
test "in place mutation for binary" do
|
||||
klass = Class.new(ActiveRecord::Base) do
|
||||
self.table_name = :binaries
|
||||
serialize :data
|
||||
end
|
||||
|
||||
klass.create!(data: "foo")
|
||||
binary = klass.first
|
||||
|
||||
assert_not binary.changed?
|
||||
|
||||
binary.data << "bar"
|
||||
|
||||
assert binary.changed?
|
||||
end
|
||||
|
||||
private
|
||||
def with_partial_writes(klass, on = true)
|
||||
old = klass.partial_writes?
|
||||
|
|
Loading…
Reference in a new issue