【发布时间】:2015-03-10 19:00:01
【问题描述】:
我正在使用Alembic 管理数据库迁移,并希望使用alembic init 生成的默认文件,而无需对env.py(例如setting target_metadata)或alembic.ini(例如setting sqlalchemy.url)进行任何修改) 来管理我的数据库从脚本的迁移。
例如,我想使用基于 command 的脚本,如下所示:
import os
from alembic.config import Config
from alembic import command
from myapp import db
alembic_cfg = Config(os.path.join(os.path.abspath(os.path.dirname(__file__)), 'alembic.ini'))
alembic_cfg.set_main_option("sqlalchemy.url",
os.environ['DATABASE_URL'])
alembic_cfg.set_main_option("script_location",
os.path.join(os.path.abspath(os.path.dirname(__file__)), '.db_migrations'))
alembic_cfg.set_main_option("target_metadata", db.metadata) # This doesn't work!
command.revision(alembic_cfg, message='Test of new system', autogenerate=True)
command.upgrade(alembic_cfg, 'head')
这一切都按预期工作,除了指示的行失败,我看不到设置target_metadata的方法(除了编辑env.py)。
我如何以编程方式在上面的脚本中(类似于)设置 Alembic 所需的 target_metadata,该脚本使用 Alembic 的命令 API?
【问题讨论】:
标签: python sqlalchemy database-migration flask-sqlalchemy alembic