LoginSignup
63
38

More than 1 year has passed since last update.

Ruby on Rails | drop_tableでテーブル削除マイグレーションする時の良い子・悪い子・普通の子

Last updated at Posted at 2018-06-18

良い子

rollback した時に、元と同じ構造のテーブルが生成されるようにする。
(元のデータは失われるが)

class DropUsers < ActiveRecord::Migration
  def change
    drop_table :users do |t|
      t.string :email, null: false
      t.timestamps null: false
    end
  end
end

悪い子

捨てて省みぬ。

class DropUsers < ActiveRecord::Migration
  def change
    drop_table :users
  end
end

普通な子

不可逆であることを明記する。

class DropUsers < ActiveRecord::Migration
  def up
    drop_table :users
  end

  def down
    fail ActiveRecord::IrreversibleMigration
  end
end

その他

マイグレーションをrevertする方法もあるらしい。

drop_tableの代わりにActiveRecordMigration#revert - kikeda1104's blog

参考

チャットメンバー募集

何か質問、悩み事、相談などあればLINEオープンチャットもご利用ください。

Twitter

63
38
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
63
38