The unescape routine in binary_to_string only ever triggers unescaping if the data includes a a \ followed by an escaped character. (This is an extremely common occurance in image files the most common item stored in bytea's and therefore this glitch is unnoticed).
However to repeat it, create a model with a bytea field, create and object of that model with the bytea field set to "\001" (a single ctrl-a) save and reload the object and the field will now be "\\001" when accessed (even thought a check of the postgres table will show it thinkgs the field is "\001").
The following patch resolves the issue and causes correct unquoting in all examples I have currently tested (including images STILL unquoting correctly):
Index: vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
===================================================================
--- vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb (revision 7824)
+++ vendor/rails/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb (working copy)
@@ -75,7 +75,7 @@
if PGconn.respond_to?(:unescape_bytea)
self.class.module_eval do
define_method(:binary_to_string) do |value|
- if value =~ /\\\\\d{3}/
+ if value =~ /\\\d{3}/
PGconn.unescape_bytea(value)
else
value
@@ -85,7 +85,7 @@
else
self.class.module_eval do
define_method(:binary_to_string) do |value|
- if value =~ /\\\\\d{3}/
+ if value =~ /\\\d{3}/
result =
i, max = 0, value.size
while i < max