diff options
author | Yves Senn <yves.senn@gmail.com> | 2014-05-28 13:32:40 +0200 |
---|---|---|
committer | Yves Senn <yves.senn@gmail.com> | 2014-05-28 13:35:02 +0200 |
commit | bdbf00dc78869f91a8af4d56a19213faf2b8d9e5 (patch) | |
tree | bb9e53068d5fb30ea7c038888d531d0673f1e7d6 /activerecord | |
parent | 7f73b9152cfc3f218cfc862e971ba56b94f6be10 (diff) | |
download | rails-bdbf00dc78869f91a8af4d56a19213faf2b8d9e5.tar.gz rails-bdbf00dc78869f91a8af4d56a19213faf2b8d9e5.tar.bz2 rails-bdbf00dc78869f91a8af4d56a19213faf2b8d9e5.zip |
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
Refs #15369.
/cc @sgrif @rafaelfranca
Diffstat (limited to 'activerecord')
5 files changed, 39 insertions, 2 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 9fca0db7a1..c3c61cf50e 100644 --- a/activerecord/CHANGELOG.md +++ b/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* diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/hstore.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/hstore.rb index 98f369a7f8..bf680b6624 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/hstore.rb @@ -8,7 +8,13 @@ module ActiveRecord end 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. + cast_value(type_cast_for_database(value)) + end + + def type_cast_for_database(value) + ConnectionAdapters::PostgreSQLColumn.hstore_to_string(value) end def cast_value(value) diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb index 42bf5656f4..42a5110ffd 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb @@ -8,7 +8,13 @@ module ActiveRecord end 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. + cast_value(type_cast_for_database(value)) + end + + def type_cast_for_database(value) + ConnectionAdapters::PostgreSQLColumn.json_to_string(value) end def cast_value(value) diff --git a/activerecord/test/cases/adapters/postgresql/hstore_test.rb b/activerecord/test/cases/adapters/postgresql/hstore_test.rb index 67a610b459..1fef4899be 100644 --- a/activerecord/test/cases/adapters/postgresql/hstore_test.rb +++ b/activerecord/test/cases/adapters/postgresql/hstore_test.rb @@ -142,6 +142,16 @@ class PostgresqlHstoreTest < ActiveRecord::TestCase 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 diff --git a/activerecord/test/cases/adapters/postgresql/json_test.rb b/activerecord/test/cases/adapters/postgresql/json_test.rb index d25f8bf958..03b546119d 100644 --- a/activerecord/test/cases/adapters/postgresql/json_test.rb +++ b/activerecord/test/cases/adapters/postgresql/json_test.rb @@ -139,6 +139,14 @@ class PostgresqlJSONTest < ActiveRecord::TestCase 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" } |