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
from . import kodidb_functions
kodidb_functions.wipe_kodi_dbs()
kodidb_functions.wipe_dbs()
if goto_settings is False:
# Open Settings page now? You will need to restart!

View file

@ -100,22 +100,6 @@ class KodiDBMethods(object):
1,
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):
"""
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
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
music sync is enabled)
@ -1272,7 +1283,7 @@ def wipe_kodi_dbs():
LOG.warn('Wiping Kodi databases!')
query = "SELECT name FROM sqlite_master WHERE type = 'table'"
kinds = ['video', 'texture']
if state.ENABLE_MUSIC:
if utils.settings('enableMusic') == 'true':
LOG.info('Also deleting music database')
kinds.append('music')
for db in kinds:
@ -1282,10 +1293,14 @@ def wipe_kodi_dbs():
tables = [i[0] for i in tables]
if 'version' in tables:
tables.remove('version')
if 'versiontagscan' in tables:
tables.remove('versiontagscan')
for table in tables:
delete_query = 'DELETE FROM %s' % table
kodi_db.cursor.execute(delete_query)
setup_kodi_default_entries()
# Make sure Kodi knows we wiped the databases
import xbmc
xbmc.executebuiltin('UpdateLibrary(video)')
if state.ENABLE_MUSIC:
if utils.settings('enableMusic') == 'true':
xbmc.executebuiltin('UpdateLibrary(music)')

View file

@ -1546,10 +1546,6 @@ class LibrarySync(Thread):
with kodidb.GetKodiDB('video') as kodi_db:
# Setup the paths for addon-paths (even when using direct paths)
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)
state.DB_SCAN = False
playlist_monitor = None

View file

@ -490,3 +490,17 @@ class Plex_DB_Functions():
else:
raise RuntimeError('Cannot delete playlist: %s' % playlist)
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.
Will also delete all cached artwork.
"""
LOG.warn('Start wiping')
# Clean up the playlists
delete_playlists()
# Clean up the video nodes
delete_nodes()
# Wipe the kodi databases
LOG.info("Resetting the Kodi video database.")
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()
from . import kodidb_functions
kodidb_functions.wipe_dbs()
from . import plexdb_functions
# First get the paths to all synced playlists
playlist_paths = []
cursor.execute('SELECT kodi_path FROM playlists')
for entry in cursor.fetchall():
playlist_paths.append(entry[0])
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()
with plexdb_functions.Get_Plex_DB() as plex_db:
plex_db.plexcursor.execute('SELECT kodi_path FROM playlists')
for entry in plex_db.plexcursor.fetchall():
playlist_paths.append(entry[0])
plexdb_functions.wipe_dbs()
# Delete all synced playlists
for path in playlist_paths:
try:
path_ops.remove(path)
LOG.info('Removed playlist %s', path)
except (OSError, IOError):
pass
LOG.warn('Could not remove playlist %s', path)
LOG.info("Resetting all cached artwork.")
# Remove all existing textures first
# Remove all cached artwork
path = path_ops.translate_path("special://thumbnails/")
if path_ops.exists(path):
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
settings('SyncInstallRunDone', value="false")
LOG.info('Wiping done')
def reset(ask_user=True):

View file

@ -87,26 +87,26 @@ PKC_MACHINE_IDENTIFIER = None
MIN_DB_VERSION = '2.0.27'
# Database paths
_DB_VIDEO_VERSION = {
DB_VIDEO_VERSION = {
17: 107, # Krypton
18: 112 # Leia
}
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
18: 72 # Leia
}
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
18: 13 # Leia
}
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"))