1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

change_column for PG adapter respects :array option.

This commit is contained in:
Yves Senn 2013-07-16 09:49:55 +02:00
parent c612638a25
commit 754a373e30
3 changed files with 19 additions and 2 deletions

View file

@ -1,3 +1,7 @@
* `change_column` for PostgreSQL adapter respects the `:array` option.
*Yves Senn*
* Remove deprecation warning from `attribute_missing` for attributes that are columns.
*Arun Agrawal*

View file

@ -384,8 +384,9 @@ module ActiveRecord
def change_column(table_name, column_name, type, options = {})
clear_cache!
quoted_table_name = quote_table_name(table_name)
execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}"
sql_type = type_to_sql(type, options[:limit], options[:precision], options[:scale])
sql_type << "[]" if options[:array]
execute "ALTER TABLE #{quoted_table_name} ALTER COLUMN #{quote_column_name(column_name)} TYPE #{sql_type}"
change_column_default(table_name, column_name, options[:default]) if options_include_default?(options)
change_column_null(table_name, column_name, options[:null], options[:default]) if options.key?(:null)

View file

@ -27,6 +27,18 @@ class PostgresqlArrayTest < ActiveRecord::TestCase
assert @column.array
end
def test_change_column_with_array
@connection.add_column :pg_arrays, :snippets, :string, array: true, default: []
@connection.change_column :pg_arrays, :snippets, :text, array: true, default: "{}"
PgArray.reset_column_information
column = PgArray.columns.find { |c| c.name == 'snippets' }
assert_equal :text, column.type
assert_equal [], column.default
assert column.array
end
def test_type_cast_array
assert @column