如何使用sqlalchemy有效管理频繁的架构更改? [英] How to efficiently manage frequent schema changes using sqlalchemy?
问题描述
我正在使用sqlalchemy编程Web应用程序.在开发的第一个阶段,现场没有生产时,一切都很顺利.通过删除旧的sqlite数据库并从头开始创建一个新的sqlite数据库,我可以轻松地更改数据库架构.
I'm programming a web application using sqlalchemy. Everything was smooth during the first phase of development when the site was not in production. I could easily change the database schema by simply deleting the old sqlite database and creating a new one from scratch.
现在该站点已经投入生产,我需要保留数据,但是我仍然希望通过轻松地将数据库转换为新架构来保持原始开发速度.
Now the site is in production and I need to preserve the data, but I still want to keep my original development speed by easily converting the database to the new schema.
因此,假设我的model.py的修订版为50,而model.py的修订版为75,描述了数据库的架构.在这两种模式之间,大多数更改都是微不足道的,例如,声明了具有默认值的新列,而我只想将此默认值添加到旧记录中.
So let's say that I have model.py at revision 50 and model.py a revision 75, describing the schema of the database. Between those two schema most changes are trivial, for example a new column is declared with a default value and I just want to add this default value to old records.
最终,一些更改可能并非微不足道,需要进行一些预先计算.
Eventually a few changes may not be trivial and require some pre-computation.
您如何(或将要)每天处理一两个新版本的生产代码来处理快速变化的Web应用程序?
How do (or would) you handle fast changing web applications with, say, one or two new version of the production code per day ?
顺便说一下,如果有任何不同,该网站将以Pylons编写.
By the way, the site is written in Pylons if this makes any difference.
推荐答案
Alembic 是新的数据库迁移工具,由SQLAlchemy的作者编写.我发现它比sqlalchemy-migrate更容易使用.它还可以与Flask-SQLAlchemy无缝协作.
Alembic is a new database migrations tool, written by the author of SQLAlchemy. I've found it much easier to use than sqlalchemy-migrate. It also works seamlessly with Flask-SQLAlchemy.
从您的SQLAlchemy模型自动生成模式迁移脚本:
Auto generate the schema migration script from your SQLAlchemy models:
alembic revision --autogenerate -m "description of changes"
然后将新的架构更改应用于您的数据库:
Then apply the new schema changes to your database:
alembic upgrade head
此处的更多信息: http://readthedocs.org/docs/alembic/
这篇关于如何使用sqlalchemy有效管理频繁的架构更改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!