From 373d34a56ed844c8543483d0db6e8b472d471079 Mon Sep 17 00:00:00 2001 From: mcarlton00 Date: Thu, 7 Mar 2024 12:41:56 -0500 Subject: [PATCH] Add automatic database migration for Kodi Omega --- jellyfin_kodi/library.py | 9 ++++++ jellyfin_kodi/objects/kodi/movies.py | 40 +++++++++++++++++++++++---- jellyfin_kodi/objects/kodi/queries.py | 16 +++++++++-- 3 files changed, 58 insertions(+), 7 deletions(-) diff --git a/jellyfin_kodi/library.py b/jellyfin_kodi/library.py index 9de23d67..7e6bcf57 100644 --- a/jellyfin_kodi/library.py +++ b/jellyfin_kodi/library.py @@ -11,6 +11,7 @@ from six.moves import queue as Queue from kodi_six import xbmc, xbmcgui from .objects import Movies, TVShows, MusicVideos, Music +from .objects.kodi import Movies as KodiDb from .database import Database, jellyfin_db, get_sync, save_sync from .full_sync import FullSync from .views import Views @@ -122,6 +123,14 @@ class Library(threading.Thread): # Make sure we always have a version in the database db.add_version((TARGET_DB_VERSION)) + # Video Database Migrations + with Database('video') as videodb: + vid_db = KodiDb(videodb.cursor) + if vid_db.migrations(): + LOG.info('changes detected, reloading skin') + xbmc.executebuiltin('UpdateLibrary(video)') + xbmc.executebuiltin('ReloadSkin()') + @stop def service(self): diff --git a/jellyfin_kodi/objects/kodi/movies.py b/jellyfin_kodi/objects/kodi/movies.py index de1da330..10172c02 100644 --- a/jellyfin_kodi/objects/kodi/movies.py +++ b/jellyfin_kodi/objects/kodi/movies.py @@ -50,7 +50,7 @@ class Movies(Kodi): def add_videoversion(self, *args): self.cursor.execute(QU.check_video_version) - if self.cursor.fetchone()[0]==1 : + if self.cursor.fetchone()[0] == 1: self.cursor.execute(QU.add_video_version, args) def update(self, *args): @@ -61,11 +61,9 @@ class Movies(Kodi): self.cursor.execute(QU.delete_movie, (kodi_id,)) self.cursor.execute(QU.delete_file, (file_id,)) self.cursor.execute(QU.check_video_version) - if self.cursor.fetchone()[0]==1 : + if self.cursor.fetchone()[0] == 1: self.cursor.execute(QU.delete_video_version, (file_id,)) - - def get_rating_id(self, *args): try: @@ -140,4 +138,36 @@ class Movies(Kodi): self.cursor.execute(QU.delete_movie_set, args) def delete_boxset(self, *args): - self.cursor.execute(QU.delete_set, args) \ No newline at end of file + self.cursor.execute(QU.delete_set, args) + + def migrations(self): + ''' + Used to trigger required database migrations for new versions + ''' + self.cursor.execute(QU.get_version) + version_id = self.cursor.fetchone()[0] + changes = False + + # Will run every time Kodi starts, but will be fast enough on + # subsequent runs to not be a meaningful delay + if version_id >= 131: + changes = self.omega_migration() + + return changes + + def omega_migration(self): + ''' + Adds a video version for all existing movies + ''' + LOG.info('Starting migration for Omega database changes') + # Tracks if this migration made any changes + changes = False + self.cursor.execute(QU.get_missing_versions) + + # Sets all existing movies without a version to standard version + for entry in self.cursor.fetchall(): + self.add_videoversion(entry[0], entry[1], "movie", "0", 40400) + changes = True + + LOG.info('Omega database migration is complete') + return changes diff --git a/jellyfin_kodi/objects/kodi/queries.py b/jellyfin_kodi/objects/kodi/queries.py index 49d5b27e..02069d8c 100644 --- a/jellyfin_kodi/objects/kodi/queries.py +++ b/jellyfin_kodi/objects/kodi/queries.py @@ -322,7 +322,7 @@ VALUES (?, ?, ?, ?, ?) check_video_version = """ SELECT COUNT(name) FROM sqlite_master WHERE type='table' AND name='videoversion' """ -add_video_version_obj = ["{FileId}","{MovieId}","movie","0",40400] +add_video_version_obj = ["{FileId}", "{MovieId}", "movie", "0", 40400] add_musicvideo = """ INSERT INTO musicvideo(idMVideo, idFile, c00, c04, c05, c06, c07, c08, c09, c10, c11, c12, premiered) @@ -407,7 +407,7 @@ VALUES (?, ?, ?) # Resulting in duplicates insert_link_if_not_exists = """ INSERT INTO {LinkType}(actor_id, media_id, media_type) -SELECT ?, ?, ? +SELECT ?, ?, ? WHERE NOT EXISTS(SELECT 1 FROM {LinkType} WHERE actor_id = ? AND media_id = ? AND media_type = ?) """ update_movie = """ @@ -581,3 +581,15 @@ WHERE media_id = ? AND media_type = ? AND type LIKE ? """ +get_missing_versions = """ +SELECT idFile,idMovie +FROM movie +WHERE NOT EXISTS ( + SELECT NULL FROM videoversion + WHERE videoversion.idMedia = movie.idMovie +) +""" +get_version = """ +SELECT idVersion +FROM version +"""