The following example adds a column admin
to the users
table, and gives that column the default value false
.
class AddDetailsToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :admin, :boolean, default: false
end
end
Migrations with defaults might take a long time in large tables with for example PostgreSQL. This is because each row will have to be updated with the default value for the newly added column. To circumvent this (and reduce downtime during deployments), you can split your migration into three steps:
add_column
-migration similar to the one above, but set no defaultchange_column
migration, which then changes the default of that column to the desired default value