[Ruby] [Beginner] How to delete NO FILE

1 minute read

I made a mistake in the migration file, hurriedly deleted it manually, and then rails db:migrate gave me an error. NO FILE came out at that time.

How to delete

① Confirmation of migration file Check current status and migration file ID with $ rails db:migration:status.

$ rails db:migration:status

Status Migration ID Migration Name
- -------------------------------------------------
   up 20200518132414 Devise create users
   up 20200521083405 Add name to users
   up 20200525013751 Create posts
   up 20200527010431 Add picture to posts
   up 20200528232616 ********** NO FILE **********
   up 20200531083300 Create comments

Confirm the ID that is NO FILE is 20200528232616

② Create a dummy file The files will be deleted later, so any hoge part is fine.

touch db/migrate/20200528232616_hoge.rb

20200528232616_hoge.rb


 class Hoge <ActiveRecord::Migrattion[5.3] Please match the #5.3 part with the rails version.
   def change
   end
 end

③ Delete migration file

Check the current status

$ rails db:migrate:status

Status Migration ID Migration Name
- -------------------------------------------------
   up 20200518132414 Devise create users
   up 20200521083405 Add name to users
   up 20200525013751 Create posts
   up 20200527010431 Add picture to posts
   up 20200528232616 Hoge
   up 20200531083300 Create comments

Currently, the Migration Name is properly attached like this. From here, move the migration file from up to down.

$ rails db:migration:down VERSION=2020052823261
$ rails db:migrate:status

Status Migration ID Migration Name
- -------------------------------------------------
   up 20200518132414 Devise create users
   up 20200521083405 Add name to users
   up 20200525013751 Create posts
   up 20200527010431 Add picture to posts
  down 20200528232616 Hoge
   up 20200531083300 Create comments

After confirming that the status is down, delete it.

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

If you check again,

$ rails db:migrate:status

Status Migration ID Migration Name
- -------------------------------------------------
   up 20200518132414 Devise create users
   up 20200521083405 Add name to users
   up 20200525013751 Create posts
   up 20200527010431 Add picture to posts
   up 20200531083300 Create comments

It should have disappeared! !!

Summary

First of all, even if you make a mistake in the migration file, it is important that you do not panic and delete it.