Eve*_*ett 5 migration sqlalchemy database-migration alembic
Anyone who has worked on an old code-base is familiar with this problem: the number of migrations in your versions folder gets out of hand. The database your application uses today looks nothing like the database it started with. Or, perhaps more commonly, migrations get introduced that may work for the immediate update, but somehow they break the ability to revert/replay the migrations from start to finish. In both cases, it can be nice to squash the migrations to mimic getting a fresh start.
I've been trying to find a complete example of how to squash migrations for a Flask application using SQLalchemy and Alembic. The closest thing I've found is 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")
Run Code Online (Sandbox Code Playgroud)
然而,这个简洁的解释并没有解释 的来源my_metadata
,而且我不清楚alembic.ini
应该包含什么才能完成此任务。有谁知道这个任务的完整示例?
Hen*_*ren -3
示例中的 my_metadata 只是声明性 API 中对 Base 的引用 ( https://docs.sqlalchemy.org/en/13/orm/extensions/declarative/api.html )
\n\n导入示例:
\n\nfrom yourapplication.models import Base\n\nmy_metadata = Base.metadata\n
Run Code Online (Sandbox Code Playgroud)\n\n只要您将所有模型导入到保存 Base 的位置,数据库中的所有模型都可以通过其元数据使用。之后按照食谱指南进行操作,它应该可以工作\xe2\x84\xa2
\n 归档时间: |
|
查看次数: |
2306 次 |
最近记录: |