cleanup history entries after db update

- delete history entries without album/artist
- fix #37
This commit is contained in:
Moritz Weber 2023-01-03 21:43:13 +01:00
parent 9c615dde02
commit cd068c9579
3 changed files with 41 additions and 0 deletions

View file

@ -418,4 +418,41 @@ class MusicDataDao extends DatabaseAccessor<MoorDatabase>
batch.deleteWhere<$BlockedFilesTable, dynamic>(blockedFiles, (tbl) => tbl.path.isIn(paths));
});
}
@override
Future<void> cleanupDatabase() async {
// get album history entries
final albumHistoryEntries = await (select(historyEntries)
..where((tbl) => tbl.type.equals(PlayableType.album.toString())))
.get();
// delete history entries with missing album
for (final entry in albumHistoryEntries) {
if ((await (select(albums)..where((tbl) => tbl.id.equals(int.parse(entry.identifier)))).get())
.isEmpty) {
(delete(historyEntries)
..where((tbl) =>
tbl.type.equals(PlayableType.album.toString()) &
tbl.identifier.equals(entry.identifier)))
.go();
}
}
// get album history entries
final artistHistoryEntries = await (select(historyEntries)
..where((tbl) => tbl.type.equals(PlayableType.artist.toString())))
.get();
// delete history entries with missing album
for (final entry in artistHistoryEntries) {
if ((await (select(artists)..where((tbl) => tbl.id.equals(int.parse(entry.identifier)))).get())
.isEmpty) {
(delete(historyEntries)
..where((tbl) =>
tbl.type.equals(PlayableType.artist.toString()) &
tbl.identifier.equals(entry.identifier)))
.go();
}
}
}
}

View file

@ -39,4 +39,6 @@ abstract class MusicDataSource {
Stream<Set<String>> get blockedFilesStream;
Future<void> addBlockedFiles(List<String> paths);
Future<void> removeBlockedFiles(List<String> paths);
Future<void> cleanupDatabase();
}

View file

@ -128,6 +128,8 @@ class MusicDataRepositoryImpl implements MusicDataRepository {
await _updateAlbums(albums);
await _musicDataSource.insertSongs(songs);
await _musicDataSource.cleanupDatabase();
_log.d('updateDatabase finished');
_updateHighlightStreams();