begin_transaction
begin_transaction() → Union[_ProxyTransaction, AbstractContextManager]
Return a context manager that will enclose an operation within a “transaction”, as defined by the environment’s offline and transactional DDL settings.
e.g.:
with context.begin_transaction():
context.run_migrations()
begin_transaction() is intended to “do the right thing” regardless of calling context:
- If is_transactional_ddl() is
False
, returns a “do nothing” context manager which otherwise produces no transactional state or directives. - If is_offline_mode() is
True
, returns a context manager that will invoke the DefaultImpl.emit_begin() and DefaultImpl.emit_commit() methods, which will produce the string directivesBEGIN
andCOMMIT
on the output stream, as rendered by the target backend (e.g. SQL Server would emitBEGIN
TRANSACTION). - Otherwise, calls sqlalchemy.engine.Connection.begin() on the current online connection, which returns a sqlalchemy.engine.Transaction object. This object demarcates a real transaction and is itself a context manager, which will roll back if an exception is raised.
Note: that a custom
env.py
script which has more specific transactional needs can of course manipulate the Connection directly to produce transactional state in “online” mode.