LoginSignup
16
12

More than 3 years have passed since last update.

【Rails】マイグレーションファイルを削除する

Last updated at Posted at 2020-07-31

参考対象者

  • マイグレーションファイルの操作の仕方を知りたい方

環境

$ 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

マイグレーションファイルを削除する

Add column to usersというファイルを削除したかったとします

  • 削除したいファイルのマイグレーションIDを確認する
$ rails db:migrate:status

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20200618162841  Create tweetposts
   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
  • ID指定して、downさせる
$ rails db:migrate:down VERSION=20200627083356
  • downさせたファイルをゴミ箱へ
$ rm db/migrate/20200627083356_add_column_to_users.rb

downさせる前に、マイグレーションファイルを削除してしまったとき

********** NO FILE **********というファイルで、up状態のファイルを削除したい

  • 削除したいファイルのマイグレーションIDを確認する
$ rails db:migrate:status

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20200618162841  Create tweetposts
   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
  • 削除したマイグレーションファイル名と同名のファイルを作成する
$ touch db/migrate/20200703201452_add_column_to_users.rb
20200703201452_add_column_to_users.rb
class AddColumnToUsers < ActiveRecord::Migration
  def change
  end
end
  • downさせて、作成したマイグレーションファイルを削除する
$ rails db:migrate:down VERSION=20200703201452
$ rm db/migrate/20200703201452_add_column_to_users.rb
16
12
0

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
16
12