简体   繁体   中英

rails migration doesn't work with an empty down function

I generated a rails 3.2 migration with an empty down function, because the migration is irreversible (and I don't want to throw an exception). I run the migration successfully, but it has no effect. When I rollback, and run db:migrate again, the effects does apply.

I solved this easily by filling the empty down function with a code which does nothing, but it's still pretty ugly.

Does anyone knows why this happens? Is this a rails bug?

The exception is throwed to prevent destroying your database, if its irreversable, then that's probably the right thing to do. Your #down could look like this:

  def down
    raise ActiveRecord::IrreversibleMigration, "Explain why its irreversable!"
  end

That will save others a lot of headache as it clearly notifies about irreversable migration and explains the reason behind it:)

EDIT: I cannot confirm this behaviour for Rails 3.2.3. I've created several different migrations without #down , and no exceptino was raised. Maybe it's something in your code, which you didn't show a bit.

EDIT 2: Just to recap, when you're using up/down method, its your responsibility to raise ActiveRecord::IrreversibleMigration . In other case, nothing will happen ( #down defined in AR will just return nil). The behaviour is different when you use #change . In some cases the mentioned exception can be raised by #inverse defined here: https://github.com/rails/rails/blob/565bfb9cd49285ebaa170141b4996c22ba81de43/activerecord/lib/active_record/migration/command_recorder.rb#L39 which is expected behaviour.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM