カラム修飾子を追加したマイグレーションがロールバックできないとき

% rails db:rollbackしたらエラーが出てrollbackできなかったので修正方法を書く。

rollbackコマンド後のエラーはこんな感じ

This migration uses change_column, which is not automatically reversible.
To make the migration reversible you can either:
1. Define #up and #down methods in place of the #change method.
2. Use the #reversible method to define reversible behavior.

rollbackする手順

(1)メソッド名のchangeをupにする
  def change
    change_column :reports, :emotion, :integer, default: 2, null: false
  end

# 修正↓

  def up
    change_column :reports, :emotion, :integer, default: 2, null: false
  end
(2)rails db:rollbackを実行する

これでロールバックされdownの状態になる

rails db:migrate:statusで確認する

一番したのマイグレーションがdownになった

(3) マイグレーションファイルを書き換える

upは書けているので、downでupの逆のことを書けばOK

  def up
    change_column :reports, :emotion, :integer, default: 2, null: false
  end

  def down
    change_column :reports, :emotion, :integer, default: nil, null: true
  end

最後にrails db:migrateでupにすれば完成

qiita.com