diff options
author | Timothy N. Tsvetkov <timothy.tsvetkov@gmail.com> | 2011-02-12 21:41:03 +0300 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-02-15 10:52:14 -0800 |
commit | 09ccdc3737144e21f090ab658b160b91e1a4a691 (patch) | |
tree | e17728faf6eec0bcef05699d4df9aedd32610d9e /activerecord | |
parent | f83218561e41f2309e2cbbcf4cf18eacf6322d61 (diff) | |
download | rails-09ccdc3737144e21f090ab658b160b91e1a4a691.tar.gz rails-09ccdc3737144e21f090ab658b160b91e1a4a691.tar.bz2 rails-09ccdc3737144e21f090ab658b160b91e1a4a691.zip |
Optimization for postgres adapter for add_column method. Set default and not null in one query
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index 20be4a22ea..576450bc3a 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -845,14 +845,18 @@ module ActiveRecord # Adds a new column to the named table. # See TableDefinition#column for details of the options you can use. def add_column(table_name, column_name, type, options = {}) - default = options[:default] - notnull = options[:null] == false + add_column_sql = "ALTER TABLE #{quote_table_name(table_name)} ADD COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}" + add_column_options!(add_column_sql, options) - # Add the column. - execute("ALTER TABLE #{quote_table_name(table_name)} ADD COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}") + begin + execute add_column_sql + rescue ActiveRecord::StatementInvalid => e + raise e if postgresql_version > 80000 - change_column_default(table_name, column_name, default) if options_include_default?(options) - change_column_null(table_name, column_name, false, default) if notnull + execute("ALTER TABLE #{quote_table_name(table_name)} ADD COLUMN #{quote_column_name(column_name)} #{type_to_sql(type, options[:limit], options[:precision], options[:scale])}") + 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 end # Changes the column of a table. |