我尝试以编程方式使用alembic(不编写迁移)在我的数据库上执行某些DDL操作。
import sqlalchemy
import alembic
context = alembic.migration.MigrationContext.configure(sqlalchemy.create_engine("postgres://user:@localhost/database"))
context.connection.echo = True
with alembic.op.Operations.context(context) as op:
op.rename_table("table", "old_table")
op.rename_table("new_table", "table")
基本上,我想一次性在桌子的位置交换new_table。但是,当我查看日志时,我看到alembic在第一个rename_table
语句后提交。
2015-07-09 16:51:02,590 INFO sqlalchemy.engine.base.Engine ALTER TABLE table RENAME TO old_table
2015-07-09 16:51:02,590 INFO sqlalchemy.engine.base.Engine {}
2015-07-09 16:51:02,591 INFO sqlalchemy.engine.base.Engine COMMIT
2015-07-09 16:51:02,598 INFO sqlalchemy.engine.base.Engine ALTER TABLE new_table RENAME TO table
2015-07-09 16:51:02,598 INFO sqlalchemy.engine.base.Engine {}
2015-07-09 16:51:02,599 INFO sqlalchemy.engine.base.Engine COMMIT
如何在执行两个命令后告诉alembic只提交?
答案 0 :(得分:0)
也许你可以用--sql
标志运行alembic,获取sql命令,用grep -iv commit
过滤掉COMMIT,然后添加最后一次提交