1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Railsのデータベースコマンドこれさえ見れば問題なし!

Posted at

概要

Railsアプリでデータベースをmigrateする際の有用なコマンドをこの記事にまとめる。

migrateコマンドまとめ

ファイル作成コマンド

  • マイグレーションファイル作成
    rails g migration クラス名
  $ rails g migration CreateUsers
  Running via Spring preloader in process 310
    invoke  active_record
      create    db/migrate/20220913055401_create_users.rb
  • モデルファイル作成
    rails g model モデル名
  $ rails g migration User
  Running via Spring preloader in process 315
      invoke  active_record
    identical    db/migrate/20220913055401_create_users.rb
      create    app/models/user.rb

本来マイグレーションファイルも作成されますが、元々モデル名に対応するマイグレーションファイルが作成されていた場合、作成はスキップされます。

データベース作成コマンド

  • database.ymlでの設定内容に基づデータベースを作成
    rails db:create
  $ rails db:create
  Created database 'データベース名①'
  Created database 'データベース名②'
  • database.ymlでの設定内容に基づデータベースを作成及びseedデータを反映
    rails db:setup
    rails db:create と rails db:seed を行う。

マイグレーション実行コマンド

  • 未実行のマイグレーションファイルを実行
    rails db:migrate
  $ rails db:migrate
  == 20220913055401 CreateUsers: migrating ==================
  -- create_table(:users)
  == 20220913055401 CreateUsers: migrated (0.2292s) =========
  • 指定したマイグレーションファイルまで実行
    rails db:migrate VERSION=20220802084230
  $ rails db:migrate VERSION=20220802084230
  == 20220405101025 ChangeColumnUsers: migrating ====================================
  -- change_column(:users, :age, :integer)
    -> 0.2541s
  == 20220405101025 ChangeColumnUsers: migrated (0.2880s) ===========================

  == 20220405103000 ChangeColumnSubjects: migrating ====================================
  -- change_column(:subjects, :title, :string)
    -> 0.2541s
  == 20220405103000 ChangeColumnSubjects: migrated (0.2880s) ===========================

  == 20220802084230 CreateTests: migrating ====================================
  -- create_table(:tests)
    -> 0.2541s
  == 20220802084230 CreateTests: migrated (0.2880s) ===========================
  • 指定したマイグレーションファイルのみ実行
    rails db:migrate:up VERSION=20220405103000
  $ rails db:migrate:up VERSION=20220405103000
  == 20220405103000 ChangeColumnSubjects: migrating ====================================
  -- change_column(:subjects, :title, :string)
    -> 0.2541s
  == 20220405103000 ChangeColumnSubjects: migrated (0.2880s) ===========================
  • 環境毎にマイグレーションを実行
    rails db:migrate RAILS_ENV=◯◯
    RAILS_ENVの値にproduction、testを入力することで、マイグレーションをする環境を替えられる。(デフォルトはdevelopment)
  $ rails db:migrate RAILS_ENV=production
  == 20220405101025 ChangeColumnUsers: migrating ====================================
  -- change_column(:users, :age, :integer)
    -> 0.2541s
  == 20220405101025 ChangeColumnUsers: migrated (0.2880s) ===========================

  == 20220405103000 ChangeColumnSubjects: migrating ====================================
  -- change_column(:subjects, :title, :string)
    -> 0.2541s
  == 20220405103000 ChangeColumnSubjects: migrated (0.2880s) ===========================

  == 20220802084230 CreateTests: migrating ====================================
  -- create_table(:tests)
    -> 0.2541s
  == 20220802084230 CreateTests: migrated (0.2880s) ===========================

マイグレーション状況確認コマンド

  • マイグレーションファイルの実行状態一覧
    rails db:migrate:status
  $ rails db:migrate:status
  Status   Migration ID    Migration Name
--------------------------------------------------
   up     20220329033155  Create users
   up     20220329034010  Create subjects
   up     20220405101025  Change column users
   up     20220405103000  Change column subjects
   up     20220802084230  Create tests
  • スキーマのバージョンを調べる
    rails db:version
  $ rails db:version
  Current version: 20220802084230

ロールバックコマンド

  • マイグレーションを一つ前の状態に戻す
    rails db:rollback
  $ rails db:rollback
  == 20220802084230 CreateTests: reverting ====================================
  -- create_table(:tests)
    -> 0.2541s
  == 20220802084230 CreateTests: reverted (0.2880s) ===========================

  $ rails db:migrate:status
  Status   Migration ID    Migration Name
--------------------------------------------------
   up     20220329033155  Create users
   up     20220329034010  Create subjects
   up     20220405101025  Change column users
   up     20220405103000  Change column subjects
  down    20220802084230  Create tests
  • マイグレーションを◯個前の状態に戻す
    rails db:rollback STEP=◯
  $ rails db:rollback STEP=2
  == 20220405103000 ChangeColumnSubjects: reverting ====================================
  -- change_column(:subjects, :title, :string)
    -> 0.2541s
  == 20220405103000 ChangeColumnSubjects: reverted (0.2880s) ===========================

  == 20220405101025 ChangeColumnUsers: reverting ====================================
  -- change_column(:users, :age, :integer)
    -> 0.2541s
  == 20220405101025 ChangeColumnUsers: reverted (0.2880s) ===========================

  $ rails db:migrate:status
  Status   Migration ID    Migration Name
--------------------------------------------------
   up     20220329033155  Create users
   up     20220329034010  Create subjects
  down    20220405101025  Change column users
  down    20220405103000  Change column subjects
  down    20220802084230  Create tests
  • 特定のマイグレーションを未実行に戻す
    rails db:migrate:down VERSION=20220329033155
  $ rails db:rollback STEP=2
  == 20220329033155 CreateUsers: reverting ====================================
  -- create_tables(:users)
    -> 0.2541s
  == 20220329033155 CreateUsers: reverted (0.2880s) ===========================

  $ rails db:migrate:status
  Status   Migration ID    Migration Name
--------------------------------------------------
  down    20220329033155  Create users
   up     20220329034010  Create subjects
  down    20220405101025  Change column users
  down    20220405103000  Change column subjects
  down    20220802084230  Create tests

再マイグレーションコマンド

  • 直前のバージョンのロールバックと再マイグレーションを行う
    rails db:migrate:redo
  $ rails db:migrate:redo
  == 20220802084230 CreateTests: reverting ====================================
  -- create_table(:tests)
    -> 0.2541s
  == 20220802084230 CreateTests: reverted (0.2880s) ===========================

  == 20220802084230 CreateTests: migrating ====================================
  -- create_table(:tests)
    -> 0.2541s
  == 20220802084230 CreateTests: migrated (0.2880s) ===========================
  • ◯個前のバージョンまでロールバックと再マイグレーションを行う
    rails db:migrate:redo STEP=2
  $ rails db:migrate:redo STEP=2
  == 20220802084230 CreateTests: reverting ====================================
  -- create_table(:tests)
    -> 0.2541s
  == 20220802084230 CreateTests: reverted (0.2880s) ===========================

  == 20220405103000 ChangeColumnSubjects: reverting ====================================
  -- change_column(:subjects, :title, :string)
    -> 0.2541s
  == 20220405103000 ChangeColumnSubjects: reverted (0.2880s) ===========================

  == 20220405103000 ChangeColumnSubjects: migrating ====================================
  -- change_column(:subjects, :title, :string)
    -> 0.2541s
  == 20220405103000 ChangeColumnSubjects: migrated (0.2880s) ===========================

  == 20220802084230 CreateTests: migrating ====================================
  -- create_table(:tests)
    -> 0.2541s
  == 20220802084230 CreateTests: migrated (0.2880s) ===========================
  • 特定のバージョンのみロールバックと再マイグレーションを行う
    rails db:migrate:redo VERSION=20220405103000
  $ rails db:migrate:redo VERSION=20220405103000
  == 20220405103000 ChangeColumnSubjects: reverting ====================================
  -- change_column(:subjects, :title, :string)
    -> 0.2541s
  == 20220405103000 ChangeColumnSubjects: reverted (0.2880s) ===========================

  == 20220405103000 ChangeColumnSubjects: migrating ====================================
  -- change_column(:subjects, :title, :string)
    -> 0.2541s
  == 20220405103000 ChangeColumnSubjects: migrated (0.2880s) ===========================

データベースを削除及び初期化コマンド

  • データベースを削除
    rails db:drop
  $ rails db:drop
  Dropped database 'データベース名①'
  Dropped database 'データベース名②'
  • データベースを初期化する
    rails db:reset
    rails db:drop でデータベースを削除後、rails db:setup でseedデータを反映させたデータベースを作成

  • データベースを初期化及びマイグレーションを実行
    rails db:migrate:reset
    rails db:drop でデータベースを削除後、rails db:create でデータベースを作成、 rails db:migrate を行う。

最後に

改めて今回まとめた事でどういうものが出力できるか理解できました。どこか間違っている所があれば指摘して頂けると嬉しいです。

ここまで読んで下さりありがとうございました〜

参考資料

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?