aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-12-08 11:40:41 -0700
committerSean Griffin <sean@thoughtbot.com>2014-12-08 11:42:29 -0700
commit4ed60af60d8b0ce6696f03d2d77916713c39af12 (patch)
tree2ce30cae4b4bc0b461f425437bb57c316002f671 /activerecord
parentd428242c865847f86512b671428f556ab88cc21d (diff)
downloadrails-4ed60af60d8b0ce6696f03d2d77916713c39af12.tar.gz
rails-4ed60af60d8b0ce6696f03d2d77916713c39af12.tar.bz2
rails-4ed60af60d8b0ce6696f03d2d77916713c39af12.zip
Revert to 4.1 behavior for casting PG arrays
The user is able to pass PG string literals in 4.1, and have it converted to an array. This is also possible in 4.2, but it would remain in string form until saving and reloading, which breaks our `attr = save.reload.attr` contract. I think we should deprecate this in 5.0, and only allow array input from user sources. However, this currently constitutes a breaking change to public API that did not go through a deprecation cycle.
Diffstat (limited to 'activerecord')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb3
-rw-r--r--activerecord/test/cases/adapters/postgresql/array_test.rb25
2 files changed, 23 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb
index cd5efe2bb8..c203e6c604 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb
@@ -34,6 +34,9 @@ module ActiveRecord
end
def type_cast_from_user(value)
+ if value.is_a?(::String)
+ value = parse_pg_array(value)
+ end
type_cast_array(value, :type_cast_from_user)
end
diff --git a/activerecord/test/cases/adapters/postgresql/array_test.rb b/activerecord/test/cases/adapters/postgresql/array_test.rb
index db302b6294..042beab23f 100644
--- a/activerecord/test/cases/adapters/postgresql/array_test.rb
+++ b/activerecord/test/cases/adapters/postgresql/array_test.rb
@@ -264,11 +264,26 @@ class PostgresqlArrayTest < ActiveRecord::TestCase
def test_assigning_non_array_value
record = PgArray.new(tags: "not-an-array")
- assert_equal "not-an-array", record.tags
- e = assert_raises(ActiveRecord::StatementInvalid) do
- record.save!
- end
- assert_instance_of PG::InvalidTextRepresentation, e.original_exception
+ assert_equal [], record.tags
+ assert_equal "not-an-array", record.tags_before_type_cast
+ assert record.save
+ assert_equal record.tags, record.reload.tags
+ end
+
+ def test_assigning_empty_string
+ record = PgArray.new(tags: "")
+ assert_equal [], record.tags
+ assert_equal "", record.tags_before_type_cast
+ assert record.save
+ assert_equal record.tags, record.reload.tags
+ end
+
+ def test_assigning_valid_pg_array_literal
+ record = PgArray.new(tags: "{1,2,3}")
+ assert_equal ["1", "2", "3"], record.tags
+ assert_equal "{1,2,3}", record.tags_before_type_cast
+ assert record.save
+ assert_equal record.tags, record.reload.tags
end
def test_uniqueness_validation