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

Change-Id: I95fc21e1d0993de94a4eb61b2b51ada7ed81044b
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane 2023-06-27 10:56:26 +01:00
parent 535bc8e22e
commit f477307bd8
1 changed files with 13 additions and 5 deletions

View File

@ -176,13 +176,21 @@ def run_migrations_online():
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
render_as_batch=True,
include_name=include_name,
include_object=include_object,
process_revision_directives=autogen.process_revision_directives, # noqa: E501
)
# when connectable is already a Connection object, calling connect() gives
# us a *branched connection*.
with connectable.connect() as connection:
with context.begin_transaction():
context.run_migrations()
else:
context.configure(
connection=connection,
connection=connectable,
target_metadata=target_metadata,
render_as_batch=True,
include_name=include_name,