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.
This commit is contained in:
Adrian Ulrich 2017-06-07 21:53:38 +02:00
parent 57992bad25
commit 981b22764f

View File

@ -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+");");