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

1 minute read

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

https://qiita.com/tt_tsutsumi/items/ea8bc346d5b9cb21e05b

↑↑↑

You can change the migrate file etc. in the last article. In that, there was a story that you can only down one file at a time, This time, I will describe how to bring down any file at once.

1. rails db:migrate:status

First of all, let’s check the operating status of migrate of the application we are currently creating, as in the previous time. 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

2. rails db:rollback STEP=2

Execute by adding STEP=2 after the last rails db:rollback. The meaning of this code is to down** the file from the current file to **2 steps back. Execution will be as below.

$rails db:rollback STEP=2
$rails db:migrate:status

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

If you want to change or delete multiple files at once, I think it’s faster to use STEP. However, if you do not use it after checking, it may be down, so be careful.

3. rails db:migrate

When you have finished making changes, save and run rails db:migrate. Even if you return several steps with STEP, it will be changed to up with one 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 the batch change and save of the migrate file. Thank you for visiting !!