aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorMichael Koziarski <michael@koziarski.com>2007-10-07 04:52:09 +0000
committerMichael Koziarski <michael@koziarski.com>2007-10-07 04:52:09 +0000
commit5a276643d163b675dea459071824deae3ebec9f2 (patch)
treef74ee2aa6d7df3101fdc3a769a315d97218210e2 /activerecord/lib/active_record
parent907d6ce28587593ec2bc93b4aeb216ca9d583a95 (diff)
downloadrails-5a276643d163b675dea459071824deae3ebec9f2.tar.gz
rails-5a276643d163b675dea459071824deae3ebec9f2.tar.bz2
rails-5a276643d163b675dea459071824deae3ebec9f2.zip
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
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb29
1 files changed, 12 insertions, 17 deletions
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)}"