LoginSignup
164
167

More than 5 years have passed since last update.

[Rails5]migrationファイルの削除方法(DB反映前/反映後)

Posted at

はじめに

Railsでmigrationファイルを作成した後に、必要ないカラムがあって削除したいなーと思ったのでDB反映前/反映後の削除方法を調べてまとめました。

  • DB反映のmigrationファイルの削除
  • DB反映のmigrationファイルの削除(1つ前)
  • DB反映のmigrationファイルの削除(複数前)

環境

Rails 5.2.1

DB反映のmigrationファイルの削除

⇒migrationファイルを削除するだけ

例えば、下記のようなusersテーブルがあり、DB反映前のmigrationファイルを削除したい場合

schema.rb
  create_table "users", force: :cascade do |t|
    t.string "name"
    t.integer "age"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

(下はコマンドプロンプトでrails db:migrate:statusを実行した結果)
dowmはmigrationファイルの内容がDBに反映されていないという意味

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20181014050402  Create users
  down    20181019135434  Add email to users 👈DBに反映されていない

↓ downのmigrationファイルを削除

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20181014050402  Create users

DB反映のmigrationファイルの削除(1つ前)

⇒rails db:rollbackでロールバックして、migrationファイルを削除

schema.rb
  create_table "users", force: :cascade do |t|
    t.string "name"
    t.integer "age"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "email" 👈削除したい
  end
 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20181014050402  Create users
   up     20181019135434  Add email to users   👈削除したいが、DBに反映済

↓ rails db:rollback (1つ前の状態に戻す)

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20181014050402  Create users
  down    20181019135434  Add email to users   👈Statusがdownになる

↓ downのmigrationファイルを削除

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20181014050402  Create users

emailのカラムが削除されています

schema.rb
  create_table "users", force: :cascade do |t|
    t.string "name"
    t.integer "age"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
  end

DB反映のmigrationファイルの削除(複数前)

⇒ID指定でdownして、migrationファイルを削除

schema.rb
  create_table "users", force: :cascade do |t|
    t.string "name"
    t.integer "age"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "email" 👈削除したい
    t.string "address"
    t.string "job"
  end
 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20181014050402  Create users
   up     20181019135434  Add email to users   👈削除したい
   up     20181019135637  Add address to users
   up     20181019135931  Add job to users

↓ rails db:migrate:down VERSION=20181019135434
  (VERSIONに「Migration ID」を指定)

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20181014050402  Create users
  down    20181019135434  Add email to users   👈Statusがdownになる
   up     20181019135637  Add address to users
   up     20181019135931  Add job to users

↓ downのmigrationファイルを削除

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20181014050402  Create users
   up     20181019135637  Add address to users
   up     20181019135931  Add job to users

emailのカラムが削除されています

schema.rb
  create_table "users", force: :cascade do |t|
    t.string "name"
    t.integer "age"
    t.datetime "created_at", null: false
    t.datetime "updated_at", null: false
    t.string "address"
    t.string "job"
  end

まとめ

  • DB反映のmigrationファイルの削除
    ⇒migrationファイルを削除

  • DB反映のmigrationファイルの削除(1つ前)
    ⇒ロールバックしてmigrationファイルを削除

  • DB反映のmigrationファイルの削除(複数前)
    ⇒ID指定でdownして、migrationファイルを削除

練習用のモデルを作成して自分で操作すると身に付きやすかったです。

164
167
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
164
167