From 96c72e73df6bfc43831bfa1edcb6155c08a00cd0 Mon Sep 17 00:00:00 2001
From: Jake Moshenko <jake.moshenko@coreos.com>
Date: Tue, 12 Jan 2016 11:43:07 -0500
Subject: [PATCH] Clean up torrents before removing referenced storages

---
 data/model/storage.py | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/data/model/storage.py b/data/model/storage.py
index 5c7ed41dd..b6a542474 100644
--- a/data/model/storage.py
+++ b/data/model/storage.py
@@ -69,6 +69,12 @@ def garbage_collect_storage(storage_id_whitelist):
                                                     storage_id_whitelist,
                                                     (ImageStorage.id,)).alias('osq'))
     if len(orphaned_storages) > 0:
+      torrents_removed = (TorrentInfo
+                          .delete()
+                          .where(TorrentInfo.storage << orphaned_storages)
+                          .execute())
+      logger.debug('Removed %s torrent info records', torrents_removed)
+
       storages_removed = (ImageStorage
                           .delete()
                           .where(ImageStorage.id << orphaned_storages)