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