Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

`change_column` for PG adapter respects `:array` option.

Conflicts:

	activerecord/CHANGELOG.md
  • Loading branch information...
commit c940b148f38fac551082416ce0799cfa89f2bbc1 1 parent 317527d
@senny senny authored
View
4 activerecord/CHANGELOG.md
@@ -1,5 +1,9 @@
## unreleased ##
+* `change_column` for PostgreSQL adapter respects the `:array` option.
+
+ *Yves Senn*
+
* fixes bug introduced by #3329. Now, when autosaving associations,
deletions happen before inserts and saves. This prevents a 'duplicate
unique value' database error that would occur if a record being created had
View
5 activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb
@@ -384,8 +384,9 @@ def add_column(table_name, column_name, type, options = {})
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
12 activerecord/test/cases/adapters/postgresql/array_test.rb
@@ -27,6 +27,18 @@ def test_column
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
Please sign in to comment.
Something went wrong with that request. Please try again.