提交 4ed60af6 编写于 作者: S Sean Griffin

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.
上级 d428242c
...@@ -34,6 +34,9 @@ def type_cast_from_database(value) ...@@ -34,6 +34,9 @@ def type_cast_from_database(value)
end end
def type_cast_from_user(value) 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) type_cast_array(value, :type_cast_from_user)
end end
......
...@@ -264,11 +264,26 @@ def test_datetime_with_timezone_awareness ...@@ -264,11 +264,26 @@ def test_datetime_with_timezone_awareness
def test_assigning_non_array_value def test_assigning_non_array_value
record = PgArray.new(tags: "not-an-array") record = PgArray.new(tags: "not-an-array")
assert_equal "not-an-array", record.tags assert_equal [], record.tags
e = assert_raises(ActiveRecord::StatementInvalid) do assert_equal "not-an-array", record.tags_before_type_cast
record.save! assert record.save
end assert_equal record.tags, record.reload.tags
assert_instance_of PG::InvalidTextRepresentation, e.original_exception 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 end
def test_uniqueness_validation def test_uniqueness_validation
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册