When I first started studying, I tried to make something myself and immediately erased it if I made a mistake. What makes me particularly confused is around migration ... Rollback is a must-have for those who have always erased it.
Rewind Being able to return the processing before committing one by one
Oh, when I think I made a mistake First, let's look at the current situation.
$ rails db:migrate:status
Status Migration ID Migration Name
--------------------------------------------------
up date Devise create users
up Date Add name to users
up date Create posts
If it is up, it cannot be read even if you edit the migration file. Therefore,
$ rails db:rollback
$ rails db:migrate:status
Status Migration ID Migration Name
--------------------------------------------------
up date Devise create users
up Date Add name to users
down date Create posts
You can edit it by setting the state to down. When you want to roll back multiple migration files at the same time
$ rails db:rollback STEP=2
$ rails db:migrate:status
Status Migration ID Migration Name
--------------------------------------------------
down date Devise create users
down date Add name to users
down date Create posts
It looks like this. After making it down, fix it and execute the rails db: migrate command again.
By the way, when you want to start over
$ rails db:rollback VERSION=0
At first, if you make a mistake, it's faster to erase it, but just remembering this will make you feel much better.
Recommended Posts