aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--activerecord/CHANGELOG2
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb2
-rw-r--r--activerecord/test/datatype_test_postgresql.rb52
3 files changed, 56 insertions, 0 deletions
diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG
index e19b8d2f90..19e9777c50 100644
--- a/activerecord/CHANGELOG
+++ b/activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* PostgreSQL: return array fields as strings. #4664 [Robby Russell]
+
* SQLServer: added tests to ensure all database statements are closed, refactored identity_insert management code to use blocks, removed update/delete rowcount code out of execute and into update/delete, changed insert to go through execute method, removed unused quoting methods, disabled pessimistic locking tests as feature is currently unsupported, fixed RakeFile to load sqlserver specific tests whether running in ado or odbc mode, fixed support for recently added decimal types, added support for limits on integer types. #5670 [Tom Ward]
* SQLServer: fix db:schema:dump case-sensitivity. #4684 [Will Rogers]
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index 2fa357993f..94584a5ea4 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -515,6 +515,8 @@ module ActiveRecord
def translate_field_type(field_type)
# Match the beginning of field_type since it may have a size constraint on the end.
case field_type
+ # PostgreSQL array data types.
+ when /\[\]$/i then 'string'
when /^timestamp/i then 'datetime'
when /^real|^money/i then 'float'
when /^interval/i then 'string'
diff --git a/activerecord/test/datatype_test_postgresql.rb b/activerecord/test/datatype_test_postgresql.rb
new file mode 100644
index 0000000000..c4c3318de4
--- /dev/null
+++ b/activerecord/test/datatype_test_postgresql.rb
@@ -0,0 +1,52 @@
+require 'abstract_unit'
+
+class PostgresqlDatatype < ActiveRecord::Base
+end
+
+class PGDataTypeTest < Test::Unit::TestCase
+ self.use_transactional_fixtures = false
+
+ TABLE_NAME = 'postgresql_datatypes'
+ COLUMNS = [
+ 'id SERIAL PRIMARY KEY',
+ 'commission_by_quarter INTEGER[]',
+ 'nicknames TEXT[]'
+ ]
+
+ def setup
+ @connection = ActiveRecord::Base.connection
+ @connection.execute "CREATE TABLE #{TABLE_NAME} (#{COLUMNS.join(',')})"
+ @connection.execute "INSERT INTO #{TABLE_NAME} (commission_by_quarter, nicknames) VALUES ( '{35000,21000,18000,17000}', '{foo,bar,baz}' )"
+ @first = PostgresqlDatatype.find( 1 )
+ end
+
+ def teardown
+ @connection.execute "DROP TABLE #{TABLE_NAME}"
+ end
+
+ def test_data_type_of_array_types
+ assert_equal :string, @first.column_for_attribute("commission_by_quarter").type
+ assert_equal :string, @first.column_for_attribute("nicknames").type
+ end
+
+ def test_array_values
+ assert_equal '{35000,21000,18000,17000}', @first.commission_by_quarter
+ assert_equal '{foo,bar,baz}', @first.nicknames
+ end
+
+ def test_update_integer_array
+ new_value = '{32800,95000,29350,17000}'
+ assert @first.commission_by_quarter = new_value
+ assert @first.save
+ assert @first.reload
+ assert_equal @first.commission_by_quarter, new_value
+ end
+
+ def test_update_text_array
+ new_value = '{robby,robert,rob,robbie}'
+ assert @first.nicknames = new_value
+ assert @first.save
+ assert @first.reload
+ assert_equal @first.nicknames, new_value
+ end
+end