开发者

How to wrap create and sqlalchemy-migrate alter statments in a transaction

I'm trying some db schema changes to my db, using the sqlalchemy table.create and sqlalchemy-migrate table.rename methods, plus some insert into select statments. I want to wrap all of this in a transaction. I can't figure out how to do this. This is what I tried:

engine = sqlalchemy.engine_from_config(conf.local_conf, 'sqlalchemy.')
trans = engine.connect().begin()
try:
    old_metatadata.tables['address'].rename('a开发者_高级运维ddress_migrate_tmp', connection=trans)
    new_metatadata.tables['address'].create(connection=trans)
except:
    trans.rollback()
    raise
else:
    trans.commit()

But it errors with:

AttributeError: 'RootTransaction' object has no attribute '_run_visitor'

(I tried using sqlalchemy-migrate column.alter(name='newname') but that errors, and does not work in a transaction, and so leaves my db in a broken state. I also need to rename multiple columns, and so I decide to roll my own code.)


Ah - I need to simply use the connection that the transaction was created on.

engine = sqlalchemy.engine_from_config(conf.local_conf, 'sqlalchemy.')
conn = engine.connect()
trans = conn.begin()
try:
    old_metatadata.tables['address'].rename('address_migrate_tmp', connection=conn)
    new_metatadata.tables['address'].create(bind=conn)
except:
    trans.rollback()
    raise
else:
    trans.commit()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜