diff --git a/swh/storage/db.py b/swh/storage/db.py --- a/swh/storage/db.py +++ b/swh/storage/db.py @@ -580,12 +580,13 @@ yield from execute_values_generator( cur, """ - SELECT %s FROM (VALUES %%s) as t(id) + SELECT %s FROM (VALUES %%s) as t(sortkey, id) LEFT JOIN revision ON t.id = revision.id LEFT JOIN person author ON revision.author = author.id LEFT JOIN person committer ON revision.committer = committer.id + ORDER BY sortkey """ % query_keys, - ((id,) for id in revisions)) + ((sortkey, id) for sortkey, id in enumerate(revisions))) def revision_log(self, root_revisions, limit=None, cur=None): cur = self._cursor(cur) @@ -831,11 +832,12 @@ yield from execute_values_generator( cur, """ - SELECT %s FROM (VALUES %%s) as t(id) + SELECT %s FROM (VALUES %%s) as t(sortkey, id) LEFT JOIN release ON t.id = release.id LEFT JOIN person author ON release.author = author.id + ORDER BY sortkey """ % query_keys, - ((id,) for id in releases)) + ((sortkey, id) for sortkey, id in enumerate(releases))) def release_get_random(self, cur=None): return self._get_random_row_from_table( diff --git a/swh/storage/tests/test_storage.py b/swh/storage/tests/test_storage.py --- a/swh/storage/tests/test_storage.py +++ b/swh/storage/tests/test_storage.py @@ -955,6 +955,27 @@ actual_result = swh_storage.revision_add([revision1, revision2]) assert actual_result == {'revision:add': 2} + def test_revision_get_order(self, swh_storage): + normalized_rev1 = Revision.from_dict(data.revision).to_dict() + normalized_rev2 = Revision.from_dict(data.revision2).to_dict() + + add_result = swh_storage.revision_add( + [normalized_rev1, normalized_rev2] + ) + assert add_result == {'revision:add': 2} + + # order 1 + res1 = swh_storage.revision_get( + [normalized_rev1['id'], normalized_rev2['id']] + ) + assert list(res1) == [normalized_rev1, normalized_rev2] + + # order 2 + res2 = swh_storage.revision_get( + [normalized_rev2['id'], normalized_rev1['id']] + ) + assert list(res2) == [normalized_rev2, normalized_rev1] + def test_revision_log(self, swh_storage): # given # data.revision4 -is-child-of-> data.revision3 @@ -1196,6 +1217,27 @@ assert unknown_releases[0] is None + def test_release_get_order(self, swh_storage): + normalized_rel1 = Release.from_dict(data.release).to_dict() + normalized_rel2 = Release.from_dict(data.release2).to_dict() + + add_result = swh_storage.release_add( + [normalized_rel1, normalized_rel2] + ) + assert add_result == {'release:add': 2} + + # order 1 + res1 = swh_storage.release_get( + [normalized_rel1['id'], normalized_rel2['id']] + ) + assert list(res1) == [normalized_rel1, normalized_rel2] + + # order 2 + res2 = swh_storage.release_get( + [normalized_rel2['id'], normalized_rel1['id']] + ) + assert list(res2) == [normalized_rel2, normalized_rel1] + def test_release_get_random(self, swh_storage): swh_storage.release_add([data.release, data.release2, data.release3])