Skip to content

Commit

Permalink
pg, change_column_default accepts []. Closes rails#11586.
Browse files Browse the repository at this point in the history
  • Loading branch information
senny committed May 12, 2014
1 parent 711af75 commit 096be96
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 3 deletions.
6 changes: 6 additions & 0 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
* `change_column_default` allows `[]` as argument to `change_column_default`.

Fixes #11586.

*Yves Senn*

* Handle `name` and `"char"` column types in the PostgreSQL adapter.

`name` and `"char"` are special character types used internally by
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ def quoted_date(value) #:nodoc:
def quote_default_value(value, column) #:nodoc:
if column.type == :uuid && value =~ /\(\)/
value
else
quote(value)
else
quote(value, column)
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ def change_column(table_name, column_name, type, options = {})
def change_column_default(table_name, column_name, default)
clear_cache!
column = column_for(table_name, column_name)

execute "ALTER TABLE #{quote_table_name(table_name)} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote_default_value(default, column)}" if column
end

Expand Down
10 changes: 9 additions & 1 deletion activerecord/test/cases/adapters/postgresql/array_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def test_default_strings

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: "{}"
@connection.change_column :pg_arrays, :snippets, :text, array: true, default: []

PgArray.reset_column_information
column = PgArray.columns.find { |c| c.name == 'snippets' }
Expand All @@ -80,6 +80,14 @@ def test_change_column_cant_make_non_array_column_to_array
end
end

def test_change_column_default_with_array
@connection.change_column_default :pg_arrays, :tags, []

PgArray.reset_column_information
column = PgArray.columns_hash['tags']
assert_equal [], column.default
end

def test_type_cast_array
data = '{1,2,3}'
oid_type = @column.instance_variable_get('@oid_type').subtype
Expand Down

0 comments on commit 096be96

Please sign in to comment.