[Ruby] [Rails] Delete migration file

1 minute read

Reference target person

  • Those who want to know how to operate the migration file

environment

$ rails -v
Rails 6.0.3.1
$ ruby -v
ruby 2.7.0p0 (2019-12-25 revision 647ee6f091) [x86_64-darwin19]
$ mysql --version
mysql Ver 14.14 Distrib 5.7.29, for osx10.15 (x86_64) using EditLine wrapper

Delete migration file

Suppose you want to delete the file Add column to users

  • Confirm the migration ID of the file you want to delete
$ rails db:migrate:status

 Status Migration ID Migration Name
- -------------------------------------------------
   up 20200618162841 Create tweet posts
   up 20200620004226 Change tweetposts to tweets
  down 20200621075518 Create posts
  down 20200623102444 Change posts to chats
  down 20200627042358 Create users
   up 20200627080839 Create users
   up 20200627083356 Add column to users
  down 20200627220915 Change datatype content of chats
  down 20200710035709 Add user id to tweets
  • Specify ID and bring down
$ rails db:migrate:down VERSION=Add column to users
  • down file to trash
$ rm db/migrate/20200703201452_add_column_to_users.rb

If you delete the migration file before #down

I want to delete the file in the up state in the file ********** NO FILE **********

  • Confirm the migration ID of the file you want to delete
$ rails db:migrate:status

 Status Migration ID Migration Name
- -------------------------------------------------
   up 20200618162841 Create tweet posts
   up 20200620004226 Change tweetposts to tweets
  down 20200621075518 Create posts
  down 20200623102444 Change posts to chats
  down 20200627042358 Create users
   up 20200627080839 Create users
   up 20200627083356 Add column to users
  down 20200627220915 Change datatype content of chats
   up 20200703201452 ********** NO FILE **********
  down 20200710035709 Add user id to tweets
  • Create a file with the same name as the deleted migration file name
$ touch db/migrate/20200703201452_add_column_to_users.rb

20200703201452_add_column_to_users.rb


class AddColumnToUsers <ActiveRecord::Migration
  def change
  end
end
  • down and delete the created migration file
$ rails db:migrate:down VERSION=20200703201452
$ rm db/migrate/20200703201452_add_column_to_users.rb