长期运行项目中的 Rebase Rails 迁移 [英] Rebase Rails migrations in a long running project

查看:13
本文介绍了长期运行项目中的 Rebase Rails 迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的意思是字典中的变基",而不是 git 定义...

In which I mean "rebasing" in the dictionary, rather than git definition...

我有一个大型、长期运行的 Rails 项目,它有大约 250 次迁移,管理所有这些变得有点笨拙.

I have a large, long running Rails project that has about 250 migrations, it's getting a touch unwieldy to manage all of these.

也就是说,我确实需要一个基础,以便在运行测试时清除和重建我的数据库.所以这些中包含的数据很重要.

That said, I do need a base from which to purge and rebuild my database when running tests. So the data contained in these is important.

有没有人有任何策略可以说,在设定点转储架构 - 归档所有旧迁移并重新开始新迁移.

Does any one have any strategies for say, dumping the schema at a set point - archiving off all the old migrations and starting afresh with new migrations.

显然我可以使用 rake schema:dump - 但我确实需要一种方法让 db:migrate 首先加载架构,然后开始运行其余的迁移.

Obviously I can use rake schema:dump - but really I need a way that db:migrate will load the schema first and then start running the rest of the migrations.

我想继续使用迁移,因为它们在开发中非常有用,但是,我无法返回并编辑 2007 年的迁移,因此保留它似乎很愚蠢.

I would like to keep using migrations as they're very useful in development, however, there's no way I'm going back and editing a migration from 2007 so it seems silly to keep it.

推荐答案

一般来说,你不需要清理旧的迁移.如果您从头开始运行 db:migrate(没有现有的 db),Rails 使用 db/schema.rb 来创建表,而不是运行每个迁移.否则,它只会运行从当前架构升级到最新架构所需的迁移.

In general, you don't need to clean up old migrations. If you're running db:migrate from scratch (no existing db), Rails uses db/schema.rb to create the tables instead of running every migration. Otherwise, it only runs the migrations required to upgrade from the current schema to the latest.

如果您仍想将直到给定点的迁移合并为一个,您可以尝试:

If you still want to combine migrations up to a given point into a single one, you could try to:

  • 使用 rake db:migrate VERSION=xxx
  • 从头开始​​迁移到目标架构
  • 使用 rake db:schema:dump
  • 转储架构
  • 删除从开始到版本 xxx 的迁移,并使用 db/schema.rb 的内容创建一个新的迁移(将 create_table 和 add_index 语句放入新迁移的 self.up 方法中).

确保为汇总的新迁移选择旧迁移版本号之一;否则,Rails 会尝试在您的生产服务器上应用该迁移(这将擦除您现有的数据,因为 create_table 语句使用:force⇒true).

Make sure to choose one of the old migration version numbers for your aggregated new migration; otherwise, Rails would try to apply that migration on your production server (which would wipe your existing data, since the create_table statements use :force⇒true).

无论如何,我不建议这样做,因为 Rails 通常自己可以很好地处理迁移.但是,如果您仍然想这样做,请务必仔细检查所有内容并先在本地尝试,然后再冒生产服务器上数据丢失的风险.

Anyway, I wouldn't recommend to do this since Rails usually handles migrations well itself. But if you still want to, make sure to double check everything and try locally first before you risk data loss on your production server.

这篇关于长期运行项目中的 Rebase Rails 迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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