Avoid using Rails models in migration.
Reasons:
- Models might have some
lifecycle callbacks
which we dont want to be invoked in a migration. - As we know what we are doing we dont want to deal with
validations
. - Model names can change irrespective of db table.
- Try to use
raw sql
as close as possible.
Squash them after a period of time.
Reasons:
- To my experience over a period of time these migration files are painful and useless.
- Every
rake db:migrate
might take a few seconds, or creating of a new database might take a few minutes when these files are big. Consider using tools like squasher.
Make them irreversible.
Reason:
In Production it makes sense to have IrreversibleMigration as the code is tested in lower environments. If there is an issue try to fix it with a follow up migration.
Note: If there is a new developer in your team and setting up their environment. Let them do `rake db:schema:load` to get latest db copy.