是否值得使用 sqlalchemy-migrate ?

问题描述

我有一个使用 sqlalchemy(在 Pylons 内)的 Web 应用程序.我需要有效地更改架构,以便能够至少每天更改生产版本,也许更多,而不会丢失数据.

I have a web application using sqlalchemy (within Pylons). I need to effiently change the schema to be able to change the production version at least on a daily basis, maybe more, without losing the data.

我在周末玩了一点 sqlalchemy-migrate,我想说它给我留下了不好的印象.首先我认为这无助于两个数据库引擎之间的迁移;这可能只用 sqlalchemy 就可以完成.其次,文档似乎不是最新的.我不得不更改一些命令行选项,例如在每个命令中提供存储库路径,这可能是迁移的错误.

I have played a little bit with sqlalchemy-migrate over the week-end and I would say that it gave me a bad impression. First I think it cannot help with migration between two databases engines; that's something that could probably be done with sqlalchemy alone. Second the docs do not seem up to date. I had to change some command-line options, like giving the repository path at each command, this could be a bug of migrate.

但最糟糕的是manage.py test"命令.不仅它实际上修改了数据库(这一点在文档中明确指出,所以我不能责怪迁移)而且我的第一个迁移脚本只是进行了简单愚蠢的模式迁移,使升级降级的数据库与与原始架构不同的架构.但是manage.py 测试"只是回答了类似

But the worst thing it the "manage.py test" command. Not only it actually modifies the database (this point is clearly indicated in the documentation so I can't blame migrate) but my first migration script just made plain stupid schema migration, leaving the upgraded-downgraded db with a different schema than the original. But the "manage.py test" just answered something like

 success !

也就是说,它甚至不检查模式是否保持一致状态.那么值得使用迁移吗?与与良好实践相关的自己动手"方法相比有什么优势 ?是否有 sqlalchemy-migrate 的替代方法实际上简化了迁移过程,或者我只是尝试使用带有错误的迁移 priori (然后请告诉我为什么明显优于创建 CSV 列,如上面的链接)?

That is, it did not even check if the schema was left in a coherent state. So is it worth using migrate? Is there any advantage compared to the Do It Yourself method associated with good practices as proposed by S.Lott ? Are there alternatives to sqlalchemy-migrate actually simplifying the migration process or am I just trying to use migrate with a bad a priori (then please show me why is't clearly superior to creating CSV columns as proposed in the link above)?

非常感谢!


解决方案

改用 Alembic:

Use Alembic instead:

http://pypi.python.org/pypi/alembic

感谢您的评论,编辑添加一些推理--

Thanks for comments, edited to add some reasoning --

它是由 SQLAlchemy 的作者开发的,它是全新的并且得到很好的支持.我对 sqlalchemy-migrate 的了解还不够,无法进行很好的比较.但我快速阅读了清晰简洁的 Alembic 文档,然后在很短的时间内完成了我自己的自动生成迁移.

It's developed by the author of SQLAlchemy, and it's brand new and well supported. I don't know enough about sqlalchemy-migrate to give a good comparison. But I took a quick read through the clear and concise Alembic docs, then got my own autogenerated migration working in a very short time.

自动生成:不是它唯一的操作模式,但如果您选择,Alembic 将读取您应用程序的 sqlalchemy 配置(例如,设置所有表、约束和映射的声明性模型类)并与实际当前数据库的状态,并输出一个 Python 脚本来表示两者之间的差异.然后将该脚本传递给 Alembic 的升级命令,然后就可以解决差异了.通常需要手动编辑少量的迁移脚本,这是 (a) 只是迁移的性质,以及 (b) 无论如何您都想做的事情,以确保您完全了解迁移的确切步骤在运行之前执行.

Autogeneration: Not its only mode of operation, but if you choose, Alembic will read your application's sqlalchemy configuration (for instance, your declarative model classes that set up all your tables, constraints, and mappings) and compare to the actual current state of your database, and output a Python script that represents the delta between the two. You then pass that script to Alembic's upgrade command and there you go, the differences are resolved. A small amount of editing the migration script by hand is usually needed, and that's (a) just the nature of migrations, and (b) something you want to do anyway to make sure you were fully aware of the exact steps that the migration is going to perform before you run it.

Alembic 也为您的迁移跟踪方式带来了类似 DVCS 的功能.它可以很容易地返回到您的数据库架构的任何过去状态.

Alembic brings a DVCS-like ability to the way your migrations are tracked, too. It makes it really easy to return to any past state of your db schema.

相关文章