First, find out the ID of the migration file you want to delete.
$ rake db:migrate:status
and understand the current situation.
database: petipeti_development
Status Migration ID Migration Name
--------------------------------------------------
down 2017072605442 Create gambles0
up 20170726054542 ********** NO FILE **********
down 20170726074346 Create gambles
In this case, this is the file you want to delete.
up 20170726054542 ********** NO FILE **********
Since there is no file, create a migration file anyway. The format should be migration_id_tmp.rb.
20170726054542_tmp.rb
Then, write the contents below in the 20170726054542_tmp.rb you just created.
class Tmp < ActiveRecord::Migration
def change
end
end
In the case of rails5
class Tmp < ActiveRecord::Migration[5.0]
def change
end
end
If you don’t do this, you’ll be at the terminal.
Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:
class Tmp < ActiveRecord::Migration[4.2]
and an error is displayed.
Check the status of the migration when you have completed the work up to this point.
$ rake db:migrate:status
database: petipeti_development
Status Migration ID Migration Name
--------------------------------------------------
down 2017072605442 Create gambles0
up 20170726054542 Tmp
down 20170726074346 Create gambles
You can see that the guy who was no file earlier has changed to Tmp.
Next, let’s down this migration file.
$ rake db:migrate:down VERSION=20170726054542
Check to see if you did it correctly.
$ rake db:migrate:status
database: petipeti_development
Status Migration ID Migration Name
--------------------------------------------------
down 2017072605442 Create gambles0
down 20170726054542 Tmp
down 20170726074346 Create gambles
After confirming the down, delete the migration file.
$ rails destroy migration Tmp
database: petipeti_development
Status Migration ID Migration Name
--------------------------------------------------
down 2017072605442 Create gambles0
down 20170726074346 Create gambles
It worked perfectly.