0

RailsでDBのスキーマ変更をロールバックした際にエラーが発生し、ロールバックできません。

実行コマンド:

$ bundle exec rake db:rollback

エラーメッセージ:

==  RemoveIndexFromSite: reverting ======================================
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

remove_index is only reversible if given a :column option./Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration/command_recorder.rb:149:in `invert_remove_index'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration/command_recorder.rb:66:in `inverse_of'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration/command_recorder.rb:50:in `record'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration/command_recorder.rb:81:in `remove_index'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:625:in `block in method_missing'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `block in say_with_time'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `say_with_time'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:617:in `method_missing'
/Users/xf-dev-mac02/app/db/migrate/20150410022329_remove_index_from_site.rb:3:in `change'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:569:in `block in exec_migration'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:458:in `block (2 levels) in revert'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration/command_recorder.rb:38:in `revert'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:458:in `block in revert'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:605:in `suppress_messages'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:457:in `revert'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:569:in `exec_migration'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:554:in `block in migrate'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:553:in `migrate'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:709:in `migrate'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:1007:in `ddl_transaction'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:920:in `block in migrate'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `each'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `migrate'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:771:in `down'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:857:in `move'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:753:in `rollback'
/Users/xf-dev-mac02/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/railties/databases.rake:126:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:rollback
(See full trace by running task with --trace)

元々ロールバックする前にnameカラムのユニーク制約を非ユニークにしようと以下のマイグレーションファイルを実行していました。

ロールバックする前に実行したマイグレーションファイル:

class RemoveIndexFromSite < ActiveRecord::Migration
  def change
    remove_index :sites, name: 'name_index'
    add_index :sites, :name, unique: false, name: 'name_index'
  end
end

おそらく状態を元に戻せないエラーなのですが、エラーを解消する方法を知っている方がいましたら教えてください。

1 件の回答 1

2

remove_index is only reversible if given a :column option.

と書かれている通りです。以下のようにカラムを明示すればロールバックできると思います。

remove_index :sites, column: :name, name: 'name_index'
1
  • ありがとうございます!無事ロールバックできました。今回の場合マイグレーションは実行できましたがロールバックで失敗するパターンだったので注意したいと思います。
    – keitaj
    Commented 2015年4月10日 5:54

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。