[RUBY] If you want to modify database columns etc.

If you want to modify the db: migrated database

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.

Development environment

DB: MySQL Rails: 5.2.4.3

First, check the database (table) you want to modify.

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.

Next, let's bring down the database (table) that you want to modify

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.

After the correction is finished

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.

If you want to rollback all at once

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

If you want to modify database columns etc.
[PostgreSQL] If you want to delete the Rails app, delete the database first!
If you want to separate Spring Boot + Thymeleaf processing
If you want to use Mockito with Kotlin, use mockito-kotlin
If you want to recreate the instance in cloud9
If you want to study programming at university, go to Australia
[# 3 Java] Read this if you want to study Java! ~ Carefully selected ~
If you want to include the parent class in Lombok's @builder
If you want to change the Java development environment from Eclipse
When you have introduced devise but want to add more columns
If you want to use Oracle JDK 11 from September (add Amazon Corretto)
If you dare to compare Integer with "==" ...
Basic Rails commands you want to learn
If you want to dynamically embed values & add text to attribute values in Thymeleaf 3
If you want to make a zip file with Ruby, it's rubyzip.
[Swift] If you want to use a URL that includes Japanese, use addingPercentEncoding.
What to do if you install Ubuntu
What to do if you get a SQLite3 :: BusyException: database is locked error
If you want to satisfy the test coverage of private methods in JUnit
If you are using Android Room and want to change the column definition
When you want to bind InputStream in JDBI3
When you want to use the method outside
What to do if you push incorrect information
[Ruby] When you want to replace multiple characters
[rails] After option useful when you want to change the order of DB columns
If you want to know the options when configuring Ruby, see `RbConfig :: CONFIG ["configure_args "]`
What to do if you accidentally create a model
When you want to dynamically replace Annotation in Java8
If you want to make a Java application a Docker image, it is convenient to use jib.
[Swift5] What to do if you want to commit files to github but there are too many