How do you skip failed migrations? (rake db:migrate)

前端 未结 7 1683
忘了有多久
忘了有多久 2021-01-30 16:32

I can\'t seem to find an option or anything that allows me to skip migrations.

I know what you\'re thinking: \"you should never have to do that...\"

I need to sk

7条回答
  •  被撕碎了的回忆
    2021-01-30 17:12

    I think you should fix the offending migrations to be less fragile, I'd guess that a couple of if statements and perhaps a rescue would be sufficient.

    But, if fixing the migrations really isn't an option, you can fake it in various ways. First of all, you could just comment out the migration methods, run rake db:migrate, and then uncomment (or revert) the offending migration.

    You can also fake it inside the database but this sort of chicanery is not recommended unless you know what you're doing and you don't mind manually patching things up when you (inevitably) make a mistake. There is a table in your database called schema_migrations that has a single varchar(255) column called version; this table is used by db:migrate to keep track of which migrations have been applied. All you need to do is INSERT the appropriate version value and rake db:migrate will think that the migration has been done. Find the offending migration file:

    db/migrate/99999999999999_XXXX.rb
    

    then go into your database and say:

    insert into schema_migrations (version) values ('99999999999999');
    

    where 99999999999999 is, of course, the number from the migration's file name. Then running rake db:migrate should skip that migration.

    I'd go with the second option before the third, I'm only including the "hack schema_versions" option for completeness.

提交回复
热议问题