Run alembic upgrade migrations in a transaction

2019-02-17 04:53发布

问题:

Does alembic upgrade head run inside a transaction so that all database alterations succeed, or fail? If not, why was it designed this way?

回答1:

My understanding is alembic runs inside a transaction for databases that support it, like Postgres. If you're on a database that does not support this (cough MySQL cough), you can't use this functionality.



回答2:

That's something you can decide within the env.py, which is where you customize the behaviour of the migrations to suit your setup. You can see how to make sure your upgrades happen in a transaction from the template provided as an example for generic databases: https://github.com/zzzeek/alembic/blob/eaaafbca88f85f5432e04affe1f94cbf1ad06080/alembic/templates/generic/env.py#L64

def run_migrations_online():
    # ...
    with context.begin_transaction():
        context.run_migrations()