Skip to content

Commit

Permalink
Merge pull request #834 from mcarlton00/omega-migration
Browse files Browse the repository at this point in the history
Add automatic database migration for Kodi Omega
  • Loading branch information
oddstr13 authored Mar 20, 2024
2 parents 2ef6330 + 373d34a commit d1a5c98
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 7 deletions.
9 changes: 9 additions & 0 deletions jellyfin_kodi/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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
Expand Down Expand Up @@ -122,6 +123,14 @@ def check_version(self):
# 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):

Expand Down
40 changes: 35 additions & 5 deletions jellyfin_kodi/objects/kodi/movies.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def add(self, *args):

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):
Expand All @@ -61,11 +61,9 @@ def delete(self, kodi_id, file_id):
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:
Expand Down Expand Up @@ -140,4 +138,36 @@ def remove_from_boxset(self, *args):
self.cursor.execute(QU.delete_movie_set, args)

def delete_boxset(self, *args):
self.cursor.execute(QU.delete_set, args)
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
16 changes: 14 additions & 2 deletions jellyfin_kodi/objects/kodi/queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@
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)
Expand Down Expand Up @@ -407,7 +407,7 @@
# 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 = """
Expand Down Expand Up @@ -581,3 +581,15 @@
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
"""

0 comments on commit d1a5c98

Please sign in to comment.