When you are on a feature branch of a Rails application with new migrations, you need to roll them back before you can resume work on another branch. However, this tends to be a tedious job and usually has to be done manually either by doing the following:
-
Running
rails db:rollback STEP=<n>
if your new migrations are all at the bottom; this may not always be the case if you pulled in changes from another branch where new migrations are added -
Checking out which versions are specific to the current branch and running
rails db:migrate:down VERSION=<version>
one by one
git rails-db-rollback
does the latter for you.
git rails-db-rollback [<branch>]
% git rails db-rollback
...migrations...
% git switch -
Switched to branch 'main'
Your branch is up to date with 'origin/main'.
% git rails db-rollback feature/another
...migrations...
% git switch feature/another
Switched to branch 'feature/another'
Your branch is up to date with 'origin/feature/another'.
% rails db:migrate
...
If no branch is given, the default branch of the origin remote is assumed, obtained via git main
.
git main