[sqlalchemy-20] Provide SQL "case" expression correct input paremeters

The "case" expression, as implemented in SQLAlchemy, consists in a set
of tuples of two elements: a column expression and a value. From the
SQLAlchemy "case" signature:
  *whens: Union[
      typing_Tuple[_ColumnExpressionArgument[bool], Any],
      Mapping[Any, Any],
  ],

Closes-Bug: #2012705
Change-Id: I2dfc497f0ac9cb4279cae7f851463c6721bf9d77
This commit is contained in:
Rodolfo Alonso Hernandez
2023-03-23 06:56:13 +01:00
parent e4da60740b
commit f42f1cfa69

View File

@@ -57,13 +57,17 @@ _TYPES_PRIORITY_ORDER = (
# The order in which the resources should be created or updated by the
# maintenance task: Root ones first and leafs at the end.
MAINTENANCE_CREATE_UPDATE_TYPE_ORDER = {
t: n for n, t in enumerate(_TYPES_PRIORITY_ORDER, 1)}
MAINTENANCE_CREATE_UPDATE_TYPE_ORDER = [
(ovn_models.OVNRevisionNumbers.resource_type == resource_type, idx)
for idx, resource_type in enumerate(_TYPES_PRIORITY_ORDER, 1)
]
# The order in which the resources should be deleted by the maintenance
# task: Leaf ones first and roots at the end.
MAINTENANCE_DELETE_TYPE_ORDER = {
t: n for n, t in enumerate(reversed(_TYPES_PRIORITY_ORDER), 1)}
MAINTENANCE_DELETE_TYPE_ORDER = [
(ovn_models.OVNRevisionNumbers.resource_type == resource_type, idx)
for idx, resource_type in enumerate(reversed(_TYPES_PRIORITY_ORDER), 1)
]
INITIAL_REV_NUM = -1
@@ -194,8 +198,7 @@ def get_inconsistent_resources(context):
:returns: A list of objects which the revision number from the
ovn_revision_number and standardattributes tables differs.
"""
sort_order = sa.case(value=ovn_models.OVNRevisionNumbers.resource_type,
whens=MAINTENANCE_CREATE_UPDATE_TYPE_ORDER)
sort_order = sa.case(*MAINTENANCE_CREATE_UPDATE_TYPE_ORDER)
time_ = (timeutils.utcnow() -
datetime.timedelta(seconds=INCONSISTENCIES_OLDER_THAN))
with db_api.CONTEXT_READER.using(context):
@@ -225,8 +228,7 @@ def get_deleted_resources(context):
the entry will be kept and returned in this list so the maintenance
thread can later fix it.
"""
sort_order = sa.case(value=ovn_models.OVNRevisionNumbers.resource_type,
whens=MAINTENANCE_DELETE_TYPE_ORDER)
sort_order = sa.case(*MAINTENANCE_DELETE_TYPE_ORDER)
with db_api.CONTEXT_READER.using(context):
return context.session.query(ovn_models.OVNRevisionNumbers).filter_by(
standard_attr_id=None).order_by(sort_order).all()