From cfbb7678a9bd0bbec8c9d28eeaf0717c13b9434b Mon Sep 17 00:00:00 2001 From: echel0n Date: Fri, 27 Jun 2014 20:33:31 -0700 Subject: [PATCH] Fixed duplicate tv cache issue. --- sickbeard/tvcache.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/sickbeard/tvcache.py b/sickbeard/tvcache.py index 39727d0a..06137dfe 100644 --- a/sickbeard/tvcache.py +++ b/sickbeard/tvcache.py @@ -55,8 +55,14 @@ class CacheDBConnection(db.DBConnection): self.action( "CREATE TABLE [" + providerName + "] (name TEXT, season NUMERIC, episodes TEXT, indexerid NUMERIC, url TEXT, time NUMERIC, quality TEXT)") else: - # remove duplicates - self.action("DELETE FROM " + providerName + " WHERE url NOT IN (SELECT url FROM " + providerName + " GROUP BY url)") + sqlResults = self.select( + "SELECT url, COUNT(url) as count FROM [" + providerName + "] GROUP BY url HAVING count > 1") + + for cur_dupe in sqlResults: + self.action("DELETE FROM [" + providerName + "] WHERE url = ?", [cur_dupe["url"]]) + + # add unique index to prevent further dupes from happening if one does not exist + self.action("CREATE UNIQUE INDEX IF NOT EXISTS idx_url ON " + providerName + " (url)") except Exception, e: if str(e) != "table [" + providerName + "] already exists": raise @@ -69,13 +75,6 @@ class CacheDBConnection(db.DBConnection): if str(e) != "table lastUpdate already exists": raise - - # Create unique index for provider table to prevent duplicate entries - try: - self.action("CREATE UNIQUE INDEX IF NOT EXISTS idx_url ON " + providerName + " (url)") - except Exception, e: - raise - class TVCache(): def __init__(self, provider):