fix: address code review findings

Remove dead code: duplicate ExternalRelease/Artist/ParsedArtist structs in
model.go, ParseArtist/mbArtist/mbArtistData in client.go, ArtistTypeFilter
and related filtering functions in api.go, SyncArtistDiscographyWithFilter
in sync.go, and CacheStats/IsArtistCacheValid in cache.go.

Fix bugs: SaveExternalRelease now stores NULL instead of empty string for
zero CachedAt; sync upserts are now transactional with stale release cleanup;
getCachedReleases returns int instead of *CacheStats; doGet uses url.Values
for proper query encoding of MBID.

Fix tests: removed duplicate TestRun_GracefulShutdown, removed dead code
(_ = dbPath) from TestNewApp, fixed assertions in httptest handler goroutine
to avoid data race, increased rate limiter timing tolerance, removed
Client.Close() calls (no-op removed), fixed sync test cache expiry to use
UPDATE instead of 0 TTL races.

Fix formatting: cancel()}() formatting in main.go, error format string in sync.go.
This commit is contained in:
2026-05-26 14:10:22 +03:00
parent 34ea84fc77
commit a5911c257c
13 changed files with 199 additions and 864 deletions

View File

@@ -7,47 +7,13 @@ import (
"naviwatcher/internal/database"
)
// CacheStats holds the result of a cache lookup for a given artist.
type CacheStats struct {
// CachedRGIDs is the list of RGIDs that are currently cached (within TTL).
CachedRGIDs []string
// CacheHitCount is the number of entries found in cache.
CacheHitCount int
}
// IsCached returns true if the given RGID is in the cached set.
func (cs *CacheStats) IsCached(rgid string) bool {
for _, id := range cs.CachedRGIDs {
if id == rgid {
return true
}
}
return false
}
// GetCachedReleases queries the external_releases table for entries
// belonging to the given artist that were cached within the specified TTL.
// It returns a CacheStats with the list of valid RGIDs already in cache.
func GetCachedReleases(db *database.DB, artistID string, ttl time.Duration) (*CacheStats, error) {
// It returns the count of cached entries and any error encountered.
func GetCachedReleases(db *database.DB, artistID string, ttl time.Duration) (int, error) {
releases, err := database.GetExternalReleasesByArtistWithCache(db, artistID, ttl)
if err != nil {
return nil, fmt.Errorf("get cached releases: %w", err)
return 0, fmt.Errorf("get cached releases: %w", err)
}
stats := &CacheStats{}
for _, r := range releases {
stats.CachedRGIDs = append(stats.CachedRGIDs, r.RGID)
stats.CacheHitCount++
}
return stats, nil
}
// IsArtistCacheValid checks whether the cache for an artist is still valid.
// Returns true if any entries exist within the TTL for this artist.
func IsArtistCacheValid(db *database.DB, artistID string, ttl time.Duration) (bool, error) {
stats, err := GetCachedReleases(db, artistID, ttl)
if err != nil {
return false, err
}
return stats.CacheHitCount > 0, nil
return len(releases), nil
}