Django生产中的数据库迁移 [英] Database migrations on django production

查看:138
本文介绍了Django生产中的数据库迁移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于在非平凡的生产环境中具有django应用程序的人,您如何处理数据库迁移?我知道有south,但是如果涉及到很多实质内容,那似乎会错过很多.

另外两个选项(我可以想到或已经使用过)是在测试数据库上进行更改,然后(与应用程序脱机)并导入该sql导出.或者,也许是一个风险更大的选择,实时在生产数据库上进行必要的更改,如果出现任何错误,请还原到备份.

您通常如何处理数据库迁移和架构更改?

解决方案

我认为此问题有两个部分.

首先是管理数据库架构及其更改.我们使用South来做到这一点,将工作模型和迁移文件都保留在我们的SCM存储库中.为了安全(或偏执狂),我们在运行任何迁移之前(如果真的很害怕,之后)对数据库进行转储.到目前为止,南方已经足以满足我们的所有要求.

第二,正在部署架构更改,这不仅仅是运行South生成的迁移文件.以我的经验,对数据库的更改通常需要对已部署的代码进行更改.如果您只有一个小型Web场,则使部署的代码与数据库模式的当前版本保持同步可能并非易事-如果考虑不同的缓存层并影响已经处于活动状态的站点用户,情况将变得更糟.不同的站点对这个问题的处理方式不同,我认为没有一个万能的答案.


解决此问题的第二部分并不一定是直截了当的.我不相信有一种千篇一律的方法,而且关于您的网站和环境的信息不足,无法提出最适合您情况的解决方案.但是,我认为需要记住一些注意事项,以帮助指导大多数情况下的部署.

在某些情况下,可以使整个站点(Web服务器和数据库)脱机.当然,这是管理更新的最直接的方法.但是频繁的停机(即使是有计划的停机)可能是快速开展业务的好方法,即使很小的代码更改也很难部署,并且如果您有大量的数据集和/或复杂的迁移,则可能要花费数小时.就是说,对于我帮助管理的网站(都是内部网站,通常只在工作日的工作时间内使用),这种方法会产生奇迹.

如果对主数据库的副本进行更改,请小心.这里的主要问题是您的站点仍处于活动状态,并且大概接受对数据库的写操作.当您忙于迁移克隆以供以后使用时,写入主数据库的数据会怎样?您的网站必须一直处于瘫痪状态,或者暂时处于某种只读状态,否则您将丢失它们.

如果您的更改是向后兼容的,并且您有一个Web服务器场,则有时您可以放弃更新实时生产数据库服务器(我认为在大多数情况下这是不可避免的),然后通过采用它们来增量更新服务器场中的节点在短时间内从负载均衡器中退出.这可以正常工作-但是这里的主要问题是,如果已经更新的节点发送了旧节点不支持的url请求,则会失败,因为您无法在负载均衡器级别进行管理. /p>

我已经看到/听到了其他两种方法可以很好地工作.

首先将所有代码更改包装在功能锁中,然后可以在运行时通过某些站点范围的配置选项进行配置.从本质上讲,这意味着您可以释放所有更改已关闭的代码,然后在对服务器进行所有必要的更新之后,更改配置选项以启用该功能.但这会使代码变得很繁重...

第二个是让代码管理迁移.我听说过一些站点,在其中编写了对代码的更改,以便可以在运行时处理迁移.它能够检测使用的模式的版本以及它返回的数据的格式-如果数据来自旧模式,它将进行迁移,如果数据已经来自新模式,则将不执行任何操作.通过自然的站点使用,大部分数据将由使用该站点的人员迁移,其余的则可以随时使用迁移脚本来完成.

但是我认为Google会成为您的朋友,因为正如我所说,该解决方案是针对具体情况的,我担心此答案将变得毫无意义...搜索零停机时间部署"之类的内容您会得到诸如此的结果有很多想法...

From someone who has a django application in a non-trivial production environment, how do you handle database migrations? I know there is south, but it seems like that would miss quite a lot if anything substantial is involved.

The other two options (that I can think of or have used) is doing the changes on a test database and then (going offline with the app) and importing that sql export. Or, perhaps a riskier option, doing the necessary changes on the production database in real-time, and if anything goes wrong reverting to the back-up.

How do you usually handle your database migrations and schema changes?

解决方案

I think there are two parts to this problem.

First is managing the database schema and it's changes. We do this using South, keeping both the working models and the migration files in our SCM repository. For safety (or paranoia), we take a dump of the database before (and if we are really scared, after) running any migrations. South has been adequate for all our requirements so far.

Second is deploying the schema change which goes beyond just running the migration file generated by South. In my experience, a change to the database normally requires a change to deployed code. If you have even a small web farm, keeping the deployed code in sync with the current version of your database schema may not be trivial - this gets worse if you consider the different caching layers and effect to an already active site user. Different sites handle this problem differently, and I don't think there is a one-size-fits-all answer.


Solving the second part of this problem is not necessarily straight forward. I don't believe there is a one-size-fits-all approach, and there is not enough information about your website and environment to suggest a solution that would be most suitable for your situation. However, I think there are a few considerations that can be kept in mind to help guide deployment in most situations.

Taking the whole site (web servers and database) offline is an option in some cases. It is certainly the most straight forward way to manage updates. But frequent downtime (even when planned) can be a good way to go our of business quickly, makes it tiresome to deploy even small code changes, and might take many hours if you have a large dataset and/or complex migration. That said, for sites I help manage (which are all internal and generally only used during working hours on business days) this approach works wonders.

Be careful if you do the changes on a copy of your master database. The main problem here is that your site is still live, and presumably accepting writes to the database. What happens to data written to the master database while you are busy migrating the clone for later use? Your site has to either be down the whole time or put in some read-only state temporarily otherwise you'll lose them.

If your changes are backwards compatible, and you have a web farm, sometimes you can get away with updating the live production database server (which I think is unavoidable in most situations) and then incrementally updating nodes in the farm by taking them out of the load balancer for a short period. This can work ok - however the main problem here is if a node that has already been updated sends a request for a url which isn't supported by an older node you will get fail as you cant manage that at the load balancer level.

I've seen/heard a couple of other ways work well.

The first is wrapping all code changes in a feature lock which is then configurable at run-time through some site-wide configuration options. This essentially means you can release code where all your changes are turned off, and then after you have made all the necessary updates to your servers you change your configuration option to enable the feature. But this makes quite heavy code...

The second is letting the code manage the migration. I've heard of sites where changes to the code is written in such a way that it handles the migration at runtime. It is able to detect the version of the schema being used, and the format of the data it got back - if the data is from the old schema it does the migration in place, if the data is already from the new schema it does nothing. From natural site usage a high portion of your data will be migrated by people using the site, the rest you can do with a migration script whenever you like.

But I think at this point Google becomes your friend, because as I say, the solution is very context specific and I'm worried this answer will start to get meaningless... Search for something like "zero down time deployment" and you'll get results such as this with plenty of ideas...

这篇关于Django生产中的数据库迁移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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