db: Replace use of legacy select() calling style

Resolve the following RemovedIn20Warning warning:

  The legacy calling style of select() is deprecated and will be removed
  in SQLAlchemy 2.0.  Please use the new calling style described at
  select().

Change-Id: I36e43e30e07f4904c7b49925cefe804af45cff6c
Signed-off-by: Stephen Finucane <sfinucan@redhat.com>
This commit is contained in:
Stephen Finucane
2021-08-20 12:18:45 +01:00
committed by Stephen Finucane
parent cd9b792ea6
commit c72068d466
4 changed files with 67 additions and 54 deletions

View File

@@ -563,7 +563,7 @@ def _compute_node_select(context, filters=None, limit=None, marker=None):
filters = {}
cn_tbl = sa.alias(models.ComputeNode.__table__, name='cn')
select = sa.select([cn_tbl])
select = sa.select(cn_tbl)
if context.read_deleted == "no":
select = select.where(cn_tbl.c.deleted == 0)
@@ -965,7 +965,7 @@ def compute_node_statistics(context):
inner_sel.c.disk_available_least
).label('disk_available_least'),
]
select = sql.select(agg_cols).select_from(j)
select = sql.select(*agg_cols).select_from(j)
conn = engine.connect()
results = conn.execute(select).fetchone()
@@ -2113,7 +2113,7 @@ def instance_get_all_by_host(context, host, columns_to_join=None):
def _instance_get_all_uuids_by_hosts(context, hosts):
itbl = models.Instance.__table__
default_deleted_value = itbl.c.deleted.default.arg
sel = sql.select([itbl.c.host, itbl.c.uuid])
sel = sql.select(itbl.c.host, itbl.c.uuid)
sel = sel.where(sql.and_(
itbl.c.deleted == default_deleted_value,
itbl.c.host.in_(sa.bindparam('hosts', expanding=True))))
@@ -4191,8 +4191,9 @@ def _get_fk_stmts(metadata, conn, table, column, records):
# AND instance.id IN (<ids>)
# We need the instance uuids for the <ids> in order to
# look up the matching instance_extra records.
select = sql.select([fk.column]).where(
sql.and_(fk.parent == fk.column, column.in_(records)))
select = sql.select(fk.column).where(
sql.and_(fk.parent == fk.column, column.in_(records))
)
rows = conn.execute(select).fetchall()
p_records = [r[0] for r in rows]
# Then, select rows in the child table that correspond to the
@@ -4205,8 +4206,9 @@ def _get_fk_stmts(metadata, conn, table, column, records):
# AND instances.uuid IN (<uuids>)
# We will get the instance_extra ids we need to archive
# them.
fk_select = sql.select([fk_column]).where(
sql.and_(fk.parent == fk.column, fk.column.in_(p_records)))
fk_select = sql.select(fk_column).where(
sql.and_(fk.parent == fk.column, fk.column.in_(p_records))
)
fk_rows = conn.execute(fk_select).fetchall()
fk_records = [r[0] for r in fk_rows]
if fk_records:
@@ -4214,9 +4216,10 @@ def _get_fk_stmts(metadata, conn, table, column, records):
# table insert statements for them and prepend them to the
# deque.
fk_columns = [c.name for c in fk_table.c]
fk_insert = fk_shadow_table.insert(inline=True).\
from_select(fk_columns, sql.select([fk_table],
fk_column.in_(fk_records)))
fk_insert = fk_shadow_table.insert(inline=True).from_select(
fk_columns,
sql.select(fk_table).where(fk_column.in_(fk_records))
)
inserts.appendleft(fk_insert)
# Create main table delete statements and prepend them to the
# deque.
@@ -4274,13 +4277,14 @@ def _archive_deleted_rows_for_table(metadata, tablename, max_rows, before,
deleted_column = table.c.deleted
columns = [c.name for c in table.c]
select = sql.select([column],
deleted_column != deleted_column.default.arg)
select = sql.select(column).where(
deleted_column != deleted_column.default.arg
)
if tablename == "task_log" and task_log:
# task_log table records are never deleted by anything, so we won't
# base our select statement on the 'deleted' column status.
select = sql.select([column])
select = sql.select(column)
if before:
if tablename != "task_log":
@@ -4307,8 +4311,9 @@ def _archive_deleted_rows_for_table(metadata, tablename, max_rows, before,
# {tablename: extra_rows_archived}
extras = collections.defaultdict(int)
if records:
insert = shadow_table.insert(inline=True).\
from_select(columns, sql.select([table], column.in_(records)))
insert = shadow_table.insert(inline=True).from_select(
columns, sql.select(table).where(column.in_(records))
)
delete = table.delete().where(column.in_(records))
# Walk FK relationships and add insert/delete statements for rows that
# refer to this table via FK constraints. fk_inserts and fk_deletes
@@ -4323,7 +4328,9 @@ def _archive_deleted_rows_for_table(metadata, tablename, max_rows, before,
# table are stored prior to their deletion. Basically the uuids of the
# archived instances are queried and returned.
if tablename == "instances":
query_select = sql.select([table.c.uuid], table.c.id.in_(records))
query_select = sql.select(table.c.uuid).where(
table.c.id.in_(records)
)
rows = conn.execute(query_select).fetchall()
deleted_instance_uuids = [r[0] for r in rows]

View File

@@ -853,12 +853,6 @@ class WarningsFixture(fixtures.Fixture):
message=r'The MetaData.bind argument is deprecated .*',
category=sqla_exc.SADeprecationWarning)
warnings.filterwarnings(
'ignore',
module='nova',
message=r'The legacy calling style of select\(\) .*',
category=sqla_exc.SADeprecationWarning)
warnings.filterwarnings(
'ignore',
module='nova',

View File

@@ -183,7 +183,8 @@ class TestDatabaseArchive(integrated_helpers._IntegratedTestBase):
results = {}
for table in shadow_tables:
r = conn.execute(
sa.select([func.count()]).select_from(table)).fetchone()
sa.select(func.count()).select_from(table)
).fetchone()
results[table.name] = r[0]
return results

View File

@@ -5751,14 +5751,15 @@ class ArchiveTestCase(test.TestCase, ModelsObjectComparatorMixin):
where(self.instance_id_mappings.c.uuid.in_(self.uuidstrs[:4]))\
.values(deleted=1, deleted_at=timeutils.utcnow())
self.conn.execute(update_statement)
qiim = sql.select([self.instance_id_mappings]).where(self.
instance_id_mappings.c.uuid.in_(self.uuidstrs))
qiim = sql.select(self.instance_id_mappings).where(
self.instance_id_mappings.c.uuid.in_(self.uuidstrs)
)
rows = self.conn.execute(qiim).fetchall()
# Verify we have 6 in main
self.assertEqual(len(rows), 6)
qsiim = sql.select([self.shadow_instance_id_mappings]).\
where(self.shadow_instance_id_mappings.c.uuid.in_(
self.uuidstrs))
qsiim = sql.select(self.shadow_instance_id_mappings).where(
self.shadow_instance_id_mappings.c.uuid.in_(self.uuidstrs)
)
rows = self.conn.execute(qsiim).fetchall()
# Verify we have 0 in shadow
self.assertEqual(len(rows), 0)
@@ -5829,10 +5830,12 @@ class ArchiveTestCase(test.TestCase, ModelsObjectComparatorMixin):
where(self.instances.c.uuid.in_(self.uuidstrs[2:4]))\
.values(deleted=1, deleted_at=timeutils.utcnow())
self.conn.execute(update_statement)
qiim = sql.select([self.instances]).where(self.
instances.c.uuid.in_(self.uuidstrs))
qsiim = sql.select([self.shadow_instances]).\
where(self.shadow_instances.c.uuid.in_(self.uuidstrs))
qiim = sql.select(self.instances).where(
self. instances.c.uuid.in_(self.uuidstrs)
)
qsiim = sql.select(self.shadow_instances).where(
self.shadow_instances.c.uuid.in_(self.uuidstrs)
)
# Verify we have 6 in main
rows = self.conn.execute(qiim).fetchall()
@@ -5923,13 +5926,15 @@ class ArchiveTestCase(test.TestCase, ModelsObjectComparatorMixin):
where(main_table.c.uuid.in_(self.uuidstrs[:4]))\
.values(deleted=1, deleted_at=timeutils.utcnow())
self.conn.execute(update_statement)
qmt = sql.select([main_table]).where(main_table.c.uuid.in_(
self.uuidstrs))
qmt = sql.select(main_table).where(
main_table.c.uuid.in_(self.uuidstrs)
)
rows = self.conn.execute(qmt).fetchall()
# Verify we have 6 in main
self.assertEqual(len(rows), 6)
qst = sql.select([shadow_table]).\
where(shadow_table.c.uuid.in_(self.uuidstrs))
qst = sql.select(shadow_table).where(
shadow_table.c.uuid.in_(self.uuidstrs)
)
rows = self.conn.execute(qst).fetchall()
# Verify we have 0 in shadow
self.assertEqual(len(rows), 0)
@@ -5972,15 +5977,16 @@ class ArchiveTestCase(test.TestCase, ModelsObjectComparatorMixin):
where(self.instance_id_mappings.c.uuid.in_(self.uuidstrs[:2]))\
.values(deleted=1)
self.conn.execute(update_statement)
qiim = sql.select([self.instance_id_mappings]).where(self.
instance_id_mappings.c.uuid.in_(self.uuidstrs[:2]))
qiim = sql.select(self.instance_id_mappings).where(
self. instance_id_mappings.c.uuid.in_(self.uuidstrs[:2])
)
rows = self.conn.execute(qiim).fetchall()
# Verify we have 2 in main
self.assertEqual(len(rows), 2)
qsiim = sql.select([self.shadow_instance_id_mappings]).\
where(self.shadow_instance_id_mappings.c.uuid.in_(
self.uuidstrs[:2]))
qsiim = sql.select(self.shadow_instance_id_mappings).where(
self.shadow_instance_id_mappings.c.uuid.in_(self.uuidstrs[:2])
)
shadow_rows = self.conn.execute(qsiim).fetchall()
# Verify we have 0 in shadow
self.assertEqual(len(shadow_rows), 0)
@@ -6036,22 +6042,25 @@ class ArchiveTestCase(test.TestCase, ModelsObjectComparatorMixin):
.values(deleted=1, deleted_at=timeutils.utcnow())
self.conn.execute(update_statement2)
# Verify we have 6 in each main table
qiim = sql.select([self.instance_id_mappings]).where(
self.instance_id_mappings.c.uuid.in_(self.uuidstrs))
qiim = sql.select(self.instance_id_mappings).where(
self.instance_id_mappings.c.uuid.in_(self.uuidstrs)
)
rows = self.conn.execute(qiim).fetchall()
self.assertEqual(len(rows), 6)
qi = sql.select([self.instances]).where(self.instances.c.uuid.in_(
self.uuidstrs))
qi = sql.select(self.instances).where(
self.instances.c.uuid.in_(self.uuidstrs)
)
rows = self.conn.execute(qi).fetchall()
self.assertEqual(len(rows), 6)
# Verify we have 0 in each shadow table
qsiim = sql.select([self.shadow_instance_id_mappings]).\
where(self.shadow_instance_id_mappings.c.uuid.in_(
self.uuidstrs))
qsiim = sql.select(self.shadow_instance_id_mappings).where(
self.shadow_instance_id_mappings.c.uuid.in_(self.uuidstrs)
)
rows = self.conn.execute(qsiim).fetchall()
self.assertEqual(len(rows), 0)
qsi = sql.select([self.shadow_instances]).\
where(self.shadow_instances.c.uuid.in_(self.uuidstrs))
qsi = sql.select(self.shadow_instances).where(
self.shadow_instances.c.uuid.in_(self.uuidstrs)
)
rows = self.conn.execute(qsi).fetchall()
self.assertEqual(len(rows), 0)
# Archive 7 rows, which should be 4 in one table and 3 in the other.
@@ -6112,13 +6121,15 @@ class ArchiveTestCase(test.TestCase, ModelsObjectComparatorMixin):
updated_at=timeutils.utcnow())
self.conn.execute(update_statement)
# Verify we have 6 in main
qtl = sql.select([self.task_log]).where(
self.task_log.c.id.in_(range(1, 7)))
qtl = sql.select(self.task_log).where(
self.task_log.c.id.in_(range(1, 7))
)
rows = self.conn.execute(qtl).fetchall()
self.assertEqual(len(rows), 6)
# Verify we have 0 in shadow
qstl = sql.select([self.shadow_task_log]).where(
self.shadow_task_log.c.id.in_(range(1, 7)))
qstl = sql.select(self.shadow_task_log).where(
self.shadow_task_log.c.id.in_(range(1, 7))
)
rows = self.conn.execute(qstl).fetchall()
self.assertEqual(len(rows), 0)
# Make sure 'before' comparison is for < not <=