Skip to content

Commit

Permalink
db: Remove use of non-integer/slice indices
Browse files Browse the repository at this point in the history
Resolve the following sqlalchemy.exc.RemovedIn20Warning:

  Using non-integer/slice indices on Row is deprecated and will be
  removed in version 2.0; please use row._mapping[<key>], or the
  mappings() accessor on the Result object.

For more information, refer to http://sqlalche.me/e/b8d9.

Change-Id: I8b845a29fa21f4ca8340d91bf5c6394094c6ab97
Signed-off-by: Stephen Finucane <stephenfin@redhat.com>
  • Loading branch information
stephenfin committed Apr 7, 2022
1 parent cb6bfc5 commit b3fe04f
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 17 deletions.
5 changes: 3 additions & 2 deletions placement/objects/allocation.py
Expand Up @@ -163,6 +163,7 @@ def _check_capacity_exceeded(ctx, allocs):
usage_map = {}
provs_with_inv = set()
for record in records:
record = record._mapping
map_key = (record['uuid'], record['resource_class_id'])
if map_key in usage_map:
raise KeyError("%s already in usage_map, bad query" % str(map_key))
Expand Down Expand Up @@ -272,7 +273,7 @@ def _get_allocations_by_provider_id(ctx, rp_id):
).select_from(users_join)
sel = sel.where(allocs.c.resource_provider_id == rp_id)

return [dict(r) for r in ctx.session.execute(sel)]
return [dict(r._mapping) for r in ctx.session.execute(sel)]


@db_api.placement_context_manager.reader
Expand Down Expand Up @@ -314,7 +315,7 @@ def _get_allocations_by_consumer_uuid(ctx, consumer_uuid):
).select_from(user_join)
sel = sel.where(allocs.c.consumer_id == consumer_uuid)

return [dict(r) for r in ctx.session.execute(sel)]
return [dict(r._mapping) for r in ctx.session.execute(sel)]


@oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True)
Expand Down
3 changes: 2 additions & 1 deletion placement/objects/allocation_candidate.py
Expand Up @@ -526,6 +526,7 @@ def _build_provider_summaries(context, rw_ctx, root_ids, prov_traits):
# ProviderSummary objects containing one or more ProviderSummaryResource
# objects representing the resources the provider has inventory for.
for usage in usages:
usage = usage._mapping
rp_id = usage['resource_provider_id']
summary = rw_ctx.summaries_by_id.get(rp_id)
if not summary:
Expand Down Expand Up @@ -944,5 +945,5 @@ def _provider_ids_from_root_ids(context, root_ids):

ret = {}
for r in context.session.execute(sel, {'root_ids': list(root_ids)}):
ret[r['id']] = r
ret[r._mapping['id']] = r
return ret
2 changes: 1 addition & 1 deletion placement/objects/consumer.py
Expand Up @@ -126,7 +126,7 @@ def _get_consumer_by_uuid(ctx, uuid):
if not res:
raise exception.ConsumerNotFound(uuid=uuid)

return dict(res)
return dict(res._mapping)


@db_api.placement_context_manager.writer
Expand Down
2 changes: 1 addition & 1 deletion placement/objects/inventory.py
Expand Up @@ -97,4 +97,4 @@ def _get_inventory_by_provider_id(ctx, rp_id):
)
sel = sel.where(inv.c.resource_provider_id == rp_id)

return [dict(r) for r in ctx.session.execute(sel)]
return [dict(r._mapping) for r in ctx.session.execute(sel)]
2 changes: 1 addition & 1 deletion placement/objects/project.py
Expand Up @@ -50,7 +50,7 @@ def _get_project_by_external_id(ctx, external_id):
if not res:
raise exception.ProjectNotFound(external_id=external_id)

return dict(res)
return dict(res._mapping)


class Project(object):
Expand Down
4 changes: 2 additions & 2 deletions placement/objects/resource_class.py
Expand Up @@ -68,7 +68,7 @@ def get_by_name(cls, context, name):
:raises: ResourceClassNotFound if no such resource class was found
"""
rc = context.rc_cache.all_from_string(name)
rc = context.rc_cache.all_from_string(name)._mapping
obj = cls(context, id=rc['id'], name=rc['name'],
updated_at=rc['updated_at'], created_at=rc['created_at'])
return obj
Expand Down Expand Up @@ -215,7 +215,7 @@ def ensure_sync(ctx):
def get_all(context):
"""Get a list of all the resource classes in the database."""
resource_classes = context.rc_cache.get_all()
return [ResourceClass(context, **rc) for rc in resource_classes]
return [ResourceClass(context, **rc._mapping) for rc in resource_classes]


@oslo_db_api.wrap_db_retry(max_retries=5, retry_on_deadlock=True)
Expand Down
10 changes: 6 additions & 4 deletions placement/objects/resource_provider.py
Expand Up @@ -140,7 +140,7 @@ def _update_inventory_for_provider(ctx, rp, inv_list, to_update):
sa.and_(
_ALLOC_TBL.c.resource_provider_id == rp.id,
_ALLOC_TBL.c.resource_class_id == rc_id))
allocations = ctx.session.execute(allocation_query).first()
allocations = ctx.session.execute(allocation_query).first()._mapping
if (allocations and
allocations['usage'] is not None and
allocations['usage'] > inv_record.capacity):
Expand Down Expand Up @@ -287,7 +287,7 @@ def _get_provider_by_uuid(context, uuid):
if not res:
raise exception.NotFound(
'No resource provider with uuid %s found' % uuid)
return dict(res)
return dict(res._mapping)


@db_api.placement_context_manager.reader
Expand Down Expand Up @@ -460,7 +460,7 @@ def _set_traits(context, rp, traits):
"""
# Get the internal IDs of our existing traits
existing_traits = trait_obj.get_traits_by_provider_id(context, rp.id)
existing_traits = set(rec['id'] for rec in existing_traits)
existing_traits = set(rec.id for rec in existing_traits)
want_traits = set(trait.id for trait in traits)

to_add = want_traits - existing_traits
Expand Down Expand Up @@ -1042,4 +1042,6 @@ def get_all_by_filters(context, filters=None):
:type filters: dict
"""
resource_providers = _get_all_by_filters_from_db(context, filters)
return [ResourceProvider(context, **rp) for rp in resource_providers]
return [
ResourceProvider(context, **rp._mapping) for rp in resource_providers
]
16 changes: 13 additions & 3 deletions placement/objects/trait.py
Expand Up @@ -18,6 +18,7 @@
from oslo_db import exception as db_exc
from oslo_log import log as logging
import sqlalchemy as sa
from sqlalchemy.engine import row as sa_row

from placement.db.sqlalchemy import models
from placement import db_api
Expand Down Expand Up @@ -89,7 +90,7 @@ def create(self):

@classmethod
def get_by_name(cls, context, name):
db_trait = context.trait_cache.all_from_string(name)
db_trait = context.trait_cache.all_from_string(name)._mapping
return cls._from_db_object(context, cls(context), db_trait)

@staticmethod
Expand Down Expand Up @@ -147,15 +148,24 @@ def ensure_sync(ctx):

def get_all(context, filters=None):
db_traits = _get_all_from_db(context, filters)
return [Trait(context, **data) for data in db_traits]
# FIXME(stephenfin): This is necessary because our cached object type is
# different from what we're getting from the database. We should use the
# same
result = []
for trait in db_traits:
if isinstance(trait, sa_row.LegacyRow):
result.append(Trait(context, **trait._mapping))
else:
result.append(Trait(context, **trait))
return result


def get_all_by_resource_provider(context, rp):
"""Returns a list containing Trait objects for any trait
associated with the supplied resource provider.
"""
db_traits = get_traits_by_provider_id(context, rp.id)
return [Trait(context, **data) for data in db_traits]
return [Trait(context, **data._mapping) for data in db_traits]


@db_api.placement_context_manager.reader
Expand Down
2 changes: 1 addition & 1 deletion placement/objects/user.py
Expand Up @@ -53,7 +53,7 @@ def _get_user_by_external_id(ctx, external_id):
if not res:
raise exception.UserNotFound(external_id=external_id)

return dict(res)
return dict(res._mapping)


class User(object):
Expand Down
2 changes: 1 addition & 1 deletion placement/tests/functional/db/test_attribute_cache.py
Expand Up @@ -51,7 +51,7 @@ def test_rc_cache_std_db(self):
def test_standard_has_time_fields(self):
cache = attribute_cache.ResourceClassCache(self.context)

vcpu_class = dict(cache.all_from_string('VCPU'))
vcpu_class = dict(cache.all_from_string('VCPU')._mapping)
expected = {'id': 0, 'name': 'VCPU', 'updated_at': None,
'created_at': None}
expected_fields = sorted(expected.keys())
Expand Down

0 comments on commit b3fe04f

Please sign in to comment.