From 981b22764f3b5da35c602351d6e52c71af4350f1 Mon Sep 17 00:00:00 2001 From: Adrian Ulrich Date: Wed, 7 Jun 2017 21:53:38 +0200 Subject: [PATCH] Nuke invalid song references If a song somehow ends up with multiple artist, album entries, the view will show the song multiple times (each possible combination). This is - confusing - and such a song should not exist. We are now deleting such entries during cleanups. --- .../android/medialibrary/MediaLibraryBackend.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java b/src/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java index 3a94563b..cba7e4b9 100644 --- a/src/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java +++ b/src/ch/blinkenlights/android/medialibrary/MediaLibraryBackend.java @@ -180,7 +180,16 @@ public class MediaLibraryBackend extends SQLiteOpenHelper { */ void cleanOrphanedEntries() { SQLiteDatabase dbh = getWritableDatabase(); + // These are not orphaned, but corrupted entries: This song exists multiple times as we have + // multiple artist or album references. Just nuke it as we can't tell which entry is correct. + // A re-scan will re-add the song (once). + dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_SONGS+" WHERE "+MediaLibrary.SongColumns._ID+" IN ("+ + "SELECT "+MediaLibrary.SongColumns._ID+" FROM "+MediaLibrary.VIEW_SONGS_ALBUMS_ARTISTS+" GROUP BY "+ + MediaLibrary.SongColumns._ID+" HAVING count("+MediaLibrary.SongColumns._ID+") > 1)"); + + // Remove all songs which are marked for deletion dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_SONGS+" WHERE "+MediaLibrary.SongColumns.MTIME+"="+PENDING_DELETION_MTIME); + // And remove any orphaned references. dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_ALBUMS+" WHERE "+MediaLibrary.AlbumColumns._ID+" NOT IN (SELECT "+MediaLibrary.SongColumns.ALBUM_ID+" FROM "+MediaLibrary.TABLE_SONGS+");"); dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_GENRES_SONGS+" WHERE "+MediaLibrary.GenreSongColumns.SONG_ID+" NOT IN (SELECT "+MediaLibrary.SongColumns._ID+" FROM "+MediaLibrary.TABLE_SONGS+");"); dbh.execSQL("DELETE FROM "+MediaLibrary.TABLE_GENRES+" WHERE "+MediaLibrary.GenreColumns._ID+" NOT IN (SELECT "+MediaLibrary.GenreSongColumns._GENRE_ID+" FROM "+MediaLibrary.TABLE_GENRES_SONGS+");");