[RUBY] [Beginner] I want to modify the migration file-How to use rollback-

When I first started studying, I tried to make something myself and immediately erased it if I made a mistake. What makes me particularly confused is around migration ... Rollback is a must-have for those who have always erased it.

What is rollback?

Rewind Being able to return the processing 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 date Create posts

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

You can edit it by setting the state to down. When you want to roll back 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 date Create posts

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

By the way, when you want to start over

$ rails db:rollback VERSION=0

Summary

At first, if you make a mistake, it's faster to erase it, but just remembering this will make you feel much better.

Recommended Posts

[Beginner] I want to modify the migration file-How to use rollback-
I want to use screen sharing on the login screen on Ubuntu 18
I want to use FormObject well
I want to use the Java 8 DateTime API slowly (now)
I want to use the sanitize method other than View.
I want to use DBViewer with Eclipse 2018-12! !!
I want to use @Autowired in Servlet
I want you to use Enum # name () for the Key of SharedPreference
I want to output the day of the week
I want to use arrow notation in Ruby
I want to use java8 forEach with index
I want to var_dump the contents of the intent
When you want to use the method outside
I want to truncate after the decimal point
I want to get the value in Ruby
I want you to use Scala as Better Java for the time being
[Eclipse] I want to use the completion function, but I want to manage to confirm the completion with spaces.
I want to use Combine in UIKit as well.
I want to use Clojure's convenient functions in Kotlin
[Java] I want to calculate the difference from the date
I want to use NetBeans on Mac → I can use it!
I want to embed any TraceId in the log
Tokoro I rewrote in the migration from Wicket 7 to 8
I want to use fish shell in Laradock too! !!
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
I want to judge the range using the monthly degree
I want to use a little icon in Rails
I want to know the answer of the rock-paper-scissors app
[Rails] I don't know how to use the model ...
I want to display the name of the poster of the comment
[Beginner] Discover the N + 1 problem! How to use Bullet
I want to dark mode with the SWT app
[Android Studio] I want to use Maven library on Android
I want to call the main method using reflection
[Rough commentary] I want to marry the pluck method
I want to get along with Map [Java beginner]
I want to be aware of the contents of variables!
I want to return the scroll position of UITableView!
I want to simplify the log output on Android
I want to add a delete function to the comment function
I want to set the conditions to be displayed in collection_check_boxes
[Rails] [bootstrap] I want to change the font size responsively
I want to convert characters ...
(´-`) .. oO (I want to easily find the standard output "Hello".
How to use JUnit (beginner)
I want to use Java Applet easily on the command line without using an IDE
I want to bring Tomcat to the server and start the application
I want to expand the clickable part of the link_to method
I want to change the log output settings of UtilLoggingJdbcLogger
I want to call a method and count the number
I want to create a form to select the [Rails] category
I want to put the JDK on my Mac PC
I want to give a class name to the select attribute
I want to recursively search the class list under the package
I want to distinct the duplicated data with has_many through
I want to transition to the same screen in the saved state
I want to narrow down the display of docker ps
I want to use FireBase to display a timeline like Twitter
I want to return multiple return values for the input argument
[Ruby] I want to reverse the order of the hash table
How to rollback migration files