Skip to content

Fix artist stats not refreshing during quick scan and after missing file deletion #4269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jun 26, 2025

Conversation

deluan
Copy link
Member

@deluan deluan commented Jun 26, 2025

Problem

Artist statistics were not being refreshed correctly in two scenarios:

  1. During quick scans: Newly added albums didn't result in their artists being marked as "touched", so incremental RefreshStats operations would skip them
  2. After missing file deletion: When missing files were deleted, related artists weren't being updated, leaving stale statistics

Root Cause

The issue stemmed from the RefreshStats method relying on artist updated_at timestamps to determine which artists need refreshing, but:

  • During scanning, artists were saved without updating the updated_at field
  • During missing file deletion, artist records remained untouched even though their stats should change

Solution

Two-part fix:

1. Scanner Fix (scanner/phase_1_folders.go)

  • Added "updated_at" to the list of columns when calling artistRepo.Put()
  • Ensures artists get proper timestamps when created/updated during scanning

2. Missing File Deletion Fix (server/nativeapi/missing.go)

  • Added background RefreshStats(true) call after successful missing file deletion
  • Uses fire-and-forget goroutine to avoid blocking API response
  • Includes proper error logging for monitoring

3. RefreshStats Simplification (persistence/artist_repository.go)

  • Simplified incremental refresh query to use updated_at > last_scan_at directly
  • More straightforward logic that leverages the fixed timestamp handling

Key Benefits

  • Quick scans now work correctly: Artists touched during scanning are properly refreshed
  • Missing file operations are accurate: Artist stats are updated after file deletions
  • Non-blocking: Missing file API calls return immediately while stats refresh in background
  • Observable: Proper logging for monitoring background operations
  • Simple & robust: Minimal code changes following existing patterns

Files Changed

  • scanner/phase_1_folders.go: Include "updated_at" in artist Put columns
  • persistence/artist_repository.go: Simplified RefreshStats query logic
  • server/nativeapi/missing.go: Background RefreshStats after file deletion

This ensures artist statistics are always accurate for both scanning and missing file management operations.

deluan added 2 commits June 26, 2025 13:34
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.
…ng files

Signed-off-by: Deluan <deluan@navidrome.org>
@Copilot Copilot AI review requested due to automatic review settings June 26, 2025 18:30
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @deluan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses critical data consistency issues related to artist statistics by ensuring that artist records are properly "touched" (their updated_at timestamps updated) during library scans and after the deletion of missing files. It also refines the artist statistics refresh mechanism to more effectively utilize these timestamps, leading to more accurate and up-to-date artist information across the application.

Highlights

  • Improved Artist Data Freshness during Scans: Artists created or updated during quick scans will now have their updated_at timestamps correctly set, ensuring they are considered for subsequent RefreshStats operations.
  • Accurate Artist Stats After File Deletions: Deleting missing media files will now trigger a background refresh of artist statistics, preventing stale data for artists whose associated files have been removed.
  • Simplified Artist Statistics Refresh Logic: The RefreshStats method's incremental query has been streamlined to directly leverage the artist.updated_at timestamp, making the logic more robust and aligned with the new timestamp handling.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR ensures artist statistics are properly refreshed by updating timestamp handling during scanning, simplifying the incremental refresh logic, and triggering a non-blocking background refresh after missing file deletions.

  • Marks artists as touched during quick scans by persisting updated_at
  • Simplifies RefreshStats to use updated_at > last_scan_at
  • Fires off a background RefreshStats(true) after deleting missing files

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.

File Description
scanner/phase_1_folders.go Added "updated_at" to artistRepo.Put columns to ensure timestamps are set
persistence/artist_repository.go Simplified RefreshStats to query artists based on updated_at
server/nativeapi/missing.go Spawn a goroutine to call RefreshStats(true) in background after file deletion
Comments suppressed due to low confidence (2)

server/nativeapi/missing.go:96

  • [nitpick] Add a comment explaining that context.Background() is used intentionally to detach the refresh job from request cancellation and carry over request values.
		bgCtx := request.AddValues(context.Background(), r.Context())

server/nativeapi/missing.go:95

  • [nitpick] Consider limiting concurrent background RefreshStats goroutines or using a job queue to prevent multiple heavy refresh operations from running simultaneously.
	go func() {

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request addresses the issue of artist statistics not refreshing correctly during quick scans and after missing file deletion. The changes include updating the artist's updated_at field during scanning, triggering a background RefreshStats call after deleting missing files, and simplifying the RefreshStats query logic. The changes look good overall, but there are some critical and high severity issues that need to be addressed.

deluan added 2 commits June 26, 2025 14:44
Signed-off-by: Deluan <deluan@navidrome.org>
- 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
@deluan deluan merged commit b63630f into master Jun 26, 2025
22 checks passed
@deluan deluan deleted the fix/artist-touched-during-quick-scan branch June 26, 2025 19:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant