aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorRafael França <rafaelmfranca@gmail.com>2017-05-29 22:12:15 -0400
committerGitHub <noreply@github.com>2017-05-29 22:12:15 -0400
commit6847877a30fd8d578f72db0cf40674f71a9b6286 (patch)
treeb4a49bb98258e075c853afe89a54e32adf43fd39 /activerecord
parent0aa6bcdf612aa05e422f1f55278bab49a7a9153e (diff)
parent71cd0659699a539ef8713faf776d12bef9ff0ce8 (diff)
downloadrails-6847877a30fd8d578f72db0cf40674f71a9b6286.tar.gz
rails-6847877a30fd8d578f72db0cf40674f71a9b6286.tar.bz2
rails-6847877a30fd8d578f72db0cf40674f71a9b6286.zip
Merge pull request #29273 from kamipo/deserialize_raw_value_from_database_for_json
Deserialize a raw value from the database in `changed_in_place?` for `AbstractJson`
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb5
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb10
-rw-r--r--activerecord/lib/active_record/type/internal/abstract_json.rb4
-rw-r--r--activerecord/test/cases/json_shared_test_cases.rb11
4 files changed, 15 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
index 9cb7c46df5..01599985ca 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -838,11 +838,6 @@ module ActiveRecord
end
class MysqlJson < Type::Internal::AbstractJson # :nodoc:
- def changed_in_place?(raw_old_value, new_value)
- # Normalization is required because MySQL JSON data format includes
- # the space between the elements.
- super(serialize(deserialize(raw_old_value)), new_value)
- end
end
class MysqlString < Type::String # :nodoc:
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb
index 87391b5dc7..705cb7f0b3 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb
@@ -6,16 +6,6 @@ module ActiveRecord
def type
:jsonb
end
-
- def changed_in_place?(raw_old_value, new_value)
- # Postgres does not preserve insignificant whitespaces when
- # round-tripping jsonb columns. This causes some false positives for
- # the comparison here. Therefore, we need to parse and re-dump the
- # raw value here to ensure the insignificant whitespaces are
- # consistent with our encoder's output.
- raw_old_value = serialize(deserialize(raw_old_value))
- super(raw_old_value, new_value)
- end
end
end
end
diff --git a/activerecord/lib/active_record/type/internal/abstract_json.rb b/activerecord/lib/active_record/type/internal/abstract_json.rb
index e19c5a14da..a8d6a63465 100644
--- a/activerecord/lib/active_record/type/internal/abstract_json.rb
+++ b/activerecord/lib/active_record/type/internal/abstract_json.rb
@@ -24,6 +24,10 @@ module ActiveRecord
end
end
+ def changed_in_place?(raw_old_value, new_value)
+ deserialize(raw_old_value) != new_value
+ end
+
def accessor
ActiveRecord::Store::StringKeyedHashAccessor
end
diff --git a/activerecord/test/cases/json_shared_test_cases.rb b/activerecord/test/cases/json_shared_test_cases.rb
index d190b027bf..ef5ca86874 100644
--- a/activerecord/test/cases/json_shared_test_cases.rb
+++ b/activerecord/test/cases/json_shared_test_cases.rb
@@ -160,6 +160,17 @@ module JSONSharedTestCases
assert_not json.changed?
end
+ def test_changes_in_place_with_ruby_object
+ time = Time.now.utc
+ json = JsonDataType.create!(payload: time)
+
+ json.reload
+ assert_not json.changed?
+
+ json.payload = time
+ assert_not json.changed?
+ end
+
def test_assigning_string_literal
json = JsonDataType.create!(payload: "foo")
assert_equal "foo", json.payload