将多个南迁移到一个迁移中 [英] Squashing multiple South migrations into one migration

查看:113
本文介绍了将多个南迁移到一个迁移中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在开发过程中,我创建了许多迁移,通常来回绕过我想要实现的东西。

During development I created many migrations, often going back and forth about how I wanted to implement something.

现在是时候把它推送到生产了,但是在重播所有在处女数据库上的迁移时,我收到错误。

Now it is time to push it to production, but I am getting errors when replaying all the migrations on the virgin database.

我最终做了以下工作让它工作。

I ended up doing the following to get it to work.

python manage.py syncdb --all
python manage.py migrate --fake

但是这不会应用我创建的数据迁移。

But that will not apply a data migration that I created.

进一步思考,我决定我想将我的所有迁移压缩成一个,当我准备好将其标记为发布版本。

Upon further thinking, I decided that I wanted to squash all my migrations into just one, when I am ready to tag this as a release version.

我该怎么做?如果不可能/不推荐,那么什么是首选方法?

How can I do this? If it is not possible/recommended, then what is the prefered method?

推荐答案

这是可能的,我自己做了几次您基本上有两种方法:

It's possible, and I've done it myself a few times. You basically have two methods:

1)这是迄今为止最简单和首选的方法。简单地说,在第一个迁移之前,将您的开发数据库回滚到squash中,然后从中删除所有迁移。最后,重新运行schemamigration管理命令。你最终会在一个文件中所需的所有迁移。

1) This is by far the easiest and preferred method. Simply, rollback your dev database to right before the first migration you want to include in the "squash", then delete all the migrations from that one on. Finally, re-run the schemamigration management command. You'll end up with all the migrations necessary all in one file.

2)如果由于某种原因你不能做#1,它仍然在技术上是可能的,但是将会更加严格的程序。将每个迁移的前进和后退方法的内容复制并粘贴到一个迁移文件(最低编号的一个)中。这部分是很容易的,但是您也必须对文件底部的冻结模型进行手动修改。我发现最好的方法是简单地尝试运行这个新的迁移。你可能会收到这样的错误,而且这样的模型没有这样的属性。这是一个信号,将该字段添加到冻结模型。当您的迁移运行没有错误,您是黄金。

2) If for some reason you can't do #1, it's still technically possible, but will be much more exacting of a procedure. Literally copy and paste the contents of the forwards and backwards methods of each migration into one migration file (your lowest numbered one). That part is easy enough, but you'll then have to also make manual modifications to the frozen models at the bottom of the file. I've found the best way is to simply try to run this new migration. You'll likely get errors about such-and-such model has no attribute such-and-such. That's a signal to go add that field to the frozen model. When the migration runs without errors, you're golden.

注意:记住删除这些旧的迁移文件时,请使用您正在使用的任何版本系统删除它们心神。换句话说,使用 git rm svn rm 等。如果你最终使用那些旧的迁移标记你的发行版,你会扼杀你的生产实例。对于我来说,我不会提交迁移,直到我准备好发布我的更改。如果丢失,您可以随时重新生成迁移。

Note: Remember when you delete those old migration files, delete them with the any versioning system you're using in mind. In other words, use git rm or svn rm, etc. If you end up with those old migrations tagging along in your release, you'll bork your production instance. For my part, I never commit migrations until I'm ready to release my changes. You can always regenerate the migration if you lose it.

这篇关于将多个南迁移到一个迁移中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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