[Ruby] [Rails] Change migrate file (rails db:rollback)

1 minute read

Hello, this is tt_tsutsumi. This time I will explain how to change the migrate file. I hope this article helps some of you.

When changing or adding columns during application creation I was confused because it made a difference from how to write ordinary code. At that time, I will describe the process and flow I did.

1. rails db:migrate:status

First, let’s check the operating status of migrate of the application we are currently creating. Enter the following code on the console.

$rails db:migrate:status

Status Migration ID Migration Name
- -------------------------------------------------
   up year/month/day Devise create users
   up year/month/day Create spots

*The date created by yourself is displayed in the year/month/day part.

If the status part is up in the above, the migrate file is operating. At this time, changes or additions to the migrate file will not be reflected.

2. rails db:rollback

The operating status of the migrate file is brought down.

$rails db:rollback
$rails db:migrate:status

Status Migration ID Migration Name
- -------------------------------------------------
   up year/month/day Devise create users
   down year/month/day Create spots

The key here is rails db:rollback!! You can bring down the running migate file by hitting this code.

*Note that this code can only down one file at a time.

3. rails db:migrate

Please confirm that it is down and change or delete the migrate file. Then save the changes and run rails db:migrate.

$rails db:migrate
$rails db:migrate:status

Status Migration ID Migration Name
- -------------------------------------------------
   up year/month/day Devise create users
   up year/month/day Create spots

This completes modifying and saving the migrate file. Thank you for visiting !!