【发布时间】:2020-02-17 18:41:01
【问题描述】:
使用过旧代码库的任何人都熟悉这个问题:您的版本文件夹中的迁移数量失控了。您的应用程序今天使用的数据库看起来与它开始使用的数据库完全不同。或者,也许更常见的是,引入的迁移可能适用于立即更新,但不知何故,它们破坏了从头到尾恢复/重播迁移的能力。在这两种情况下,最好压缩迁移以模拟重新开始。
我一直在尝试找到一个完整示例,说明如何使用 SQLalchemy 和 Alembic 压缩 Flask 应用程序的迁移。我找到的最接近的是https://alembic.sqlalchemy.org/en/latest/cookbook.html:
# inside of a "create the database" script, first create
# tables:
my_metadata.create_all(engine)
# then, load the Alembic configuration and generate the
# version table, "stamping" it with the most recent rev:
from alembic.config import Config
from alembic import command
alembic_cfg = Config("/path/to/yourapp/alembic.ini")
command.stamp(alembic_cfg, "head")
然而,这个简洁的解释并没有解释my_metadata 的来源,我不清楚alembic.ini 应该包含什么才能完成这项任务。有谁知道这个任务的完整示例?
【问题讨论】:
标签: sqlalchemy migration database-migration alembic