Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

pg, keep `hstore` and `json` attributes as `Hash` in @attributes.

The solution presented in this patch is not efficient. We should replace it
in the near future. The following needs to be worked out:
  * Is `@attributes` storing the Ruby or SQL representation?
  * `cacheable_column?` is broken but `hstore` and `json` rely on that behavior

Closes #15369.
  • Loading branch information...
commit 5e09ea06a640c0205f4c7db1f2b2205dcebd13de 1 parent 7f46d20
@senny senny authored
View
7 activerecord/CHANGELOG.md
@@ -1,3 +1,10 @@
+* Keep PostgreSQL `hstore` and `json` attributes as `Hash` in `@attributes`.
+ Fixes duplication in combination with `store_accessor`.
+
+ Fixes #15369.
+
+ *Yves Senn*
+
* `rake railties:install:migrations` respects the order of railties.
*Arun Agrawal*
View
10 activerecord/lib/active_record/connection_adapters/postgresql/oid.rb
@@ -231,7 +231,10 @@ def type_cast(value)
class Hstore < Type
def type_cast_for_write(value)
- ConnectionAdapters::PostgreSQLColumn.hstore_to_string value
+ # roundtrip to ensure uniform uniform types
+ # TODO: This is not an efficient solution.
+ stringified = ConnectionAdapters::PostgreSQLColumn.hstore_to_string(value)
+ type_cast(stringified)
end
def type_cast(value)
@@ -255,7 +258,10 @@ def type_cast(value)
class Json < Type
def type_cast_for_write(value)
- ConnectionAdapters::PostgreSQLColumn.json_to_string value
+ # roundtrip to ensure uniform uniform types
+ # TODO: This is not an efficient solution.
+ stringified = ConnectionAdapters::PostgreSQLColumn.json_to_string(value)
+ type_cast(stringified)
end
def type_cast(value)
View
10 activerecord/test/cases/adapters/postgresql/hstore_test.rb
@@ -126,6 +126,16 @@ def test_with_store_accessors
assert_equal "GMT", x.timezone
end
+ def test_duplication_with_store_accessors
+ x = Hstore.new(language: "fr", timezone: "GMT")
+ assert_equal "fr", x.language
+ assert_equal "GMT", x.timezone
+
+ y = x.dup
+ assert_equal "fr", y.language
+ assert_equal "GMT", y.timezone
+ end
+
def test_gen1
assert_equal(%q(" "=>""), @column.class.hstore_to_string({' '=>''}))
end
View
8 activerecord/test/cases/adapters/postgresql/json_test.rb
@@ -122,6 +122,14 @@ def test_with_store_accessors
assert_equal "640×1136", x.resolution
end
+ def test_duplication_with_store_accessors
+ x = JsonDataType.new(resolution: "320×480")
+ assert_equal "320×480", x.resolution
+
+ y = x.dup
+ assert_equal "320×480", y.resolution
+ end
+
def test_update_all
json = JsonDataType.create! payload: { "one" => "two" }
Please sign in to comment.
Something went wrong with that request. Please try again.