[Rails] Addition of columns / Change of data type / column name

Introduction

A typical procedure for adding columns and changing column names and data types is 1 ⃣ Check the current status with rails db: migrate: status 2 ⃣ Enter a command to create or add a migration file in the terminal (the command differs depending on the operation you want to perform) 3 ⃣ Describe the changed contents in the migration file (If you add it, you only need to confirm the contents) 4⃣rails db:migrate I think (I'm sorry if it's different). Of course, I think this is also the correct answer. (I also use rails db: rollback) I think that the table may disappear mysteriously every time I check the command or make a typo ...

** There seems to be an easy way if the contents of the record can be erased, (I'm sorry if you know) **

Easy method above (multiple changes are possible at the same time)

1 ⃣ rails db: migrate: status to check the current status (if there is no problem, go to 2 ⃣) 2 ⃣ Directly rewrite the part of the migration file that you have already created that you want to change (please add if you want to add it) 3 ⃣ Execute rails db: migrate: reset to complete Check out schema.rb!

rails db:migrate:By resetting the existing migration file**Use all**And recreate the table. Therefore, all the rewritten parts and added parts will be reflected.(Even if an error occurs due to a typographical error, it will be mostly fixed.)


 However, if you do not want to delete the record, you need to use it properly depending on the case, so type the command carefully.

## Example
#### 1 ⃣ Status check

#### **`rails db:migrate:status execution`**

There are as many migration files as are output when the command is executed, and the image will be such that they will be migrated again all at once in the subsequent process.

Terminal


vocstartsoft:~/environment/bookers2-task (master) $ rails db:migrate:status #Run here

database: /home/ec2-user/environment/bookers2-task/db/development.sqlite3

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20200830060820  Devise create users      #The state will come out whether it is up or down.
   up     20200830062142  Create books             #up is basically migrated
   up     20201101080413  Create book comments
  #Omitted below

2 ⃣ Addition / change to migration file

Bring the migration file containing the contents you want to add / change from the ** db / migrate ** folder. This time, both the ** title column ** and ** body column ** used for posting have incorrect data types, so I'll fix that (what kind of mistake is boolean?)

db/migrate/20201115102020_create_books.rb


class CreateBooks < ActiveRecord::Migration[5.2]
  def change
    create_table :books do |t|
      t.integer :title          #← I want to set integer to string
      t.boolean :body           #← I want to change boolean to text
      t.integer :user_id
      t.timestamps
    end
  end
end

Rewrite (please add if you want to add)

db/migrate/20201115102020_create_books.rb


      #↑ Omitted
      t.string :title          #integer→string
      t.text :body             #boolean→text
      #↓ Omitted

3 ⃣ Rails db: migrate: reset to delete DB at once → create

Terminal


$ rails db:migrate:reset

If the terminal outputs the same content as usual rails db: migrate, there is probably no problem. Check schema.rb to see if you have made any changes or additions. It seems to be tedious, but please note that the user information (name, etc.) and posted content that you created should have disappeared.

bonus

About the difference between rails db: reset and rails db: migrat: reset

Both commands delete the DB and recreate it, but there seems to be a big difference.

rails db:reset drops the DB and the current scheme.Load rb and recreate the DB. db/migrate/~.rb doesn't seem to be used.



#### **`rails db:migrate:reset will migrate as usual after dropping the DB (db:migrate) is performed. That is, db/migrate/~.All rbs are executed from oldest to newest.`**

Finally, other than the above methods [Click here for column addition articles] (https://qiita.com/jackie0922youhei/items/09a7b081e40506f07358) It was helpful, and on the contrary, [Click here for an article about the contents when rails db: migrate: reset could not be done] (https://qiita.com/mom0tomo/items/a252ff8a42eea00f81b1) It's been a long time, but I personally feel that there are a lot of complicated errors around the DB, so I'm glad if this article solves it! (I'm sorry if it doesn't improve ...)

Recommended Posts

[Rails] Addition of columns / Change of data type / column name
[Rails] How to change the column name of the table
[Rails] Change the label name of f.label
[Ruby on Rails] How to change the column name
[Ruby on Rails] Change URL id to column name
Utilization of Rails Boolean type
Summary of basic migration knowledge rails db: rollback and column addition / deletion
[Rails] How to create a table, add a column, and change the column type
[Ruby on Rails] Introduction of initial data
[Rails] Addition of Ruby On Rails comment function
How to change app name in rails
[Rails] Temporary retention of data by session
Rails Addition of easy and easy login function
[Rails] Get the path name of the URL before the transition and change the link destination
[Order method] Set the order of data in Rails
[Rails] I learned about database data type types!
[Rails] Display of multi-layered data using select boxes
[rails] After option useful when you want to change the order of DB columns