Rolling back a failed Rails migration

后端 未结 9 738
一向
一向 2020-12-07 17:03

How do you roll back a failed rails migration? I would expect that rake db:rollback would undo the failed migration, but no, it rolls back the previous migratio

相关标签:
9条回答
  • 2020-12-07 18:03

    To go to a specified version just use:

    rake db:migrate VERSION=(the version you want to go to)
    

    But if a migration fails part way, you'll have to clean it up first. One way would be:

    • edit the down method of the migration to just undo the part of the up that worked
    • migrate back to the prior state (where you started)
    • fix the migration (including undoing your changes to the down)
    • try again
    0 讨论(0)
  • 2020-12-07 18:05

    At 2015 with Rails 4.2.1 and MySQL 5.7, a failed migration can't be fixed with standard rake actions that Rails provide, as it was at 2009.

    MySql does not support rollback of DDL statments (at MySQL 5.7 Manual). And Rails can not do anything with that.

    Also, we can check how Rails is doing the job: A migration is wrapped in a transaction depending on how connection adapter respond to :supports_ddl_transactions?. After a search of this action at rails source (v 4.2.1), I found that only Sqlite3 and PostgreSql supports transactions, and by default it is not supported.

    Edit Thus the current answer to the original question: A failed MySQL migration must be manually fixed.

    0 讨论(0)
  • 2020-12-07 18:07

    Run just the down migration from the console:

    http://gilesbowkett.blogspot.com/2007/07/how-to-use-migrations-from-console.html (click through to his pastie)

    0 讨论(0)
提交回复
热议问题