[RUBY] [Rails] Migration command summary

Development environment

・ Ruby: 2.5.7 Rails: 5.2.4 ・ Vagrant: 2.2.7 -VirtualBox: 6.1 ・ OS: macOS Catalina

How to write a migration command

$ rails g migration ChangeColumnToBooks is Same as writing $ rails generation migration change_column_to_books.

In other words, generation can be abbreviated as g, Like ʻAddBodyToBooks`, you can save the trouble of writing" _ "by capitalizing the beginning of a word.

Basic commands

1. Create model and table

$ rails g model model name column name: type name

Terminal


$ rails g model Book title:string

migrate/~_create_books.rb


class CreateBooks < ActiveRecord::Migration[5.2]
  def change
    create_table :books do |t|
      t.string :title

      t.timestamps
    end
  end
end

2. Delete model and table

rails d model model name

Terminal


$ rails d model Book

3. Execute migration

Terminal


$ rails db:migrate

4. Return the contents of the migration

** ① When going back one step **

Terminal


$ rails db:rollback

** ① When returning to multiple steps **

Terminal


$ rails db:rollback STEP=5 #Numbers can be changed freely

4. Check migration status

Terminal


rails db:migrate:status

Table relation

1. Delete only the table

$ rails g migration Drop table name

Terminal


$ rails g migration DropBooks

migrate/~_drop_books.rb


class DropBooks < ActiveRecord::Migration[5.2]
  def change
    drop_table :books #Postscript
  end
end

2. Change the table name

$ rails g migration Rename Table name before change To Table name after change

Terminal


$ rails g migration RenameBooksToArticles

migrate/~_rename_books_to_articles.rb


class RenameBooksToArticles < ActiveRecord::Migration[5.2]
  def change
    rename_table :books, :articles #Postscript
  end
end

Column relation

1. Add column

** ① Single **

$ rails g migration Add Column name To Table name Column name: Type name

Terminal


$ rails g migration AddBodyToBooks body:text

migrate/~_add_body_to_books.rb


class AddBodyToBooks < ActiveRecord::Migration[5.2]
  def change
    add_column :books, :body, :text
  end
end

** ② Multiple **

$ rails g migration AddColumnsTo Table name Column name: Type name Column name: Type name Column name: Type name

Terminal


$ rails g migration AddColumnsToBooks body:text introduction:text price:integer

migrate/~_add_columns_to_books.rb


class AddColumnsToBooks < ActiveRecord::Migration[5.2]
  def change
    add_column :books, :body, :text
    add_column :books, :introduction, :text
    add_column :books, :price, :integer
  end
end

2. Delete column

** ① Single **

$ rails g migration Remove Column name From table name Column name: Type name

Terminal


$ rails g migration RemoveTitleFromBooks title:string

migrate/~_remove_title_from_books.rb


class RemoveTitleFromBooks < ActiveRecord::Migration[5.2]
  def change
    remove_column :books, :title, :string
  end
end

** ② Multiple **

$ rails g migration RemoveColumnsFrom table name column name: type name column name: type name column name: type name

Terminal


$ rails g migration RemoveColumnsFromBooks body:text introduction:text price:integer

migrate/~_remove_columns_from_books.rb


class RemoveColumnsFromBooks < ActiveRecord::Migration[5.2]
  def change
    remove_column :books, :body, :text
    remove_column :books, :introduction, :text
    remove_column :books, :price, :integer
  end
end

3. Change column data type

$ rails g migration ChangeData Column name To Table name Column name: Type name

Terminal


$ rails g migration ChangeDataTitleToBooks

migrate/~_change_data_title_to_books.rb


class ChangeDataTitleToBooks < ActiveRecord::Migration[5.2]
  def change
    change_column :books, :title, :text #Postscript
  end
end

4. Added column options

$ rails g migration ChangeOption Column name To Table name Column name: Type name

Terminal


$ rails g migration ChangeOptionTitleToBooks

migrate/~_change_option_title_to_books.rb


class ChangeOptionTitleToBook < ActiveRecord::Migration[5.2]
  def change
    change_column :books, :title, :string, null: false #Postscript
  end
end

5. Data type list

Model name role
string Short string
text Long string
integer integer
float Floating point
decimal Highly accurate decimal
datetime Date and time
timestamp Time stamp
time time
date date
binary Binary string
boolean Boolean value

6. Frequently used options

Option name role
default Set initial value
null The truth of the blank
limit Limit length
unique Give unique constraints
unique Give index

Recommended Posts

[Rails] Migration command summary
[Rails] rails db command summary
Rails migration
Heroku command summary
Docker command summary
Rails 6.0 Routing Summary
rails db: 〇〇 Summary
[Docker] Command summary
[Rails] About migration files
[Rails] devise-related routing summary
Various rails migration operations
rails error resolution summary
Understand migration in rails
docker command personal summary
rails new application launch command
docker-compose command list (for Rails)
[rails] Deleted NOFILE migration file
Rails Tutorial (4th Edition) Summary
Cancel Ruby on Rails migration
Ruby on Rails validation summary
[Rails] Delete the migration file
Introduction to RSpec-Everyday Rails Summary-
Database command for migration file environment
Ruby on Rails Overview (Beginner Summary)
[Rails Struggle/Rails Tutorial] Summary of Rails Tutorial Chapter 2
Summary of rails validation (for myself)
Ruby on Rails variable, constant summary
rails tutorial chapter 10 summary (for self-learning)
[Rails] Summary of complicated routing configurations
Check the migration status of rails
Summary
[Rails] AWS deployment error encounter summary
Summary of basic migration knowledge rails db: rollback and column addition / deletion
Rails Tutorial 6th Edition Learning Summary Chapter 10
Rails Tutorial 6th Edition Learning Summary Chapter 7
Migration file to add comment to Rails table
Rails Tutorial 6th Edition Learning Summary Chapter 4
Rails Tutorial 6th Edition Learning Summary Chapter 9
Rails Tutorial 6th Edition Learning Summary Chapter 6
[Ruby on Rails] model, controller terminal command
Ruby on Rails model creation / deletion command
[Rails Struggle/Rails Tutorial] Summary of Heroku commands
Rails Tutorial 6th Edition Learning Summary Chapter 5
Rails Tutorial 6th Edition Learning Summary Chapter 2
Ruby on Rails controller create / delete command
Ruby on Rails application new creation command
Rails migration column changes and so on.
Rails: A little summary about data types
Rails Tutorial 6th Edition Learning Summary Chapter 3
[Rails] Method summary for conversion / verification / search
Rails Tutorial 6th Edition Learning Summary Chapter 8