解决Rails孤儿迁移的最佳方法是什么? [英] What is the best way to resolve Rails orphaned migrations?

查看:87
本文介绍了解决Rails孤儿迁移的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在项目中的分支之间切换,每个分支都有不同的迁移......这是场景:


$ rake db:migrate:status



 状态迁移ID迁移名称
--- -----------------------------------------------
...
up 20130307154128更改流量捕获中的列
up 20130311155109删除日志设置
up 20130311160901删除日志警报表
up 20130320144219 ********** NO FILE **********
最多20130320161939 **********无文件**********
最多20130320184628 **** ****** NO FILE **********
最多20130322004817将复制副本添加到根设置
最多20130403190042 **********无文件*** *******
最多20130403195300 **********无文件**********
最多20130403214000 ********* * NO FILE **********
最多20130405164752修复主机名
up 20130410194222 ********** NO FILE **********



<问题是 rake db:rollback 完全不工作,因为缺少文件...



我该怎么做才能重新回滚并摆脱NO FILE消息?



顺便说一句, rake db:reset c $ c>或 rake db:drop 不是一个选项,我不能丢失其他表中的数据...

我最终解决了这个问题:



(1)转到分支那里有迁移文件并将其回滚。当你有很多分支时,这是不平凡的,如果你试图合并它们,会导致许多冲突。因此,我使用这个命令来找出每个孤立迁移所属的分支。



所以,我需要找到上次迁移被修改时的提交。 p>

  git log --all --reverse --stat | grep< LASTEST_ORPHAN_MIGRATION_ID> -C 10 

我使用提交散列并确定它属于哪个分支:

  git branch --contains< COMMIT_HASH> 

然后我可以回到那个分支,做一个回滚并对所有丢失的文件重复这个过程。

(2)运行迁移:检出您最终想要处理的分支并运行迁移,您应该很好。



疑难解答



我也在某些情况下运行了孤立迁移,为了解决这个问题,我创建了具有相同migration_id的缺失文件的虚拟迁移文件,并将其回滚。之后,我可以删除他们的虚拟迁移并且具有干净的迁移状态:)



另一种方法是直接从数据库中删除丢失的文件:

 从schema_migrations中删除where version ='< MIGRATION_ID>'; 


I have been switching between branches in a project and each of them have different migrations... This is the scenario:

$ rake db:migrate:status

 Status   Migration ID    Migration Name
--------------------------------------------------
   ...
   up     20130307154128  Change columns in traffic capture
   up     20130311155109  Remove log settings
   up     20130311160901  Remove log alarm table
   up     20130320144219  ********** NO FILE **********
   up     20130320161939  ********** NO FILE **********
   up     20130320184628  ********** NO FILE **********
   up     20130322004817  Add replicate to root settings
   up     20130403190042  ********** NO FILE **********
   up     20130403195300  ********** NO FILE **********
   up     20130403214000  ********** NO FILE **********
   up     20130405164752  Fix ap hostnames
   up     20130410194222  ********** NO FILE **********

The problem is rake db:rollback don't work at all because of the missing files...

What should I do to be able to rollback again and get rid of the NO FILE messages?

Btw, rake db:reset or rake db:drop are not an option, I cannot lose data from other tables...

解决方案

I ended up solving the problem like this:

(1) Go to the branches that has the migration files and roll them back. This is not trivial when you have many branches which are will result in many conflicts if you try to merge them. So I use this commands to find out the branches of each orphan migration belongs to.

So, I need to find commit of the last time the migration was modified.

git log --all --reverse --stat | grep <LASTEST_ORPHAN_MIGRATION_ID> -C 10

I take the commit hash and determine which branch it belongs like this:

git branch --contains <COMMIT_HASH>

Then I can go back to that branch, do a rollback and repeat this process for all the missing files.

(2) Run migrations: checkout the branch you finally want to work on and run the migrations and you should be good to go.

Troubleshooting

I also ran in some cases where orphaned migrations where on deleted branches.

To solve this I created dummy migration files with the same migration_id of the missing files and roll them back. After that, I was able to delete they dummy migrations and have a clean migration status :)

Another alternative is deleting the missing files from the database directly:

delete from schema_migrations where version='<MIGRATION_ID>';

这篇关于解决Rails孤儿迁移的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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