[Ruby] [Beginner] Want to modify migration file-How to use rollback-

1 minute read

When I first started studying, I tried to make something myself and made a mistake and immediately erased it. The ones that make me particularly vulnerable are the migration… Rollback is something that people who have always erased should use it.

What is ###rollback Rewind Be able to return the processing up to before committing one by one

How to use

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 Dates Create posts

If it is up, you cannot read the migration file even if you edit it. 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 dates Create posts

It can be edited by setting the status to down. If you want to rollback 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 dates Create posts

It looks like this. After making it down, fix it and execute the rails db:migrate command again.

By the way, if you want to start over from the beginning

$ Rails db:rollback VERSION=0

Summary

At first, I think that if you make a mistake, it is faster to erase it, but just remembering it will make you feel much better.