diff --git a/resources/lib/itemtypes.py b/resources/lib/itemtypes.py index 64f3c452..50f72129 100644 --- a/resources/lib/itemtypes.py +++ b/resources/lib/itemtypes.py @@ -480,6 +480,7 @@ class Movies(Items): artwork.deleteArtwork(kodi_id, kodi_type, kodicursor) if kodi_type == v.KODI_TYPE_MOVIE: + self.kodi_db.delete_countries(kodi_id, kodi_type) # Delete kodi movie and file kodicursor.execute("DELETE FROM movie WHERE idMovie = ?", (kodi_id,)) diff --git a/resources/lib/kodidb_functions.py b/resources/lib/kodidb_functions.py index ee10e2ea..5cda6fc7 100644 --- a/resources/lib/kodidb_functions.py +++ b/resources/lib/kodidb_functions.py @@ -273,6 +273,31 @@ class KodiDBMethods(object): ) self.cursor.execute(query, (country_id, kodiid, mediatype)) + def delete_countries(self, kodi_id, kodi_type): + """ + Assuming that video kodi_id, kodi_type gets deleted, will delete any + associated country links in the table country_link and also deletes + orphaned countries in the table country + """ + # Get all existing links + query = ''' + SELECT country_id FROM country_link + WHERE media_id = ? AND media_type = ? + ''' + self.cursor.execute(query, (kodi_id, kodi_type)) + country_ids = self.cursor.fetchall() + # Delete all links + query = 'DELETE FROM country_link WHERE media_id = ? AND media_type = ?' + self.cursor.execute(query, (kodi_id, kodi_type)) + # Which countries are now orphaned? + query = 'SELECT country_id FROM country_link WHERE country_id = ?' + query_delete = 'DELETE FROM country WHERE country_id = ?' + for country_id in country_ids: + # country_id still in table? + self.cursor.execute(query, (country_id,)) + if self.cursor.fetchone() is None: + self.cursor.execute(query_delete, (country_id,)) + def _getactorid(self, name): """ Crucial für sync speed!