db: Replace use of Engine.execute() method

Resolve the following RemovedIn20Warning warning:

  The Engine.execute() method is considered legacy as of the 1.x
  series of SQLAlchemy and will be removed in 2.0.

This also allows us to resolve the following RemovedIn20Warning warning:

  The current statement is being autocommitted using implicit
  autocommit, which will be removed in SQLAlchemy 2.0. Use the .begin()
  method of Engine or Connection in order to use an explicit transaction
  for DML and DDL statements.

Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
Change-Id: I46f3277ea17e814e7b631750b2739f25e4395e22
This commit is contained in:
Stephen Finucane 2023-09-21 13:42:45 +01:00
parent 9a66301aef
commit 947c7b4ffd
2 changed files with 2 additions and 18 deletions

View File

@ -171,7 +171,8 @@ class Connection(base.Connection):
def clear(self):
engine = enginefacade.writer.get_engine()
for table in reversed(models.Base.metadata.sorted_tables):
engine.execute(table.delete())
with engine.connect() as conn, conn.begin():
conn.execute(table.delete())
engine.dispose()
def _retrieve_data(self, filter_expr, orderby, limit, table):

View File

@ -79,23 +79,6 @@ class WarningsFixture(fixtures.Fixture):
category=sqla_exc.SADeprecationWarning,
)
# ...but filter everything out until we get around to fixing them
# TODO(stephenfin): Fix all of these
warnings.filterwarnings(
'ignore',
module='aodh',
message=r'The Engine.execute\(\) method is considered legacy ',
category=sqla_exc.SADeprecationWarning,
)
warnings.filterwarnings(
'ignore',
module='aodh',
message='The current statement is being autocommitted using ',
category=sqla_exc.SADeprecationWarning,
)
# Enable general SQLAlchemy warnings also to ensure we're not doing
# silly stuff. It's possible that we'll need to filter things out here
# with future SQLAlchemy versions, but that's a good thing