MySQL doesnt allow us to use the table we are modifying in a subquery.

This commit is contained in:
Jake Moshenko 2014-10-17 17:01:55 -04:00
parent efe8825a15
commit 6ca0115b5e

View file

@ -1421,18 +1421,19 @@ def garbage_collect_storage(storage_id_whitelist):
logger.debug('Garbage collecting storage from candidates: %s', storage_id_whitelist) logger.debug('Garbage collecting storage from candidates: %s', storage_id_whitelist)
with config.app_config['DB_TRANSACTION_FACTORY'](db): with config.app_config['DB_TRANSACTION_FACTORY'](db):
# Find out which derived storages will be removed, and add them to the whitelist # Find out which derived storages will be removed, and add them to the whitelist
orphaned_from_candidates = orphaned_storage_query(ImageStorage.select(), storage_id_whitelist) orphaned_from_candidates = list(orphaned_storage_query(ImageStorage.select(ImageStorage.id),
storage_id_whitelist))
derived_to_remove = (ImageStorage derived_to_remove = (ImageStorage
.select(ImageStorage.id) .select(ImageStorage.id)
.join(DerivedImageStorage, .join(DerivedImageStorage,
on=(ImageStorage.id == DerivedImageStorage.derivative)) on=(ImageStorage.id == DerivedImageStorage.derivative))
.where(DerivedImageStorage.source << orphaned_from_candidates.clone())) .where(DerivedImageStorage.source << orphaned_from_candidates))
storage_id_whitelist.update({derived.id for derived in derived_to_remove}) storage_id_whitelist.update({derived.id for derived in derived_to_remove})
# Remove the dervived image storages with sources of orphaned storages # Remove the dervived image storages with sources of orphaned storages
(DerivedImageStorage (DerivedImageStorage
.delete() .delete()
.where(DerivedImageStorage.source << orphaned_from_candidates.clone()) .where(DerivedImageStorage.source << orphaned_from_candidates)
.execute()) .execute())
# Track all of the data that should be removed from blob storage # Track all of the data that should be removed from blob storage
@ -1450,14 +1451,14 @@ def garbage_collect_storage(storage_id_whitelist):
placements_subquery = placements_to_remove.clone().select(ImageStoragePlacement.id) placements_subquery = placements_to_remove.clone().select(ImageStoragePlacement.id)
(ImageStoragePlacement (ImageStoragePlacement
.delete() .delete()
.where(ImageStoragePlacement.id << placements_subquery) .where(ImageStoragePlacement.id << list(placements_subquery))
.execute()) .execute())
# Remove the all orphaned storages # Remove the all orphaned storages
(ImageStorage (ImageStorage
.delete() .delete()
.where(ImageStorage.id << orphaned_storage_query(ImageStorage.select(ImageStorage.id), .where(ImageStorage.id << list(orphaned_storage_query(ImageStorage.select(ImageStorage.id),
storage_id_whitelist)) storage_id_whitelist)))
.execute()) .execute())
# Delete the actual blob storage # Delete the actual blob storage