diff --git a/maloja/apis/native_v1.py b/maloja/apis/native_v1.py index d969115..e7d2ff4 100644 --- a/maloja/apis/native_v1.py +++ b/maloja/apis/native_v1.py @@ -757,6 +757,16 @@ def merge_artists(target_id,source_ids): "status":"success" } +@api.post("merge_albums") +@authenticated_function(api=True) +@catch_exceptions +def merge_artists(target_id,source_ids): + """Internal Use Only""" + result = database.merge_albums(target_id,source_ids) + return { + "status":"success" + } + @api.post("reparse_scrobble") @authenticated_function(api=True) @catch_exceptions diff --git a/maloja/database/__init__.py b/maloja/database/__init__.py index 1d8dccd..628c576 100644 --- a/maloja/database/__init__.py +++ b/maloja/database/__init__.py @@ -221,6 +221,17 @@ def merge_tracks(target_id,source_ids): return result +@waitfordb +def merge_albums(target_id,source_ids): + sources = [sqldb.get_album(id) for id in source_ids] + target = sqldb.get_album(target_id) + log(f"Merging {sources} into {target}") + result = sqldb.merge_albums(target_id,source_ids) + dbcache.invalidate_entity_cache() + dbcache.invalidate_caches() + + return result + diff --git a/maloja/database/sqldb.py b/maloja/database/sqldb.py index 3bc35b5..df897e1 100644 --- a/maloja/database/sqldb.py +++ b/maloja/database/sqldb.py @@ -647,6 +647,19 @@ def merge_artists(target_id,source_ids,dbconn=None): return True +@connection_provider +def merge_albums(target_id,source_ids,dbconn=None): + + op = DB['tracks'].update().where( + DB['tracks'].c.album_id.in_(source_ids) + ).values( + album_id=target_id + ) + result = dbconn.execute(op) + clean_db(dbconn=dbconn) + + return True + ### Functions that get rows according to parameters @@ -1257,8 +1270,14 @@ def clean_db(dbconn=None): "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)", # albums with no tracks (albumartist entries first) - "from albumartists where album_id in (select id from albums where id not in (select album_id from tracks))", - "from albums where id not in (select album_id from tracks)" + "from albumartists where album_id in (select id from albums where id not in (select album_id from tracks where album_id is not null))", + "from albums where id not in (select album_id from tracks where album_id is not null)", + # albumartist entries that are missing a reference + "from albumartists where album_id not in (select album_id from tracks where album_id is not null)", + "from albumartists where artist_id not in (select id from artists)", + # trackartist entries that mare missing a reference + "from trackartists where track_id not in (select id from tracks)", + "from trackartists where artist_id not in (select id from artists)" ] for d in to_delete: