Get rid of oslo_db warning about "id" not being in "sort_keys"

* Now when we query a collection of objects from DB we don't
  use paginated query if query set is not limited
* In case of paginated query we now always insert "id" into
  sort keys to avoid possible looping for multiple paginated
  querying
* Minor cosmetic changes

Change-Id: Ie4707b04a97c7fa17dcf278c74e2e852346ac33c
This commit is contained in:
Renat Akhmerov
2016-07-21 16:50:03 +07:00
parent 0bc0fd0281
commit a55a1c5805
4 changed files with 32 additions and 13 deletions

View File

@@ -144,16 +144,23 @@ def _paginate_query(model, limit=None, marker=None, sort_keys=None,
if not query:
query = _secure_query(model)
sort_keys = sort_keys if sort_keys else []
sort_dirs = sort_dirs if sort_dirs else []
if 'id' not in sort_keys:
sort_keys.append('id')
sort_dirs.append('asc')
query = db_utils.paginate_query(
query,
model,
limit,
sort_keys if sort_keys else {},
sort_keys,
marker=marker,
sort_dirs=sort_dirs
)
return query.all()
return query
def _delete_all(model, session=None, **kwargs):
@@ -170,24 +177,27 @@ def _get_collection(model, insecure=False, limit=None, marker=None,
if fields else ()
)
tags = kwargs.pop('tags', None)
query = (b.model_query(model, *columns) if insecure
else _secure_query(model, *columns))
query = query.filter_by(**kwargs)
tags = kwargs.pop('tags', None)
# To match the tag list, a resource must contain at least all of the
# tags present in the filter parameter.
if tags:
tag_attr = getattr(model, 'tags')
if len(tags) == 1:
expr = tag_attr.contains(tags)
else:
expr = sa.and_(*[tag_attr.contains(tag) for tag in tags])
query = query.filter(expr)
try:
return _paginate_query(
if marker or limit:
# Paginate query only if query set is limited.
query = _paginate_query(
model,
limit,
marker,
@@ -195,10 +205,13 @@ def _get_collection(model, insecure=False, limit=None, marker=None,
sort_dirs,
query
)
try:
return query.all()
except Exception as e:
raise exc.DBQueryEntryError(
"Failed when querying database, error type: %s, "
"error message: %s" % (e.__class__.__name__, e.message)
"error message: %s" % (e.__class__.__name__, str(e))
)

View File

@@ -65,10 +65,12 @@ class ActionTestsV2(base.TestCase):
delimiter='&'
)
# NOTE: 'id' gets included into sort keys automatically with 'desc'
# sorting to avoid pagination looping.
expected_sub_dict = {
'limit': 1,
'sort_keys': 'name',
'sort_dirs': 'desc'
'sort_keys': 'name,id',
'sort_dirs': 'desc,asc'
}
self.assertDictContainsSubset(expected_sub_dict, param_dict)

View File

@@ -88,10 +88,12 @@ class ExecutionTestsV2(base.TestCase):
delimiter='&'
)
# NOTE: 'id' gets included into sort keys automatically with 'desc'
# sorting to avoid pagination looping.
expected_dict = {
'limit': 1,
'sort_keys': 'workflow_name',
'sort_dirs': 'asc',
'sort_keys': 'workflow_name,id',
'sort_dirs': 'asc,asc',
}
self.assertTrue(

View File

@@ -73,10 +73,12 @@ class WorkflowTestsV2(base.TestCase):
delimiter='&'
)
# NOTE: 'id' gets included into sort keys automatically with 'desc'
# sorting to avoid pagination looping.
expected_sub_dict = {
'limit': 1,
'sort_keys': 'name',
'sort_dirs': 'desc'
'sort_keys': 'name,id',
'sort_dirs': 'desc,asc'
}
self.assertDictContainsSubset(expected_sub_dict, param_dict)