I created the following Active Record migration, which adds and removes some indexes.
class FixIndexes < ActiveRecord::Migration
def change
add_index :table1, :field1, :unique => true
remove_index :table2, :name => "index_table2_on_field1"
remove_index :table2, :name => "index_table2_on_field2"
remove_index :table3, :name => "index_table3_on_field1"
add_index :table3, [:field1, :field2]
end
end
When I start the migration ( $ bundle exec rake db:migrate), it works fine as expected.
Unfortunately, when I try to return the migration ( $ bundle exec rake db:rollback), it does not work and throws an exceptionActiveRecord::IrreversibleMigration
== FixIndexes: reverting =====================================================
rake aborted!
An error has occurred, all later migrations canceled:
ActiveRecord::IrreversibleMigration/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.14/lib/active_record/migration/command_recorder.rb:42:in `block in inverse'
/usr/local/rvm/gems/ruby-1.9.3-p392/gems/activerecord-3.2.14/lib/active_record/migration/command_recorder.rb:40:in `map'
My questions:
- Why is this irreversible migration? It is just adding and removing some indexes, not data.
- Will this fix the problem if I use
def self.upand def self.down
instead def change? - How can I undo this change without manually adding or removing indexes from MySQL?
source
share