db: Avoid relying on autobegin

If you load a table schema using the 'autoload_with' parameter, a
connection will be auto-begun. We were previously getting away with this
because we were relying on nested connections, but these are no longer a
thing in SQLAlchemy 2.0. Explicitly manage connections when loading the
table schema.

Change-Id: I1a37deef449f72b54cb3cecf0c677203256c0443
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2023-04-17 17:03:54 +01:00
parent 9b47bcca9d
commit de683a2ed0
1 changed files with 8 additions and 5 deletions

View File

@ -4259,8 +4259,10 @@ def _get_fk_stmts(metadata, conn, table, column, records):
# Create the shadow table for the referencing table.
fk_shadow_tablename = _SHADOW_TABLE_PREFIX + fk_table.name
try:
fk_shadow_table = schema.Table(
fk_shadow_tablename, metadata, autoload_with=conn)
with conn.begin():
fk_shadow_table = schema.Table(
fk_shadow_tablename, metadata, autoload_with=conn,
)
except sqla_exc.NoSuchTableError:
# No corresponding shadow table; skip it.
continue
@ -4368,9 +4370,10 @@ def _archive_deleted_rows_for_table(
rows_archived = 0
deleted_instance_uuids = []
try:
shadow_table = schema.Table(
shadow_tablename, metadata, autoload_with=conn,
)
with conn.begin():
shadow_table = schema.Table(
shadow_tablename, metadata, autoload_with=conn,
)
except sqla_exc.NoSuchTableError:
# No corresponding shadow table; skip it.
conn.close()