I wanted to modify the database while learning team development at a programming school. I missed the knowledge of the details, so I will summarize it for memorandum.
I hope it will be helpful for those who are not familiar with database operations and those who are learning databases in the future.
DB: MySQL Rails: 5.2.4.3
Terminal
% rails db:migrate:status
Then, I think that the tables will come out like this (the number and names of the tables that come out will be different)
Terminal
Status Migration ID Migration Name
--------------------------------------------------
up 20200823051138 Devise create ----s
up 20200824122031 Create -------s
up 20200824122659 Add ancestry to ------s
up 20200824123715 Create -----s
up 20200829083145 Create -----s
up 20200906141656 Create -----s I want to modify this table this time
up 20200907114227 Create -----s
down 20200927061950 Create -----s
down 20200927065357 Create -----s
※----Is the table name you created
Pay attention to up and down here.
To fix the migration, the status must be down.
To bring it down, run a command like this in your terminal
Terminal
% rails db:rollback
Let's check the status again
Terminal
% rails db:migrate:status
Terminal
Status Migration ID Migration Name
--------------------------------------------------
up 20200823051138 Devise create ----s
up 20200824122031 Create -------s
up 20200824122659 Add ancestry to ------s
up 20200824123715 Create -----s
up 20200829083145 Create -----s
up 20200906141656 Create -----s I want to modify this table this time
down 20200907114227 Create -----s
down 20200927061950 Create -----s
down 20200927065357 Create -----s
※----Is the table name you created
that?
Only one down has changed to down.
This is because the rollback command can only be turned down one by one.
So let's try again.
Terminal
% rails db:rollback
Let's check the status again
Terminal
% rails db:migrate:status
Terminal
Status Migration ID Migration Name
--------------------------------------------------
up 20200823051138 Devise create ----s
up 20200824122031 Create -------s
up 20200824122659 Add ancestry to ------s
up 20200824123715 Create -----s
up 20200829083145 Create -----s
down 20200906141656 Create -----s I want to modify this table this time
down 20200907114227 Create -----s
down 20200927061950 Create -----s
down 20200927065357 Create -----s
※----Is the table name you created
This time, I was able to bring down the target table safely.
This time I wanted to correct the column name, so I changed the column name in the migration file after this.
Finally
Terminal
% rails db:migrate
Let's check the status again
Terminal
% rails db:migrate:status
Terminal
Status Migration ID Migration Name
--------------------------------------------------
up 20200823051138 Devise create ----s
up 20200824122031 Create -------s
up 20200824122659 Add ancestry to ------s
up 20200824123715 Create -----s
up 20200829083145 Create -----s
up 20200906141656 Create -----s Modified table
up 20200907114227 Create -----s
up 20200927061950 Create -----s
up 20200927065357 Create -----s
※----Is the table name you created
For the rails db: migrate command, change all down tables to up.
db: migrate is all up at once You can only down db: rollback one by one.
I will also introduce a method that can be done collectively when you have to rollback multiple times like this time
Terminal
% rails db:rollback STEP=2
Once you get used to the rollback command, use the STEP option positively to improve your workability.
Recommended Posts