1

I'm trying to use alembic programmatically (without writing migrations) to perform some DDL operations on my database.

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")

Basically, I'd like to swap in new_table in the place of table in one go. When I look at the logs however, I see that alembic commits after the first rename_table statement.

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

How can I tell alembic to only commit after the two commands are performed?

BvdBijl
  • 607
  • 6
  • 16

1 Answers1

0

just trying a crazy idea:

maybe you can run alembic with the --sql flag, get the sql commands, filter out the COMMITs with something like grep -iv commit and then add the last commit

akiva
  • 2,677
  • 3
  • 31
  • 40