]> kaliko git repositories - mpd-sima.git/commitdiff
Add Triggers for blocklist→albums|artists
authorkaliko <kaliko@azylum.org>
Fri, 30 Apr 2021 09:35:59 +0000 (11:35 +0200)
committerkaliko <kaliko@azylum.org>
Wed, 5 May 2021 15:31:17 +0000 (17:31 +0200)
sima/lib/db.py
tests/test_simadb.py

index 229a233cb50c385b9d02c6e5defa7869b9bda919..a2687b8188f0428542727cf2ca0b34f47b398c36 100644 (file)
@@ -142,6 +142,30 @@ class SimaDB:
              DELETE FROM tracks WHERE id = old.track;
             END;
             ''')
+        # DELETE blocklist → Artists table
+        # The "SELECT count(*) FROM blocklist" is useless,
+        # there can be only one blocklist.artist
+        connection.execute('''
+            CREATE TRIGGER IF NOT EXISTS del_blocklist_cleanup_artists
+            AFTER DELETE ON blocklist
+            WHEN ((SELECT count(*) FROM tracks WHERE artist=old.artist) = 0 AND
+                  (SELECT count(*) FROM blocklist WHERE artist=old.artist) = 0)
+            BEGIN
+             DELETE FROM artists WHERE id = old.artist;
+            END;
+            ''')
+        # DELETE Tracks → Albums table
+        # The "SELECT count(*) FROM blocklist" is useless,
+        # there can be only one blocklist.album
+        connection.execute('''
+            CREATE TRIGGER IF NOT EXISTS del_blocklist_cleanup_albums
+            AFTER DELETE ON blocklist
+            WHEN ((SELECT count(*) FROM tracks WHERE album=old.album) = 0 AND
+                  (SELECT count(*) FROM blocklist WHERE album=old.album) = 0)
+            BEGIN
+             DELETE FROM albums WHERE id = old.album;
+            END;
+            ''')
         self.close_database_connection(connection)
 
     def drop_all(self):
@@ -152,13 +176,18 @@ class SimaDB:
             connection.execute(f'DROP TABLE IF EXISTS {r[0]}')
         connection.close()
 
-    def _remove_blocklist_id(self, blid):
+    def _remove_blocklist_id(self, blid, with_connection=None):
         """Remove id"""
+        if with_connection:
+            connection = with_connection
+        else:
+            connection = self.get_database_connection()
         connection = self.get_database_connection()
         connection.execute('DELETE FROM blocklist'
                            ' WHERE blocklist.id = ?', (blid,))
         connection.commit()
-        self.close_database_connection(connection)
+        if not with_connection:
+            self.close_database_connection(connection)
 
     def _get_album(self, album, connection):
         if album.mbid:
@@ -426,7 +455,7 @@ class SimaDB:
             connection = self.get_database_connection()
         track_id = self.get_track(track, with_connection=connection, add=True)
         rows = connection.execute(
-            "SELECT * FROM blocklist WHERE track = ?", (track_id,))
+            "SELECT id FROM blocklist WHERE track = ?", (track_id,))
         if not rows.fetchone():
             if not add:
                 return None
@@ -434,13 +463,13 @@ class SimaDB:
                                (track_id,))
             connection.commit()
         rows = connection.execute(
-            "SELECT * FROM blocklist WHERE track = ?", (track_id,))
+            "SELECT id FROM blocklist WHERE track = ?", (track_id,))
         return rows.fetchone()[0]
 
     def get_bl_album(self, album, with_connection=None, add=True):
         """Add an album to blocklist
         :param sima.lib.meta.Album: Album object to add to blocklist
-        :param sqlite3.Connection with_connection: sqlite3.Connection to reuse, ele create a new one
+        :param sqlite3.Connection with_connection: sqlite3.Connection to reuse, else create a new one
         :param bool add: Default is to add a new record, set to False to fetch associated record"""
         if with_connection:
             connection = with_connection
@@ -448,7 +477,7 @@ class SimaDB:
             connection = self.get_database_connection()
         album_id = self.get_album(album, with_connection=connection, add=True)
         rows = connection.execute(
-            "SELECT * FROM blocklist WHERE album = ?", (album_id,))
+            "SELECT id FROM blocklist WHERE album = ?", (album_id,))
         if not rows.fetchone():
             if not add:
                 return None
@@ -456,13 +485,13 @@ class SimaDB:
                                (album_id,))
             connection.commit()
         rows = connection.execute(
-            "SELECT * FROM blocklist WHERE album = ?", (album_id,))
-        return rows.fetchone()
+            "SELECT id FROM blocklist WHERE album = ?", (album_id,))
+        return rows.fetchone()[0]
 
     def get_bl_artist(self, artist, with_connection=None, add=True):
         """Add an artist to blocklist
         :param sima.lib.meta.Artist: Artist object to add to blocklist
-        :param sqlite3.Connection with_connection: sqlite3.Connection to reuse, ele create a new one
+        :param sqlite3.Connection with_connection: sqlite3.Connection to reuse, else create a new one
         :param bool add: Default is to add a new record, set to False to fetch associated record"""
         if with_connection:
             connection = with_connection
@@ -470,7 +499,7 @@ class SimaDB:
             connection = self.get_database_connection()
         artist_id = self.get_artist(artist, with_connection=connection, add=True)
         rows = connection.execute(
-            "SELECT * FROM blocklist WHERE artist = ?", (artist_id,))
+            "SELECT id FROM blocklist WHERE artist = ?", (artist_id,))
         if not rows.fetchone():
             if not add:
                 return None
@@ -478,9 +507,23 @@ class SimaDB:
                                (artist_id,))
             connection.commit()
         rows = connection.execute(
-            "SELECT * FROM blocklist WHERE artist = ?", (artist_id,))
-        return rows.fetchone()
+            "SELECT id FROM blocklist WHERE artist = ?", (artist_id,))
+        return rows.fetchone()[0]
 
+    def delete_bl(self, track=None, album=None, artist=None):
+        if not (track or album or artist):
+            return
+        connection = self.get_database_connection()
+        blid = None
+        if track:
+            blid = self.get_bl_track(track, with_connection=connection)
+        if album:
+            blid = self.get_bl_album(album, with_connection=connection)
+        if artist:
+            blid = self.get_bl_artist(artist, with_connection=connection)
+        if not blid:
+            return
+        self._remove_blocklist_id(blid, with_connection=connection)
 
 def main():
     DEVOLT = {
index b71fa1baca973c13e8ac2e2e6581d1a62c0e68f1..8e0d49fd224b8a57fb5b86dccf69fd4488717e7e 100644 (file)
@@ -28,7 +28,7 @@ DEVOLT = {
     'track': '3/6'}
 
 DB_FILE = 'file::memory:?cache=shared'
-KEEP_FILE = False  # File db in file to ease debug
+KEEP_FILE = True  # File db in file to ease debug
 if KEEP_FILE:
     DB_FILE = '/dev/shm/unittest.sqlite'
 CURRENT = datetime.datetime.utcnow()
@@ -226,7 +226,20 @@ class Test_01BlockList(Main):
             if i == 3:
                 self.db.get_bl_artist(trk.Artist)
 
-    def test_blocklist_triggers(self):
+    def test_blocklist_triggers_00(self):
+        trk01 = Track(file='01', name='01', artist='artist A', album='album A')
+        blart01_id = self.db.get_bl_artist(trk01.Artist)
+        blalb01_id = self.db.get_bl_album(Album(name=trk01.album, mbid=trk01.musicbrainz_albumid))
+        conn = self.db.get_database_connection()
+        self.db._remove_blocklist_id(blart01_id, with_connection=conn)
+        self.db._remove_blocklist_id(blalb01_id, with_connection=conn)
+        albums = conn.execute('SELECT albums.name FROM albums;').fetchall()
+        artists = conn.execute('SELECT artists.name FROM artists;').fetchall()
+        conn.close()
+        self.assertNotIn((trk01.album,), albums)
+        self.assertNotIn((trk01.artist,), artists)
+
+    def test_blocklist_triggers_01(self):
         trk01 = Track(file='01', name='01', artist='artist A', album='album A')
         trk02 = Track(file='02', name='01', artist='artist A', album='album B')
         trk01_id = self.db.get_bl_track(trk01)