Rails migration column changes and so on.

About this article

Beginner Rails articles written by beginners for beginners

I'll forget the description of migration soon.

① Check the current migration version

② Return migration to the specified version

③ Cancel one migration that was executed immediately before

④ Change column

⑤ Add index

① Check the current migration version

rails db:migrate:status

database: first_development

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20201115064445  Create members

(2) Return migration to the specified version.

Execute the command by specifying the list of numbers (year, month, day, day) in the file name.

スクリーンショット 2020-11-15 15.58.33.png

rails db:migrate VERSION=20201115064445

③ Cancel one migration that was executed immediately before

You can use it when you realize that "Oh, there's a guy I forgot to write!"

rails db:migrate:rollback

④ Change column

python


#Command to create migration file
rails g migration rename_Column name before change_column_to_table name(Plural form)

#Contents of migration file
class Rename Column name before change ColumnTo Table name s< ActiveRecord::Migration[5.2]
  def change
    rename_column :Table name (plural), :Column name before change, :Column name after change
  end
end


④ Change column

python


#Command to create migration file
rails g migration rename_Column name before change_column_to_table name(Plural form)

#Contents of migration file
class Rename Column name before change ColumnTo Table name< ActiveRecord::Migration[5.2]
  def change
    rename_column :Table name (plural), :Column name before change, :Column name after change
  end
end


⑤ Add index

python


#Command to create migration file
rails generate migration add_index_table name_Column name

#Contents of migration file
class AddIndexTo table name< ActiveRecord::Migration
  def change
    add_index :table name,Column name
  end
end

By the way, let's explain the index.

A handyman who speeds up the search when retrieving data from a particular column. For example, I want to search for a user by name! If there is no index in the name column of the Users table, check the name column of the User table one by one from the top and try to get the data of the corresponding user. If this confirms the data of tens of thousands of people from scratch, it will take time even for programming. Therefore, by putting an index in the name column of the Users table, you can sort the names in alphabetical order and make it easier to search.

Recommended Posts

Rails migration column changes and so on.
Cancel Ruby on Rails migration
Rails migration
Rails and FormData
Tailwind on Rails
Ruby on Rails ~ Basics of MVC and Router ~
Dynamically display titles with content_for and yield-Ruby on Rails
[rails] Hide navbar and header only on TOP page
[Ruby on Rails] How to change the column name
[Ruby on Rails] Change URL id to column name
(Ruby on Rails6) How to create models and tables
[Rails] About migration files
Rails valid? And invalid?
[Rails] Migration command summary
Launch Rails on EC2
Various rails migration operations
Test run on rails
Ruby on Rails Elementary
Yay! I'm on Rails!
Ruby On Rails Association
[Ruby on Rails] Add a column with a foreign key constraint
[Ruby on Rails] Understand why Set # include? Is so fast
[Rails] Understand migration up and down by comparing with change
[Ruby on Rails] Column restrictions when saving to DB (4 representatives)
[Rails] Search method when date and time have each column
How to run React and Rails on the same server
[Ruby on Rails] Infinite scrolling using gem kaminari and jscroll