db: Don't pass strings to Connection.execute()

Resolve the following RemovedIn20Warning warning:

  Passing a string to Connection.execute() is deprecated and will be
  removed in version 2.0.  Use the text() construct, or the
  Connection.exec_driver_sql() method to invoke a driver-level SQL
  string.

We do the latter.

Change-Id: I0ee518022772beeb0298e12b5b36279fd384fb4a
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
This commit is contained in:
Stephen Finucane 2023-07-14 13:21:28 +01:00
parent 4545fdf69f
commit 69d62e3e6d
3 changed files with 5 additions and 12 deletions

View File

@ -23,8 +23,8 @@ from oslo_db.sqlalchemy import enginefacade
from oslo_db.sqlalchemy import utils as sqlalchemyutils from oslo_db.sqlalchemy import utils as sqlalchemyutils
from oslo_log import log as logging from oslo_log import log as logging
from oslo_utils import timeutils from oslo_utils import timeutils
from sqlalchemy import or_, and_ import sqlalchemy as sa
from sqlalchemy.ext.compiler import compiles from sqlalchemy.ext import compiler
from sqlalchemy import MetaData from sqlalchemy import MetaData
from sqlalchemy.orm import joinedload from sqlalchemy.orm import joinedload
from sqlalchemy import sql from sqlalchemy import sql
@ -736,7 +736,7 @@ class DeleteFromSelect(sa_sql.expression.UpdateBase):
# NOTE(pooja_jadhav): MySQL doesn't yet support subquery with # NOTE(pooja_jadhav): MySQL doesn't yet support subquery with
# 'LIMIT & IN/ALL/ANY/SOME' We need work around this with nesting select. # 'LIMIT & IN/ALL/ANY/SOME' We need work around this with nesting select.
@compiles(DeleteFromSelect) @compiler.compiles(DeleteFromSelect)
def visit_delete_from_select(element, compiler, **kw): def visit_delete_from_select(element, compiler, **kw):
return "DELETE FROM %s WHERE %s in (SELECT T1.%s FROM (%s) as T1)" % ( return "DELETE FROM %s WHERE %s in (SELECT T1.%s FROM (%s) as T1)" % (
compiler.process(element.table, asfrom=True), compiler.process(element.table, asfrom=True),
@ -774,7 +774,7 @@ def purge_deleted_rows(context, age_in_days, max_rows):
if table.name == 'notifications': if table.name == 'notifications':
status_column = table.c.status status_column = table.c.status
query_delete = sql.select([column]).where( query_delete = sql.select([column]).where(
and_(updated_at_column < deleted_age, or_( sa.and_(updated_at_column < deleted_age, sa.or_(
status_column == 'finished', status_column == 'failed', status_column == 'finished', status_column == 'failed',
status_column == 'ignored'))).order_by(status_column) status_column == 'ignored'))).order_by(status_column)
else: else:

View File

@ -188,13 +188,6 @@ class WarningsFixture(fixtures.Fixture):
category=sqla_exc.SADeprecationWarning, category=sqla_exc.SADeprecationWarning,
) )
warnings.filterwarnings(
'ignore',
message=r'Passing a string to Connection.execute\(\)',
module='masakari',
category=sqla_exc.SADeprecationWarning,
)
warnings.filterwarnings( warnings.filterwarnings(
'ignore', 'ignore',
message=r'The legacy calling style of select\(\)', message=r'The legacy calling style of select\(\)',

View File

@ -111,7 +111,7 @@ class PurgeDeletedTest(test.TestCase):
dialect = self.engine.url.get_dialect() dialect = self.engine.url.get_dialect()
if dialect == sqlite.dialect: if dialect == sqlite.dialect:
self.conn.execute("PRAGMA foreign_keys = ON") self.conn.exec_driver_sql("PRAGMA foreign_keys = ON")
def _count(self, table): def _count(self, table):
return self.conn.execute( return self.conn.execute(