Optimize resetting of Kodi and Plex databases

- Fixes #527
This commit is contained in:
croneter 2018-10-10 19:18:06 +02:00
parent 06f1045656
commit 25675a9136
6 changed files with 68 additions and 87 deletions

View file

@ -640,7 +640,7 @@ class InitialSetup(object):
# New installation - make sure we start with a clean slate # New installation - make sure we start with a clean slate
from . import kodidb_functions from . import kodidb_functions
kodidb_functions.wipe_kodi_dbs() kodidb_functions.wipe_dbs()
if goto_settings is False: if goto_settings is False:
# Open Settings page now? You will need to restart! # Open Settings page now? You will need to restart!

View file

@ -100,22 +100,6 @@ class KodiDBMethods(object):
1, 1,
0)) 0))
def setup_music_db_dummy_entries(self):
"""
Kodi Krypton Krypton has some dummy first entries that we might've
deleted
idArtist: 1 strArtist: [Missing Tag]
strMusicBrainzArtistID: Artist Tag Missing
"""
query = '''
INSERT OR REPLACE INTO artist(
idArtist, strArtist, strMusicBrainzArtistID)
VALUES (?, ?, ?)
'''
self.cursor.execute(query, (1,
'[Missing Tag]',
'Artist Tag Missing'))
def parent_path_id(self, path): def parent_path_id(self, path):
""" """
Video DB: Adds all subdirectories to path table while setting a "trail" Video DB: Adds all subdirectories to path table while setting a "trail"
@ -1264,7 +1248,34 @@ def kodiid_from_filename(path, kodi_type=None, db_type=None):
return kodi_id, kodi_type return kodi_id, kodi_type
def wipe_kodi_dbs(): def setup_kodi_default_entries():
"""
Makes sure that we retain the Kodi standard databases. E.g. that there
is a dummy artist with ID 1
"""
if utils.settings('enableMusic') == 'true':
with GetKodiDB('music') as kodi_db:
query = '''
INSERT OR REPLACE INTO artist(
idArtist, strArtist, strMusicBrainzArtistID)
VALUES (?, ?, ?)
'''
kodi_db.cursor.execute(query, (1,
'[Missing Tag]',
'Artist Tag Missing'))
if v.KODIVERSION >= 18:
query = '''
INSERT OR REPLACE INTO versiontagscan(
idVersion, iNeedsScan, lastscanned)
VALUES (?, ?, ?)
'''
kodi_db.cursor.execute(query, (v.DB_MUSIC_VERSION[v.KODIVERSION],
0,
utils.unix_date_to_kodi(
utils.unix_timestamp())))
def wipe_dbs():
""" """
Completely resets the Kodi databases 'video', 'texture' and 'music' (if Completely resets the Kodi databases 'video', 'texture' and 'music' (if
music sync is enabled) music sync is enabled)
@ -1272,7 +1283,7 @@ def wipe_kodi_dbs():
LOG.warn('Wiping Kodi databases!') LOG.warn('Wiping Kodi databases!')
query = "SELECT name FROM sqlite_master WHERE type = 'table'" query = "SELECT name FROM sqlite_master WHERE type = 'table'"
kinds = ['video', 'texture'] kinds = ['video', 'texture']
if state.ENABLE_MUSIC: if utils.settings('enableMusic') == 'true':
LOG.info('Also deleting music database') LOG.info('Also deleting music database')
kinds.append('music') kinds.append('music')
for db in kinds: for db in kinds:
@ -1282,10 +1293,14 @@ def wipe_kodi_dbs():
tables = [i[0] for i in tables] tables = [i[0] for i in tables]
if 'version' in tables: if 'version' in tables:
tables.remove('version') tables.remove('version')
if 'versiontagscan' in tables:
tables.remove('versiontagscan')
for table in tables: for table in tables:
delete_query = 'DELETE FROM %s' % table delete_query = 'DELETE FROM %s' % table
kodi_db.cursor.execute(delete_query) kodi_db.cursor.execute(delete_query)
setup_kodi_default_entries()
# Make sure Kodi knows we wiped the databases
import xbmc import xbmc
xbmc.executebuiltin('UpdateLibrary(video)') xbmc.executebuiltin('UpdateLibrary(video)')
if state.ENABLE_MUSIC: if utils.settings('enableMusic') == 'true':
xbmc.executebuiltin('UpdateLibrary(music)') xbmc.executebuiltin('UpdateLibrary(music)')

View file

@ -1546,10 +1546,6 @@ class LibrarySync(Thread):
with kodidb.GetKodiDB('video') as kodi_db: with kodidb.GetKodiDB('video') as kodi_db:
# Setup the paths for addon-paths (even when using direct paths) # Setup the paths for addon-paths (even when using direct paths)
kodi_db.setup_path_table() kodi_db.setup_path_table()
with kodidb.GetKodiDB('music') as kodi_db:
# Hack for a dummy genre entry - even when we're not using
# Plex Music
kodi_db.setup_music_db_dummy_entries()
utils.window('plex_dbScan', clear=True) utils.window('plex_dbScan', clear=True)
state.DB_SCAN = False state.DB_SCAN = False
playlist_monitor = None playlist_monitor = None

View file

@ -490,3 +490,17 @@ class Plex_DB_Functions():
else: else:
raise RuntimeError('Cannot delete playlist: %s' % playlist) raise RuntimeError('Cannot delete playlist: %s' % playlist)
self.plexcursor.execute(query, (var, )) self.plexcursor.execute(query, (var, ))
def wipe_dbs():
"""
Completely resets the Plex database
"""
query = "SELECT name FROM sqlite_master WHERE type = 'table'"
with Get_Plex_DB() as plex_db:
plex_db.plexcursor.execute(query)
tables = plex_db.plexcursor.fetchall()
tables = [i[0] for i in tables]
for table in tables:
delete_query = 'DELETE FROM %s' % table
plex_db.plexcursor.execute(delete_query)

View file

@ -469,81 +469,37 @@ def wipe_database():
as Plex databases completely. as Plex databases completely.
Will also delete all cached artwork. Will also delete all cached artwork.
""" """
LOG.warn('Start wiping')
# Clean up the playlists # Clean up the playlists
delete_playlists() delete_playlists()
# Clean up the video nodes # Clean up the video nodes
delete_nodes() delete_nodes()
from . import kodidb_functions
# Wipe the kodi databases kodidb_functions.wipe_dbs()
LOG.info("Resetting the Kodi video database.") from . import plexdb_functions
connection = kodi_sql('video')
cursor = connection.cursor()
cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
rows = cursor.fetchall()
for row in rows:
tablename = row[0]
if tablename != "version":
cursor.execute("DELETE FROM %s" % tablename)
connection.commit()
cursor.close()
if settings('enableMusic') == "true":
LOG.info("Resetting the Kodi music database.")
connection = kodi_sql('music')
cursor = connection.cursor()
cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"')
rows = cursor.fetchall()
for row in rows:
tablename = row[0]
if tablename != "version":
cursor.execute("DELETE FROM %s" % tablename)
connection.commit()
cursor.close()
# Wipe the Plex database
LOG.info("Resetting the Plex database.")
connection = kodi_sql('plex')
cursor = connection.cursor()
# First get the paths to all synced playlists # First get the paths to all synced playlists
playlist_paths = [] playlist_paths = []
cursor.execute('SELECT kodi_path FROM playlists') with plexdb_functions.Get_Plex_DB() as plex_db:
for entry in cursor.fetchall(): plex_db.plexcursor.execute('SELECT kodi_path FROM playlists')
playlist_paths.append(entry[0]) for entry in plex_db.plexcursor.fetchall():
cursor.execute('SELECT tbl_name FROM sqlite_master WHERE type="table"') playlist_paths.append(entry[0])
rows = cursor.fetchall() plexdb_functions.wipe_dbs()
for row in rows:
tablename = row[0]
if tablename != "version":
cursor.execute("DELETE FROM %s" % tablename)
connection.commit()
cursor.close()
# Delete all synced playlists # Delete all synced playlists
for path in playlist_paths: for path in playlist_paths:
try: try:
path_ops.remove(path) path_ops.remove(path)
LOG.info('Removed playlist %s', path)
except (OSError, IOError): except (OSError, IOError):
pass LOG.warn('Could not remove playlist %s', path)
LOG.info("Resetting all cached artwork.") LOG.info("Resetting all cached artwork.")
# Remove all existing textures first # Remove all cached artwork
path = path_ops.translate_path("special://thumbnails/") path = path_ops.translate_path("special://thumbnails/")
if path_ops.exists(path): if path_ops.exists(path):
path_ops.rmtree(path, ignore_errors=True) path_ops.rmtree(path, ignore_errors=True)
# remove all existing data from texture DB
connection = kodi_sql('texture')
cursor = connection.cursor()
query = 'SELECT tbl_name FROM sqlite_master WHERE type=?'
cursor.execute(query, ("table", ))
rows = cursor.fetchall()
for row in rows:
table_name = row[0]
if table_name != "version":
cursor.execute("DELETE FROM %s" % table_name)
connection.commit()
cursor.close()
# reset the install run flag # reset the install run flag
settings('SyncInstallRunDone', value="false") settings('SyncInstallRunDone', value="false")
LOG.info('Wiping done')
def reset(ask_user=True): def reset(ask_user=True):

View file

@ -87,26 +87,26 @@ PKC_MACHINE_IDENTIFIER = None
MIN_DB_VERSION = '2.0.27' MIN_DB_VERSION = '2.0.27'
# Database paths # Database paths
_DB_VIDEO_VERSION = { DB_VIDEO_VERSION = {
17: 107, # Krypton 17: 107, # Krypton
18: 112 # Leia 18: 112 # Leia
} }
DB_VIDEO_PATH = try_decode(xbmc.translatePath( DB_VIDEO_PATH = try_decode(xbmc.translatePath(
"special://database/MyVideos%s.db" % _DB_VIDEO_VERSION[KODIVERSION])) "special://database/MyVideos%s.db" % DB_VIDEO_VERSION[KODIVERSION]))
_DB_MUSIC_VERSION = { DB_MUSIC_VERSION = {
17: 60, # Krypton 17: 60, # Krypton
18: 72 # Leia 18: 72 # Leia
} }
DB_MUSIC_PATH = try_decode(xbmc.translatePath( DB_MUSIC_PATH = try_decode(xbmc.translatePath(
"special://database/MyMusic%s.db" % _DB_MUSIC_VERSION[KODIVERSION])) "special://database/MyMusic%s.db" % DB_MUSIC_VERSION[KODIVERSION]))
_DB_TEXTURE_VERSION = { DB_TEXTURE_VERSION = {
17: 13, # Krypton 17: 13, # Krypton
18: 13 # Leia 18: 13 # Leia
} }
DB_TEXTURE_PATH = try_decode(xbmc.translatePath( DB_TEXTURE_PATH = try_decode(xbmc.translatePath(
"special://database/Textures%s.db" % _DB_TEXTURE_VERSION[KODIVERSION])) "special://database/Textures%s.db" % DB_TEXTURE_VERSION[KODIVERSION]))
DB_PLEX_PATH = try_decode(xbmc.translatePath("special://database/plex.db")) DB_PLEX_PATH = try_decode(xbmc.translatePath("special://database/plex.db"))