问题
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()
来源:https://stackoverflow.com/questions/22095039/run-alembic-upgrade-migrations-in-a-transaction