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):
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:
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
(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
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
(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
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
(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 = {
'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()
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)