如何跳过失败的迁移?(耙数据库:迁移) [英] How do you skip failed migrations? (rake db:migrate)

查看:29
本文介绍了如何跳过失败的迁移?(耙数据库:迁移)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎找不到可以让我跳过迁移的选项或任何东西.

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 skip a migration that makes changes to specific user records that don't exist in my development database. I don't want to change the migration because it is not part of the source I am supposed to be working with. Is there a way to skip a migration or skip failed migrations?

提前致谢!

推荐答案

我认为您应该修复有问题的迁移以使其不那么脆弱,我猜想有几个 if 语句,也许还有一个救援就足够了.

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.

但是,如果修复迁移确实不是一种选择,您可以通过各种方式伪造它.首先,您可以注释掉迁移方法,运行 rake db:migrate,然后取消注释(或还原)有问题的迁移.

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.

您也可以在数据库中伪造它,但不推荐这种诡计,除非您知道自己在做什么并且不介意在(不可避免地)犯错误时手动修补.您的数据库中有一个名为 schema_migrations 的表,它有一个名为 versionvarchar(255) 列;db:migrate 使用此表来跟踪已应用的迁移.您需要做的就是插入适当的 version 值,rake db:migrate 会认为迁移已经完成.找到有问题的迁移文件:

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');

其中 99999999999999 当然是迁移文件名中的数字.然后运行 ​​rake db:migrate 应该跳过那个迁移.

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

我会在第三个选项之前选择第二个选项,为了完整性,我只包含hack schema_versions"选项.

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

这篇关于如何跳过失败的迁移?(耙数据库:迁移)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆