From 40c0edb06f6da5bc7d887ff7f59520110cfbde9a Mon Sep 17 00:00:00 2001 From: krateng Date: Thu, 7 Apr 2022 21:21:00 +0200 Subject: [PATCH] Improved DB cleanup --- maloja/database/sqldb.py | 44 ++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index 4e3dc73..629dc7c 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -700,34 +700,34 @@ def clean_db(): with SCROBBLE_LOCK: with engine.begin() as conn: - #log(f"Database Cleanup...") + log(f"Database Cleanup...") - ### Delete tracks that have no scrobbles (delete their trackartist entries first) - a1 = conn.execute(sql.text(''' - delete from trackartists where track_id in (select id from tracks where id not in (select track_id from scrobbles)) - ''')).rowcount - a2 = conn.execute(sql.text(''' - delete from tracks where id not in (select track_id from scrobbles) - ''')).rowcount + to_delete = [ + # tracks with no scrobbles (trackartist entries first) + "from trackartists where track_id in (select id from tracks where id not in (select track_id from scrobbles))", + "from tracks where id not in (select track_id from scrobbles)", + # artists with no tracks + "from artists where id not in (select artist_id from trackartists) and id not in (select target_artist from associated_artists)", + # tracks with no artists (scrobbles first) + "from scrobbles where track_id in (select id from tracks where id not in (select track_id from trackartists))", + "from tracks where id not in (select track_id from trackartists)" + ] - if a2+a1>0: log(f"Deleted {a2} tracks without scrobbles ({a1} track artist entries)") + for d in to_delete: + selection = conn.execute(sql.text(f"select * {d}")) + for row in selection.all(): + log(f"Deleting {row}") + deletion = conn.execute(sql.text(f"delete {d}")) - ### Delete artists that have no tracks - a3 = conn.execute(sql.text(''' - delete from artists where id not in (select artist_id from trackartists) and id not in (select target_artist from associated_artists) - ''')).rowcount + log("Database Cleanup complete!") - if a3>0: log(f"Deleted {a3} artists without tracks") - ### Delete tracks that have no artists (delete their scrobbles first) - a4 = conn.execute(sql.text(''' - delete from scrobbles where track_id in (select id from tracks where id not in (select track_id from trackartists)) - ''')).rowcount - a5 = conn.execute(sql.text(''' - delete from tracks where id not in (select track_id from trackartists) - ''')).rowcount - if a5+a4>0: log(f"Deleted {a5} tracks without artists ({a4} scrobbles)") + #if a2+a1>0: log(f"Deleted {a2} tracks without scrobbles ({a1} track artist entries)") + + #if a3>0: log(f"Deleted {a3} artists without tracks") + + #if a5+a4>0: log(f"Deleted {a5} tracks without artists ({a4} scrobbles)")