diff --git a/data/model/legacy.py b/data/model/legacy.py index bec4bc1f4..c8e304ecb 100644 --- a/data/model/legacy.py +++ b/data/model/legacy.py @@ -1423,18 +1423,20 @@ def garbage_collect_storage(storage_id_whitelist): # Find out which derived storages will be removed, and add them to the whitelist orphaned_from_candidates = list(orphaned_storage_query(ImageStorage.select(ImageStorage.id), storage_id_whitelist)) - derived_to_remove = (ImageStorage - .select(ImageStorage.id) - .join(DerivedImageStorage, - on=(ImageStorage.id == DerivedImageStorage.derivative)) - .where(DerivedImageStorage.source << orphaned_from_candidates)) - storage_id_whitelist.update({derived.id for derived in derived_to_remove}) - # Remove the dervived image storages with sources of orphaned storages - (DerivedImageStorage - .delete() - .where(DerivedImageStorage.source << orphaned_from_candidates) - .execute()) + if len(orphaned_from_candidates) > 0: + derived_to_remove = (ImageStorage + .select(ImageStorage.id) + .join(DerivedImageStorage, + on=(ImageStorage.id == DerivedImageStorage.derivative)) + .where(DerivedImageStorage.source << orphaned_from_candidates)) + storage_id_whitelist.update({derived.id for derived in derived_to_remove}) + + # Remove the dervived image storages with sources of orphaned storages + (DerivedImageStorage + .delete() + .where(DerivedImageStorage.source << orphaned_from_candidates) + .execute()) # Track all of the data that should be removed from blob storage placements_to_remove = orphaned_storage_query(ImageStoragePlacement @@ -1448,18 +1450,21 @@ def garbage_collect_storage(storage_id_whitelist): paths_to_remove = placements_query_to_paths_set(placements_to_remove.clone()) # Remove the placements for orphaned storages - placements_subquery = placements_to_remove.clone().select(ImageStoragePlacement.id) - (ImageStoragePlacement - .delete() - .where(ImageStoragePlacement.id << list(placements_subquery)) - .execute()) + placements_subquery = list(placements_to_remove.clone().select(ImageStoragePlacement.id)) + if len(placements_subquery) > 0: + (ImageStoragePlacement + .delete() + .where(ImageStoragePlacement.id << list(placements_subquery)) + .execute()) # Remove the all orphaned storages - (ImageStorage - .delete() - .where(ImageStorage.id << list(orphaned_storage_query(ImageStorage.select(ImageStorage.id), - storage_id_whitelist))) - .execute()) + orphaned_storages = list(orphaned_storage_query(ImageStorage.select(ImageStorage.id), + storage_id_whitelist)) + if len(orphaned_storages) > 0: + (ImageStorage + .delete() + .where(ImageStorage.id << orphaned_storages) + .execute()) # Delete the actual blob storage for location_name, image_path in paths_to_remove: