From d683ddb86eef89074a2ea09a454c8efd8b88561b Mon Sep 17 00:00:00 2001 From: Takashi Kajinami Date: Mon, 15 Apr 2024 18:25:26 +0900 Subject: [PATCH] db: Don't rely on branched connections We were previously calling 'connect()' on the 'connectable' object in 'run_migrations_online', regardless of whether it was an 'Engine' or 'Connection' object. This worked because, as noted in an inline comment, "when connectable is already a Connection object, calling 'connect()' gives us a *branched connection*." This is no longer the case. From the SQLAlchemy docs [1]: The Connection object does not support "branching", which was a pattern by which a sub "connection" would be used that refers to this connection as a parent. Update our code to reflect this change, using the newly updated example from the SQLAlchemy cookbook doc [2] as inspiration. [1] https://docs.sqlalchemy.org/en/14/core/future.html#sqlalchemy.future.Connection [2] https://alembic.sqlalchemy.org/en/latest/cookbook.html#connection-sharing Closes-Bug: #2061375 Co-Authored-By: Stephen Finucane Change-Id: I5617dc7e6e97e4ba5aad2f5d8be40b2241b9cf4c --- .../persistence/backends/sqlalchemy/alembic/env.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/taskflow/persistence/backends/sqlalchemy/alembic/env.py b/taskflow/persistence/backends/sqlalchemy/alembic/env.py index 2094fe459..c6b0ce00e 100644 --- a/taskflow/persistence/backends/sqlalchemy/alembic/env.py +++ b/taskflow/persistence/backends/sqlalchemy/alembic/env.py @@ -63,13 +63,18 @@ 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: - context.configure(connection=connection, - target_metadata=target_metadata) + with connectable.connect() as connection: + context.configure(connection=connection, + target_metadata=target_metadata) + with context.begin_transaction(): + context.run_migrations() + else: + context.configure( + connection=connectable, + target_metadata=target_metadata) with context.begin_transaction(): context.run_migrations() - if context.is_offline_mode(): run_migrations_offline() else: