向表中添加新列时,是应该创建新迁移还是编辑旧迁移?

时间:2011-01-04 02:26:04

标签: migration ruby-on-rails-3

如果我创建一个包含rails generate migration的表,我可以稍后通过创建新的迁移为其添加一个额外的列。我还可以回滚原始迁移,然后对其进行编辑以包含额外的列。

方法1:新迁移

//Create the model including the migration
$ rails generate model Foo bar:string

//Perform the migration
$ rake db:migrate

//Create the add column migration
$ rails generate migration add_foobar_to_foos foobar:string

//Perform the new migration
$ rake db:migrate

方法2:回滚

//Create the model including the migration
$ rails generate model Foo bar:string

//Perform the migration
$ rake db:migrate

//Rollback the migration
$ rake db:rollback

//Edit the original migration file

//Perform the new migration
$ rake db:migrate

完成此任务的正确/最佳方法是什么?为什么?

1 个答案:

答案 0 :(得分:13)

我想方法1.为什么?因为如果其他开发人员/机器正在使用此环境,您可能会因为他们可能需要在正确的时间回滚以维持正确的数据库结构而获得不稳定的状态。