Rolling back Rails migrations
There are a bunch of ways to roll back migrations, so I figured I’d capture them in Q & A format.
Let’s say the following migration files exist:
> ls db/migrate 20160613172644_migration_1 20160614173819_migration_2 20160615142814_migration_3 20160615160123_migration_4 20160615174549_migration_5
Q: How do I roll back the last migration.
Q: How do I roll back the last 3 migrations?
rake db:rollback STEP=3
Q: How do I roll back a specific migration?
rake db:migrate:down VERSION=20160615142814
The timestamp comes from the filename:
and… the one I learned today:
Q: How do I roll back all the migration past a certain version?
rake db:migrate VERSION=20160615142814.
The above will keep the following:
20160613172644_migration_1 20160614173819_migration_2 20160615142814_migration_3
and roll back the following:
In other words, it will keep all the migrations upto and including the version you specified.