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

PostgreSQL: add_column can add a not null column with a default.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4575 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper 2006-07-07 10:46:53 +00:00
parent 40bdbba48d
commit 044f960fd3

View file

@ -325,9 +325,23 @@ module ActiveRecord
end end
def add_column(table_name, column_name, type, options = {}) def add_column(table_name, column_name, type, options = {})
execute("ALTER TABLE #{table_name} ADD #{column_name} #{type_to_sql(type, options[:limit])}") default = options[:default]
execute("ALTER TABLE #{table_name} ALTER #{column_name} SET NOT NULL") if options[:null] == false notnull = options[:null] == false
change_column_default(table_name, column_name, options[:default]) unless options[:default].nil?
# Add the column.
execute("ALTER TABLE #{table_name} ADD COLUMN #{column_name} #{type_to_sql(type, options[:limit])}")
# Set optional default. If not null, update nulls to the new default.
unless default.nil?
change_column_default(table_name, column_name, default)
if notnull
execute("UPDATE #{table_name} SET #{column_name}='#{default}' WHERE #{column_name} IS NULL")
end
end
if notnull
execute("ALTER TABLE #{table_name} ALTER #{column_name} SET NOT NULL")
end
end end
def change_column(table_name, column_name, type, options = {}) #:nodoc: def change_column(table_name, column_name, type, options = {}) #:nodoc: