通过编程检查是否有需要部署的django South迁移
我的部署策略如下所示(使用 Fabric):
- 创建一个新的 virtualenv
- 在新的 virtualenv 中部署新代码
- 显示维护页面
- 将当前数据库复制到新数据库
- 迁移新数据库
- 点 新代码到新数据库
- 符号链接 当前 virtualenv 到新 venv 重新
- 启动服务
- 删除维护页面
我想快速迭代。现在,大多数代码更改不包含迁移。此外,数据库正在增长,因此每次部署(大部分是小)更改时复制数据库都会产生大量开销。为了避免复制数据库,我想检查是否有需要部署的迁移(在步骤 4 之前)。如果没有迁移,我可以直接从步骤 2 到步骤 7。如果有,我将遵循所有步骤。为此,我需要以编程方式检查是否存在需要部署的迁移。我该怎么做?
My deployment strategy looks like this (using Fabric):
- create a new virtualenv
- deploy new code in new virtualenv
- show a maintenance page
- copy the current db to new db
- migrate new db
- point new code to new db
- symlink current virtualenv to new venv
- restart services
- remove maintenance page
I want to iterate fast. Now, most of the code changes do not contain migrations. Also, the db is growing, so there is much overhead created by copying the database everytime I deploy a (mostly small) change. To avoid copying the database I want to check whether there are migrations that need to be deployed (prior to step 4). If there are no migrations, I can go straight from step 2 to step 7. If there are, I will follow all the steps. For this, I need to check programmatically whether there are migrations that need to be deployed. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在部署新代码的步骤 2 中,您可以部署一个脚本,该脚本在服务器上运行时将检测是否有新的迁移。
示例代码如下:
如果将上述代码封装在脚本中,您的 Fabric 部署脚本可以使用 run 操作来获取新迁移的数量。
如果返回零,则您可以跳过与复制数据库相关的步骤。
In step 2 while deploying the new code, you could deploy a script which when run on the server will detect if there are new migrations.
Example code is as follows:
If you wrap the above code up in a script, your fabric deployment script can use the run operation to get the number of new migrations.
If this returns zero, then you can skip the steps associated with copying the database.
将告诉您并展示哪些迁移。如果您想要返回代码或计数,请使用 wc。
这样做的优点是不需要像接受的答案那样复制和粘贴代码(违反 DRY),而且如果内部南 API 发生更改,您的代码仍然可以工作。
更新:
Django 1.7 将输出更改为使用括号而不是括号,Django 1.8 引入了 showmigration 命令:
Will tell and show you which migrations. If you want a return code or count, use wc.
This has the advantages of not copying and pasting code like the accepted answer (violating DRY), and also if the internal south api changes your code will still work.
UPDATE:
Django 1.7 changed the output to use bracket instead of parenthesis and Django 1.8 introduced a showmigration command:
dalore 的答案已更新为 Django 1.7+
如果您只想要计数,那么:
dalore's answer updated for Django 1.7+
If you just want the count then:
为什么要移动数据库?迁移的重点是将开发中所做的更改应用到生产数据库中。
您的步骤实际上应该是:
如果没有实际的新迁移,迁移步骤不会花费那么长时间运行。它只会运行每个应用程序,并表示它已经是最新的。
如果您要复制数据库以进行备份,那么无论如何都应该在 cron 或其他东西上运行,而不是作为部署的一部分。
实际上,我也对每次创建新的 virtualenv 感到困惑。规范化(读:最典型)部署是:
如果您想在维护页面中添加回内容,您可以,但该过程总共只需要一两分钟。
Why are you moving the databases around? The whole point of migrations is to apply the changes you made in development to your production database in place.
Your steps should really be:
And the migration step doesn't take that long if there's no actual new migrations to run. It'll just run through each app saying it's already up to date.
If you're copying the database to have a backup, that's something that should be running anyways on cron or something, not as part of your deployment.
Actually, I'm confused on the creating a new virtualenv each time too. The normalized (read: most typical) deployment is:
If you want to add back in the maintenance page stuff, you can, but the process takes only a minute or two total.