I made a mistake in the migration file, deleted it manually in a hurry, and then rails db: migrate resulted in an error. At that time, NO FILE came out.
① Check the migration file Check the current status and check the ID of the migration file 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 that the ID that is NO FILE is 20200528232616
② Create a dummy file Any part of hoge is okay because the file will be deleted later.
touch db/migrate/20200528232616_hoge.rb
20200528232616_hoge.rb
class Hoge < ActiveRecord::Migrattion[5.3] #5.Match part 3 to the version of rails
def change
end
end
③ Delete the migration file
Check the current situation
$ 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
At present, the Migration Name is properly attached like this. From here, change 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 it is in the down state, delete it.
$ rm db/migrate/db/migrate/20200528232616_hoge.rb
When I check it 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 properly! !!
First of all, even if you make a mistake in the migration file, it is important not to delete it in a hurry in the up state.
Recommended Posts