Fix the timestamp modified trigger on the db (#2266)
authorMarcel van der Veldt <m.vanderveldt@outlook.com>
Thu, 3 Jul 2025 09:36:35 +0000 (11:36 +0200)
committerGitHub <noreply@github.com>
Thu, 3 Jul 2025 09:36:35 +0000 (11:36 +0200)
* Fix the timestamp modified trigger on the db

* change query

music_assistant/controllers/music.py

index e11b2f6f0f77b6d97ea0503b15e3a65767e7ea11..593d83fbfa727d930460c524f0da546534b3d69c 100644 (file)
@@ -85,7 +85,7 @@ DEFAULT_SYNC_INTERVAL = 12 * 60  # default sync interval in minutes
 CONF_SYNC_INTERVAL = "sync_interval"
 CONF_DELETED_PROVIDERS = "deleted_providers"
 CONF_ADD_LIBRARY_ON_PLAY = "add_library_on_play"
-DB_SCHEMA_VERSION: Final[int] = 17
+DB_SCHEMA_VERSION: Final[int] = 18
 
 
 class MusicController(CoreController):
@@ -1176,7 +1176,7 @@ class MusicController(CoreController):
                 translation_key="recent_favorite_tracks",
                 icon="mdi-file-music",
                 items=await self.tracks.library_items(
-                    favorite=True, limit=10, order_by="timestamp_added"
+                    favorite=True, limit=10, order_by="timestamp_modified_desc"
                 ),
             ),
             RecommendationFolder(
@@ -1559,6 +1559,20 @@ class MusicController(CoreController):
                             },
                         )
 
+        if prev_version <= 17:
+            # migrate triggers to auto update timestamps
+            # it had an error in the previous version where it was not created
+            for db_table in (
+                "artists",
+                "albums",
+                "tracks",
+                "playlists",
+                "radios",
+                "audiobooks",
+                "podcasts",
+            ):
+                await self.database.execute(f"DROP TRIGGER IF EXISTS update_{db_table}_timestamp;")
+
         # save changes
         await self.database.commit()
 
@@ -1905,11 +1919,10 @@ class MusicController(CoreController):
             await self.database.execute(
                 f"""
                 CREATE TRIGGER IF NOT EXISTS update_{db_table}_timestamp
-                AFTER UPDATE ON {db_table} FOR EACH ROW
-                WHEN NEW.timestamp_modified <= OLD.timestamp_modified
+                AFTER UPDATE ON {db_table}
                 BEGIN
-                    UPDATE {db_table} set timestamp_modified=cast(strftime('%s','now') as int)
-                    WHERE item_id=OLD.item_id;
+                    UPDATE {db_table} SET timestamp_modified=cast(strftime('%s','now') as int)
+                    WHERE rowid = new.rowid;
                 END;
                 """
             )