From edd696323094543a4ddc1579213f877d4c771518 Mon Sep 17 00:00:00 2001 From: Marcel van der Veldt Date: Wed, 15 Jan 2025 10:01:45 +0100 Subject: [PATCH] Fix: Allow 2 more db schema versions back migration --- music_assistant/controllers/music.py | 49 ++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 7 deletions(-) diff --git a/music_assistant/controllers/music.py b/music_assistant/controllers/music.py index b880267f..e5aa6c4b 100644 --- a/music_assistant/controllers/music.py +++ b/music_assistant/controllers/music.py @@ -1171,22 +1171,57 @@ class MusicController(CoreController): "Migrating database from version %s to %s", prev_version, DB_SCHEMA_VERSION ) - if prev_version <= 9: + if prev_version < 7: raise MusicAssistantError("Database schema version too old to migrate") - if prev_version <= 10: - # add new columns to playlog table + if prev_version <= 7: + # remove redundant artists and provider_mappings columns + for table in ( + DB_TABLE_TRACKS, + DB_TABLE_ALBUMS, + DB_TABLE_ARTISTS, + DB_TABLE_RADIOS, + DB_TABLE_PLAYLISTS, + ): + for column in ("artists", "provider_mappings"): + try: + await self.database.execute(f"ALTER TABLE {table} DROP COLUMN {column}") + except Exception as err: + if "no such column" in str(err): + continue + raise + # add cache_checksum column to playlists try: await self.database.execute( - f"ALTER TABLE {DB_TABLE_PLAYLOG} ADD COLUMN fully_played BOOLEAN" - ) - await self.database.execute( - f"ALTER TABLE {DB_TABLE_PLAYLOG} ADD COLUMN seconds_played INTEGER" + f"ALTER TABLE {DB_TABLE_PLAYLISTS} ADD COLUMN cache_checksum TEXT DEFAULT ''" ) except Exception as err: if "duplicate column" not in str(err): raise + if prev_version <= 8: + # migrate track_loudness --> loudness_measurements + async for db_row in self.database.iter_items("track_loudness"): + if db_row["integrated"] == inf or db_row["integrated"] == -inf: + continue + if db_row["provider"] in ("radiobrowser", "tunein"): + continue + await self.database.insert_or_replace( + DB_TABLE_LOUDNESS_MEASUREMENTS, + { + "item_id": db_row["item_id"], + "media_type": "track", + "provider": db_row["provider"], + "loudness": db_row["integrated"], + }, + ) + await self.database.execute("DROP TABLE IF EXISTS track_loudness") + + if prev_version <= 10: + # Recreate playlog table due to complete new layout + await self.database.execute(f"DROP TABLE IF EXISTS {DB_TABLE_PLAYLOG}") + await self.__create_database_tables() + if prev_version <= 12: # Need to drop the NOT NULL requirement on podcasts.publisher and audiobooks.publisher # However, because there is no ALTER COLUMN support in sqlite, we will need -- 2.34.1