LoginSignup
142
130

More than 3 years have passed since last update.

Ruby on Rails 『NO FILEのmigrationを削除する方法』

Last updated at Posted at 2019-09-15

状態

$ rails db:migrate 実行後のマイグレーションファイルを削除してしまい、NO FILEのファイルが出来上がった。

解決方法

migrationの状態を確認。

$ rails db:migrate:status

下記のように、migrationの一覧が表示されます。
upが$ rails db:migrate実行後。
downが$ rails db:migrate実行前。

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20190915023701  Create posts
   up     20190915065320  ********** NO FILE **********
  down    20190915080932  Devise create users

Migration IDをコピペし、ダミーファイルを作成。(後で削除するので、hogeのところは適当でいいです。)

$ touch db/migrate/20190915065320_hoge.rb

下記のように記述したダミーファイルを作っておく。

20190915065320_hoge.rb
class Hoge < ActiveRecord::Migration[6.0]
  def change
  end
end

$ rails db:migrate:status を実行してMigration Nameが付与されていることを確認。

Status   Migration ID    Migration Name
--------------------------------------------------
   up     20190915023701  Create posts
   up     20190915065320  Hog
  down    20190915080932  Devise create users

VERSION にMigration ID を代入して下コマンドを実行。

$ rails db:migrate:down VERSION=20190915065320

これでdown(rails db:migrate実行前)になります。

Status   Migration ID    Migration Name
--------------------------------------------------
   up     20190915023701  Create posts
  down    20190915065320  Hoge
  down    20190915080932  Devise create users

↓ 必要なくなったので削除。

$ rm db/migrate/db/migrate/20190915065320_hoge.rb

あとは先程と同じく、$ rails db:migrate:status を実行すると消えてるのが確認できる。

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20190915023701  Create posts
  down    20190915080932  Devise create users

 
以上がNO FILEのmigrationを削除する方法でした。

そもそも、つまらないミスをしなければこのような作業はしなくていいのですが...笑

参考資料

railsのrakeで作成したmigrationファイルとstatus履歴を削除する

142
130
3

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
142
130