From e0fdfac0631506c73b0beae3adeeae52efc1394b Mon Sep 17 00:00:00 2001 From: croneter Date: Wed, 24 Oct 2018 18:40:44 +0200 Subject: [PATCH] Check Plex DB version and force-reset if necessary --- resources/lib/plex_db/common.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/resources/lib/plex_db/common.py b/resources/lib/plex_db/common.py index 283f0f59..ed8cebef 100644 --- a/resources/lib/plex_db/common.py +++ b/resources/lib/plex_db/common.py @@ -196,7 +196,37 @@ def initialize(): Run once upon PKC startup to verify that plex db exists. """ with PlexDBBase() as plexdb: + init = False # Create the tables for the plex database + try: + plexdb.cursor.execute('SELECT * FROM version') + version = plexdb.cursor.fetchone()[0] + except (utils.OperationalError, TypeError): + init = True + else: + init = utils.compare_version(version, v.MIN_DB_VERSION) + if not init: + return + # Delete all tables + from logging import getLogger + LOG = getLogger('PLEX.plex_db') + LOG.warn('Need to reset the Plex database') + plexdb.cursor.execute(''' + SELECT name FROM sqlite_master WHERE type = 'table' + ''') + tables = [x[0] for x in plexdb.cursor] + for table in tables: + query = 'DROP table IF EXISTS %s' % table + plexdb.cursor.execute(query) + # Set them up again + plexdb.cursor.execute(''' + CREATE TABLE IF NOT EXISTS version( + idVersion TEXT) + ''') + plexdb.cursor.execute(''' + INSERT OR REPLACE INTO version(idVersion) + VALUES (?) + ''', (v.ADDON_VERSION, )) plexdb.cursor.execute(''' CREATE TABLE IF NOT EXISTS sections( section_id INTEGER PRIMARY KEY,