From 5a276643d163b675dea459071824deae3ebec9f2 Mon Sep 17 00:00:00 2001 From: Michael Koziarski Date: Sun, 7 Oct 2007 04:52:09 +0000 Subject: Allow change_column to set NOT NULL in the PostgreSQL adaptor. Closes #3904 [tarmo] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7766 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../connection_adapters/postgresql_adapter.rb | 29 +++++++++------------- 1 file changed, 12 insertions(+), 17 deletions(-) (limited to 'activerecord/lib/active_record') diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 786ad1c864..331f331de7 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -579,22 +579,11 @@ module ActiveRecord default = options[:default] notnull = options[:null] == false - quoted_column_name = quote_column_name(column_name) - # Add the column. - execute("ALTER TABLE #{table_name} ADD COLUMN #{quoted_column_name} #{type_to_sql(type, options[:limit])}") + execute("ALTER TABLE #{table_name} ADD COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit])}") - # Set optional default. If not null, update nulls to the new default. - if options_include_default?(options) - change_column_default(table_name, column_name, default) - if notnull - execute("UPDATE #{table_name} SET #{quoted_column_name}=#{quote(default, options[:column])} WHERE #{quoted_column_name} IS NULL") - end - end - - if notnull - execute("ALTER TABLE #{table_name} ALTER #{quoted_column_name} SET NOT NULL") - end + change_column_default(table_name, column_name, default) if options_include_default?(options) + change_column_null(table_name, column_name, false, default) if notnull end # Changes the column of a table. @@ -612,9 +601,8 @@ module ActiveRecord commit_db_transaction end - if options_include_default?(options) - change_column_default(table_name, column_name, options[:default]) - end + 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) end # Changes the default value of a table column. @@ -622,6 +610,13 @@ module ActiveRecord execute "ALTER TABLE #{table_name} ALTER COLUMN #{quote_column_name(column_name)} SET DEFAULT #{quote(default)}" end + def change_column_null(table_name, column_name, null, default = nil) + unless null || default.nil? + execute("UPDATE #{table_name} SET #{quote_column_name(column_name)}=#{quote(default)} WHERE #{quote_column_name(column_name)} IS NULL") + end + execute("ALTER TABLE #{table_name} ALTER #{quote_column_name(column_name)} #{null ? 'DROP' : 'SET'} NOT NULL") + end + # Renames a column in a table. def rename_column(table_name, column_name, new_column_name) execute "ALTER TABLE #{table_name} RENAME COLUMN #{quote_column_name(column_name)} TO #{quote_column_name(new_column_name)}" -- cgit v1.2.3