Skip to content

Commit

Permalink
frame a transaction around autocommit
Browse files Browse the repository at this point in the history
Fixed issue where the :meth:`.MigrationContext.autocommit_block` feature
would fail to function when using a SQLAlchemy engine using 2.0 future
mode.

Change-Id: I851573424c7cde2595ae22c816ec6580d7cab248
Fixes: #944
  • Loading branch information
zzzeek committed Oct 4, 2021
1 parent d95e15c commit ed3428e
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
8 changes: 8 additions & 0 deletions alembic/runtime/migration.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,19 @@ def upgrade():
self.connection = (
self.impl.connection
) = base_connection.execution_options(isolation_level="AUTOCOMMIT")

# sqlalchemy future mode will "autobegin" in any case, so take
# control of that "transaction" here
fake_trans: Optional[Transaction] = self.connection.begin()
else:
fake_trans = None
try:
yield
finally:
if not self.as_sql:
assert self.connection is not None
if fake_trans is not None:
fake_trans.commit()
self.connection.execution_options(
isolation_level=current_level
)
Expand Down
8 changes: 8 additions & 0 deletions docs/build/unreleased/944.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. change::
:tags: bug, environment
:tickets: 944

Fixed issue where the :meth:`.MigrationContext.autocommit_block` feature
would fail to function when using a SQLAlchemy engine using 2.0 future
mode.

5 changes: 5 additions & 0 deletions tests/test_postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
from alembic.testing.env import staging_env
from alembic.testing.env import write_script
from alembic.testing.fixtures import capture_context_buffer
from alembic.testing.fixtures import FutureEngineMixin
from alembic.testing.fixtures import op_fixture
from alembic.testing.fixtures import TablesTest
from alembic.testing.fixtures import TestBase
Expand Down Expand Up @@ -457,6 +458,10 @@ def test_alter_enum(self, migration_context):
)


class PGAutocommitBlockTestFuture(FutureEngineMixin, PGAutocommitBlockTest):
pass


class PGOfflineEnumTest(TestBase):
def setUp(self):
staging_env()
Expand Down

0 comments on commit ed3428e

Please sign in to comment.