aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2006-07-07 10:46:53 +0000
committerJeremy Kemper <jeremy@bitsweat.net>2006-07-07 10:46:53 +0000
commit044f960fd35a93159d1e0278124109dd74107670 (patch)
tree266029a4379fae25b773c384103c4b4097aa6ee5 /activerecord/lib/active_record
parent40bdbba48dfb80b824b1522fc99ca24669dea7b4 (diff)
downloadrails-044f960fd35a93159d1e0278124109dd74107670.tar.gz
rails-044f960fd35a93159d1e0278124109dd74107670.tar.bz2
rails-044f960fd35a93159d1e0278124109dd74107670.zip
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
Diffstat (limited to 'activerecord/lib/active_record')
-rw-r--r--activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb20
1 files changed, 17 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
index b307ad2fe0..745e3a45e3 100644
--- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb
@@ -325,9 +325,23 @@ module ActiveRecord
end
def add_column(table_name, column_name, type, options = {})
- execute("ALTER TABLE #{table_name} ADD #{column_name} #{type_to_sql(type, options[:limit])}")
- execute("ALTER TABLE #{table_name} ALTER #{column_name} SET NOT NULL") if options[:null] == false
- change_column_default(table_name, column_name, options[:default]) unless options[:default].nil?
+ default = options[:default]
+ notnull = options[:null] == false
+
+ # 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
def change_column(table_name, column_name, type, options = {}) #:nodoc: