Rails 迁移更改顺序或顺序 [英] Rails migration change sequence or order
问题描述
我为我的 Rails 3 应用程序编写了一些迁移,但我想更改迁移的顺序.如何更改迁移顺序或顺序?是否像使用时间戳重命名迁移文件一样简单?
I wrote a few migrations for my Rails 3 app, but I would like to change the order of the migrations. How can I change the migration order or sequence? Is it as simple as renaming the migration file with what appears to be the timestamp?
我知道这是一个奇怪的问题,但基本上,我弄乱了我的迁移并删除了一些旧的迁移,现在我需要在创建新表之前删除一个表.我也知道我可以在 create-the-new-table 迁移中包含 drop 语句,但我很想知道如何重新排序迁移.
I know this is an odd question, but basically, I made a mess of my migrations and removed some old migrations and now I need to drop a table before creating a new one. I also know I can include the drop statement in the create-the-new-table migration, but I'm curious to know how to reorder migrations.
推荐答案
是的,它会按照前缀的顺序运行没有运行过的迁移.在早期版本的 Rails 中,可能是 2.1
或 2.2
,它们曾经以 01
开始编号,但它们切换到时间戳.
Yes, it runs the migrations which have not been run in the order of the prefix. In earlier versions of rails, maybe 2.1
or 2.2
, they used to be numbered starting with 01
, but they switched to timestamps.
有一个表格可以跟踪已运行的迁移.目的是,多个开发人员添加了迁移,并稍后在版本控制中检查它们.因此,可能存在尚未运行但编号在已运行的最高编号迁移之前的迁移.
There is a table which keeps track of which migrations have run. The intentions is, multiple developers my have added migrations, and checked them in version control later. So, there may be a migration which has not run, but is numbered before the highest numbered migration which has run.
如果您更改了迁移顺序,(我也有)最好在重新排序的所有迁移之前先降低版本.将 VERSION
选项与 db:migrate
一起使用.如果您要保留(而不是向下运行)编号最高的迁移是 20120318143249
,请这样调用.
If you change the migration sequence, (and I have) it's better to first down version before all the migrations you are re-sequencing. Use the VERSION
option with db:migrate
. If the highest numbered migration that you want to keep (not run the down) is 20120318143249
, call it this way.
rake db:migrate VERSION=20120318143249
我经常运行一个迁移,然后重新运行它,直到我得到满意的迁移细节.有时我会重新订购它们,当我想为其中一个工作时,我希望它是最后一个.
I often run the down on a migration, and re-run it until I get the details of the migration to my satisfaction. Sometimes I re-order them, when I want to work on one of them, and I want it to be the last.
这篇关于Rails 迁移更改顺序或顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!