fix(scanner) artist stats not refreshing during quick scan and after missing file deletion (#4269)

* Fix artist not being marked as touched during quick scans

When a new album is added during quick scans, artists were not being
marked as 'touched' due to media files having older modification times
than the scan completion time.

Changes:
- Add 'updated_at' to artist Put() columns in scanner to ensure
  timestamp is set when artists are processed
- Simplify RefreshStats query to check artist.updated_at directly
  instead of complex media file joins
- Artists from new albums now properly get refreshed in later phases

This fixes the issue where newly added albums would have incomplete
artist information after quick scans.

* fix(missing): refresh artist stats in background after deleting missing files

Signed-off-by: Deluan <deluan@navidrome.org>

* fix(request): add InternalAuth to user context

Signed-off-by: Deluan <deluan@navidrome.org>

* Add comprehensive test for artist stats update during quick scans

- Add test that verifies artist statistics are correctly updated when new files are added during incremental scans
- Test ensures both overall stats (AlbumCount, SongCount) and role-specific stats are properly refreshed
- Validates fix for artist stats not being refreshed during quick scans when new albums are added
- Uses real artist repository instead of mock to verify actual stats calculation behavior

---------

Signed-off-by: Deluan <deluan@navidrome.org>
This commit is contained in:
Deluan Quintão
2025-06-26 15:50:56 -04:00
committed by GitHub
parent 28bbd00dcc
commit b63630fa6e
5 changed files with 70 additions and 15 deletions

View File

@@ -303,12 +303,11 @@ func (r *artistRepository) RefreshStats(allArtists bool) (int64, error) {
}
log.Debug(r.ctx, "RefreshStats: Refreshing all artists.", "count", len(allTouchedArtistIDs))
} else {
// Only refresh artists with updated media files
// Only refresh artists with updated timestamps
touchedArtistsQuerySQL := `
SELECT DISTINCT mfa.artist_id
FROM media_file_artists mfa
JOIN media_file mf ON mfa.media_file_id = mf.id
WHERE mf.updated_at > (SELECT last_scan_at FROM library ORDER BY last_scan_at ASC LIMIT 1)
SELECT DISTINCT id
FROM artist
WHERE updated_at > (SELECT last_scan_at FROM library ORDER BY last_scan_at ASC LIMIT 1)
`
if err := r.db.NewQuery(touchedArtistsQuerySQL).Column(&allTouchedArtistIDs); err != nil {
return 0, fmt.Errorf("fetching touched artist IDs: %w", err)