Reapplying Rails DB migration

It is a quite often situation when an issue in new DB migration becomes apparent immediately after you just executed db:migrate.

Before the migration finds its way to the integration branch, fixing it in place is okay. It is much better than creating more follow-up migrations turning some trivial DB table creation into an unreadable mess.

Here is a bash oneliner to re-apply the last migration. It will revert one most recent migration and then execute db:migrate again for development and test environments:

rails db:rollback db:migrate && RAILS_ENV=test rails db:rollback db:migrate

It can live in bin/reapply_last_migration and occasionally save you a minute.



Somewhat related: