rails/rails — pull request #10264 by cconstantine (2013-04-18 19:46:29+00:00)
Title: Postgresql array columns don't properly escape single quote strings when loading fixtures

In the fixture builder there was a single statement that inserts the fixture row and yields something like:

INSERT INTO table_name ("arr_column") VALUES ('{"asdf", "as'df"}')

The problem with this was that the single quote in the second array value needs to be escaped. Normally they won't need to be escaped in strings, but when array columns go all the way to sql (instead of binding to the connection) they get wrapped in '{ and }' and that makes escaping them required.

This fix causes the insert to look more like:
INSERT INTO table_name("arr_column") VALUES( $1 ) [ ["arr_column", ["asdf", "as''df"] ] ]
The extra single quote (') character is the escape for a single quote in a string.

I made a test for this, to run the test:
cd activerecord && ARCONN=postgresql ruby -Itest test/cases/adapters/postgresql/array_test.rb -n test_insert_fixture

To see the test fail revert the change at https://github.com/cconstantine/rails/commit/0e34a7efa6992e6771d34dc8163b79dd7df52667#L0R35

This change is only in postgresql code, and everything passes when I run
ARCONN=postgresql rake test_postgresql

This is a followup to the failed attempt to fix https://github.com/rails/rails/pull/10043 .