以编程方式检查是否需要部署django南迁移 [英] Programmatically check whether there are django south migrations that need to be deployed

查看:46
本文介绍了以编程方式检查是否需要部署django南迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的部署策略如下(使用Fabric):

My deployment strategy looks like this (using Fabric):


  1. 创建一个新的virtualenv

  2. 在新的virtualenv中部署新代码

  3. 显示维护页面

  4. 将当前数据库复制到新数据库中

  5. 迁移新数据库

  6. 将新代码指向新数据库

  7. 将当前的virtualenv链接到新venv

  8. 重新启动服务

  9. 删除维护页面

  1. create a new virtualenv
  2. deploy new code in new virtualenv
  3. show a maintenance page
  4. copy the current db to new db
  5. migrate new db
  6. point new code to new db
  7. symlink current virtualenv to new venv
  8. restart services
  9. remove maintenance page

我想快速进行迭代。现在,大多数代码更改都不包含迁移。而且,数据库正在增长,因此每次部署(大多是很小的)更改时,复制数据库都会产生大量开销。为了避免复制数据库,我想检查是否存在需要部署的迁移(步骤4之前)。如果没有迁移,我可以直接从步骤2转到步骤7。如果有迁移,我将遵循所有步骤。为此,我需要以编程方式检查是否存在需要部署的迁移。我该怎么做?

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?

推荐答案

在部署新代码的步骤2中,您可以部署一个脚本,该脚本在服务器将检测是否有新的迁移。

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.

示例代码如下:

# copied mostly from south.management.commands.migrate
from south import migration
from south.models import MigrationHistory

apps  = list(migration.all_migrations())

applied_migrations = MigrationHistory.objects.filter(app_name__in=[app.app_label() for app in apps])
applied_migrations = ['%s.%s' % (mi.app_name,mi.migration) for mi in applied_migrations]

num_new_migrations = 0
for app in apps:
    for migration in app:
        if migration.app_label() + "." + migration.name() not in applied_migrations:
            num_new_migrations = num_new_migrations + 1

return num_new_migrations

如果将以上代码包装在脚本中,则结构部署脚本可以使用运行操作来获取新迁移的数量。

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.   

这篇关于以编程方式检查是否需要部署django南迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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