[Performance Improvement Idea] Add multiple columns to table using single ALTER statement

To add 2 new cols to existing table in migration, we will have to do:

add_column :my_table, :col1, :string
add_column :my_table, :col2, :string

this will create 2 ALTER table statements.

Alter table my_table add column col1 varchar(255) Alter table my_table add column col2 varchar(255)

I don’t know about all databases, but atleast MySQL doesn’t seem to modify the table “in place”. MySQL actually creates a new table, as copy of the old table with the specified modifications.Two separate statements would require MySQL to do that copy operation twice.so I am wondering can we add support for adding multiple columns to table using single ALTER TABLE? if yes, then I see following approches

  1. Add support to accept multiple cols, in add_column[1]

  2. Introduce a new method add_columns

  3. Internal optimisation, if there are multiple add_column calls to same table in a migration, consolidate them & generate single ALTER TABLE statement. not sure if that is possible?

If you any thoughts on this Performance Improvement Idea, let me know

–Gaurish

[1] ActiveRecord::ConnectionAdapters::SchemaStatements

I believe change_table, perhaps in combination with the bulk option, will do what you want.

http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/SchemaStatements.html#method-i-change_table