From: Mike Bayer Date: Wed, 1 Mar 2017 06:14:50 +0000 (-0500) Subject: Don't raise on open transaction if we already started in one X-Git-Tag: rel_0_9_1~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=cfb9d348c48bca8667f4cb12c5baab085f053494;p=thirdparty%2Fsqlalchemy%2Falembic.git Don't raise on open transaction if we already started in one An adjustment to the bug fix for :ticket:`369` to accommodate for env.py scripts that use an enclosing transaction distinct from the one that the context provides, so that the check for "didn't commit the transaction" doesn't trigger in this scenario. Change-Id: I3c1fa614495b61532999a84b2af3773e4d33c30b Fixes: #417 --- diff --git a/alembic/runtime/migration.py b/alembic/runtime/migration.py index 0f1d88e0..d13b9996 100644 --- a/alembic/runtime/migration.py +++ b/alembic/runtime/migration.py @@ -308,6 +308,9 @@ class MigrationContext(object): head_maintainer = HeadMaintainer(self, heads) + starting_in_transaction = not self.as_sql and \ + self.connection.in_transaction() + for step in self._migrations_fn(heads, self): with self.begin_transaction(_per_migration=True): if self.as_sql and not head_maintainer.heads: @@ -326,7 +329,8 @@ class MigrationContext(object): # just to run the operations on every version head_maintainer.update_to_step(step) - if not self.as_sql and not self.impl.transactional_ddl and \ + if not starting_in_transaction and not self.as_sql and \ + not self.impl.transactional_ddl and \ self.connection.in_transaction(): raise util.CommandError( "Migration \"%s\" has left an uncommitted " diff --git a/docs/build/changelog.rst b/docs/build/changelog.rst index 3a4f0e2a..fcd9aae6 100644 --- a/docs/build/changelog.rst +++ b/docs/build/changelog.rst @@ -7,6 +7,15 @@ Changelog :version: 0.9.1 :released: + .. change:: 417 + :tags: bug, commands + :tickets: 417, 369 + + An adjustment to the bug fix for :ticket:`369` to accommodate for + env.py scripts that use an enclosing transaction distinct from the + one that the context provides, so that the check for "didn't commit + the transaction" doesn't trigger in this scenario. + .. changelog:: :version: 0.9.0 :released: February 28, 2017 diff --git a/tests/test_script_consumption.py b/tests/test_script_consumption.py index 6a229584..b313273a 100644 --- a/tests/test_script_consumption.py +++ b/tests/test_script_consumption.py @@ -8,7 +8,7 @@ from alembic.util import compat from alembic.script import ScriptDirectory, Script from alembic.testing.env import clear_staging_env, staging_env, \ _sqlite_testing_config, write_script, _sqlite_file_db, \ - three_rev_fixture, _no_sql_testing_config + three_rev_fixture, _no_sql_testing_config, env_file_fixture from alembic.testing import eq_, assert_raises_message from alembic.testing.fixtures import TestBase, capture_context_buffer from alembic.environment import EnvironmentContext @@ -281,6 +281,35 @@ def downgrade(): transactional_ddl=True, transaction_per_migration=False): command.upgrade(self.cfg, c) + def test_noerr_transaction_opened_externally(self): + a, b, c = self._opened_transaction_fixture() + + env_file_fixture(""" +from sqlalchemy import engine_from_config, pool + +def run_migrations_online(): + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix='sqlalchemy.', + poolclass=pool.NullPool) + + with connectable.connect() as connection: + with connection.begin() as real_trans: + context.configure( + connection=connection, + transactional_ddl=False, + transaction_per_migration=False + ) + + with context.begin_transaction(): + context.run_migrations() + +run_migrations_online() + +""") + + command.stamp(self.cfg, c) + class EncodingTest(TestBase):