'Change multiple columns with one single migration change

I have the following migration. Is there a way to run these changes in one change than 3?

def change
 change_column :comments, :attr_1, :string, null: true
 change_column :comments, :attr_2, :string, null: true
 change_column :comments, :attr_3, :string, null: true
end


Solution 1:[1]

You can use the change_table function with bulk set to true which will run the alter as a single MySQL query and be much faster than running individually. Example is below.

def change
 change_table(:comments, bulk: true) do |t|
  t.change :attr_1, :string, null: true
  t.change :attr_2, :string, null: true
  t.change :attr_3, :string, null: true
 end
end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Kavan