diff --git a/swh/deposit/migrations/0018_migrate_swhids.py b/swh/deposit/migrations/0018_migrate_swhids.py --- a/swh/deposit/migrations/0018_migrate_swhids.py +++ b/swh/deposit/migrations/0018_migrate_swhids.py @@ -17,7 +17,9 @@ REVISION, SNAPSHOT, ) +from swh.model.model import TargetType from swh.storage import get_storage as get_storage_client +from swh.storage.interface import ListOrder SWH_PROVIDER_URL = "https://www.softwareheritage.org" @@ -73,19 +75,42 @@ """Retrieve the snapshot targeting the revision_id for the given origin. """ - all_visits = storage.origin_visit_get(origin) - for visit in all_visits: - if not visit["snapshot"]: - continue - detail_snapshot = storage.snapshot_get(visit["snapshot"]) - if not detail_snapshot: - continue - for branch_name, branch in detail_snapshot["branches"].items(): - if branch["target_type"] == "revision": - revision = branch["target"] - if hash_to_hex(revision) == revision_id: - # Found the snapshot - return hash_to_hex(visit["snapshot"]) + next_page_token_visit = None + while True: + visit_page = storage.origin_visit_get( + origin, order=ListOrder.DESC, page_token=next_page_token_visit + ) + next_page_token_visit = visit_page.next_page_token + for visit in visit_page.result: + next_page_token_visit_status = None + while True: + visit_status_page = storage.origin_visit_status_get( + origin, + visit, + order=ListOrder.DESC, + page_token=next_page_token_visit_status, + ) + next_page_token_visit_status = visit_status_page.next_page_token + for visit_status in visit_status_page.results: + snapshot_id = visit_status.snapshot + if snapshot_id is None: + continue + + detail_snapshot = storage.snapshot_get(snapshot_id) + if not detail_snapshot: + continue + for branch_name, branch in detail_snapshot.branches.items(): + if branch.target_type == TargetType.REVISION: + revision = branch.target + if hash_to_hex(revision) == revision_id: + # Found the snapshot + return hash_to_hex(snapshot_id) + if next_page_token_visit_status is None: + break + + if next_page_token_visit is None: + break + return None