- Start Web UI before the blocking initial sync so the dashboard is reachable during the (rate-limited, potentially multi-minute) first sync; fold the immediate sync into startPeriodicSync's overlap guard so it can never race a concurrent tick over the shared DB / MB client. - Make MarkNotificationSent idempotent: INSERT OR IGNORE for same-second PK collisions, and explicitly swallow FK violations when a release was pruned by a concurrent re-sync. Prevents a single vanished/duplicate release from aborting the digest mark-sent loop and re-sending. - Do not abort NotifyOnce's mark-sent loop on a single failure; log and continue so every release in the batch is marked. - NULL-safe reads: COALESCE(type,''), COALESCE(release_date,'') in the external_releases and unnotified readers to match the cache reader. - Update/extend tests for the new idempotency and startup contracts.
256 lines
8.4 KiB
Go
256 lines
8.4 KiB
Go
package database
|
|
|
|
import (
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
)
|
|
|
|
// utcLayout is the canonical layout for the cached_at column. go-sqlite3
|
|
// serializes a time.Time as RFC3339, which does not compare correctly against
|
|
// the space-separated cutoff used by the cache query. Storing this layout keeps
|
|
// the lexicographic comparison in GetExternalReleasesByArtistWithCache valid.
|
|
const utcLayout = "2006-01-02 15:04:05"
|
|
|
|
// FormatCachedAt renders a timestamp in the canonical UTC layout for storage.
|
|
// A zero time yields nil so the column is left NULL.
|
|
func FormatCachedAt(t time.Time) interface{} {
|
|
if t.IsZero() {
|
|
return nil
|
|
}
|
|
return t.UTC().Format(utcLayout)
|
|
}
|
|
|
|
// JoinSecondaryTypes renders a slice of secondary types as a comma-separated
|
|
// string for storage in the secondary_types TEXT column (empty when none).
|
|
func JoinSecondaryTypes(types []string) string {
|
|
return strings.Join(types, ",")
|
|
}
|
|
|
|
// splitSecondaryTypes parses the comma-separated secondary_types column back
|
|
// into a slice. A NULL/empty column yields an empty slice.
|
|
func splitSecondaryTypes(s string) []string {
|
|
if s == "" {
|
|
return nil
|
|
}
|
|
parts := strings.Split(s, ",")
|
|
out := make([]string, 0, len(parts))
|
|
for _, p := range parts {
|
|
if p != "" {
|
|
out = append(out, p)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// GetExternalRelease retrieves an external_release row by RGID.
|
|
func GetExternalRelease(db *DB, rgid string) (*ExternalRelease, error) {
|
|
var r ExternalRelease
|
|
var cachedAt sql.NullTime
|
|
var secondaryTypes sql.NullString
|
|
err := db.Conn().QueryRow(
|
|
"SELECT rgid, artist_id, title, COALESCE(type,''), COALESCE(release_date,''), is_ignored, cached_at, secondary_types FROM external_releases WHERE rgid = ?",
|
|
rgid,
|
|
).Scan(&r.RGID, &r.ArtistID, &r.Title, &r.Type, &r.ReleaseDate, &r.IsIgnored, &cachedAt, &secondaryTypes)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("get external release: %w", err)
|
|
}
|
|
if cachedAt.Valid {
|
|
r.CachedAt = cachedAt.Time
|
|
}
|
|
if secondaryTypes.Valid {
|
|
r.SecondaryTypes = splitSecondaryTypes(secondaryTypes.String)
|
|
}
|
|
return &r, nil
|
|
}
|
|
|
|
// SaveExternalRelease inserts or replaces an external_release row.
|
|
func SaveExternalRelease(db *DB, release *ExternalRelease) error {
|
|
var cachedAt interface{} = FormatCachedAt(release.CachedAt)
|
|
_, err := db.Conn().Exec(
|
|
"INSERT OR REPLACE INTO external_releases (rgid, artist_id, title, type, release_date, is_ignored, cached_at, secondary_types) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
|
release.RGID, release.ArtistID, release.Title, release.Type, release.ReleaseDate, release.IsIgnored, cachedAt, JoinSecondaryTypes(release.SecondaryTypes),
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("save external release: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetExternalReleasesByArtist returns all external_release rows for a given artist_id.
|
|
func GetExternalReleasesByArtist(db *DB, artistID string) ([]ExternalRelease, error) {
|
|
rows, err := db.Conn().Query(
|
|
"SELECT rgid, artist_id, title, COALESCE(type,''), COALESCE(release_date,''), is_ignored, cached_at, secondary_types FROM external_releases WHERE artist_id = ?",
|
|
artistID,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query external releases by artist: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var results []ExternalRelease
|
|
for rows.Next() {
|
|
var r ExternalRelease
|
|
var cachedAt sql.NullTime
|
|
var secondaryTypes sql.NullString
|
|
if err := rows.Scan(&r.RGID, &r.ArtistID, &r.Title, &r.Type, &r.ReleaseDate, &r.IsIgnored, &cachedAt, &secondaryTypes); err != nil {
|
|
return nil, fmt.Errorf("scan external release: %w", err)
|
|
}
|
|
if cachedAt.Valid {
|
|
r.CachedAt = cachedAt.Time
|
|
}
|
|
if secondaryTypes.Valid {
|
|
r.SecondaryTypes = splitSecondaryTypes(secondaryTypes.String)
|
|
}
|
|
results = append(results, r)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("iterate external releases: %w", err)
|
|
}
|
|
return results, nil
|
|
}
|
|
|
|
// GetIgnoredReleases returns all external_release rows where is_ignored = 1.
|
|
func GetIgnoredReleases(db *DB) ([]ExternalRelease, error) {
|
|
rows, err := db.Conn().Query(
|
|
"SELECT rgid, artist_id, title, COALESCE(type,''), COALESCE(release_date,''), is_ignored, cached_at, secondary_types FROM external_releases WHERE is_ignored = 1",
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query ignored releases: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var results []ExternalRelease
|
|
for rows.Next() {
|
|
var r ExternalRelease
|
|
var cachedAt sql.NullTime
|
|
var secondaryTypes sql.NullString
|
|
if err := rows.Scan(&r.RGID, &r.ArtistID, &r.Title, &r.Type, &r.ReleaseDate, &r.IsIgnored, &cachedAt, &secondaryTypes); err != nil {
|
|
return nil, fmt.Errorf("scan ignored release: %w", err)
|
|
}
|
|
if cachedAt.Valid {
|
|
r.CachedAt = cachedAt.Time
|
|
}
|
|
if secondaryTypes.Valid {
|
|
r.SecondaryTypes = splitSecondaryTypes(secondaryTypes.String)
|
|
}
|
|
results = append(results, r)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("iterate ignored releases: %w", err)
|
|
}
|
|
return results, nil
|
|
}
|
|
|
|
// SetReleaseIgnored updates the is_ignored flag for a given RGID.
|
|
func SetReleaseIgnored(db *DB, rgid string, ignored bool) error {
|
|
result, err := db.Conn().Exec(
|
|
"UPDATE external_releases SET is_ignored = ? WHERE rgid = ?",
|
|
ignored, rgid,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("set release ignored: %w", err)
|
|
}
|
|
|
|
rowsAffected, err := result.RowsAffected()
|
|
if err != nil {
|
|
return fmt.Errorf("rows affected: %w", err)
|
|
}
|
|
if rowsAffected == 0 {
|
|
return fmt.Errorf("%w: %s", ErrReleaseNotFound, rgid)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// ArtistCacheFresh reports whether the artist was synced within the TTL. A hit
|
|
// requires a fresh external_releases row (covers pre-seeded/non-empty caches)
|
|
// OR a fresh artist_settings.last_synced (covers empty discographies, which
|
|
// store no external_releases rows but are still marked as synced). Either
|
|
// signal means we should not re-fetch from MusicBrainz.
|
|
func ArtistCacheFresh(db *DB, artistID string, ttl time.Duration) (bool, error) {
|
|
if ttl <= 0 {
|
|
return false, nil
|
|
}
|
|
cutoff := time.Now().UTC().Add(-ttl).Format(utcLayout)
|
|
var dummy int
|
|
err := db.Conn().QueryRow(
|
|
"SELECT 1 FROM external_releases WHERE artist_id = ? AND cached_at >= ? LIMIT 1",
|
|
artistID, cutoff,
|
|
).Scan(&dummy)
|
|
if err == nil {
|
|
return true, nil
|
|
}
|
|
if err != sql.ErrNoRows {
|
|
return false, fmt.Errorf("check artist cache freshness (releases): %w", err)
|
|
}
|
|
|
|
// Fall back to the per-artist last_synced marker (set even on empty syncs).
|
|
err = db.Conn().QueryRow(
|
|
"SELECT 1 FROM artist_settings WHERE id = ? AND last_synced >= ? LIMIT 1",
|
|
artistID, cutoff,
|
|
).Scan(&dummy)
|
|
if err == nil {
|
|
return true, nil
|
|
}
|
|
if err == sql.ErrNoRows {
|
|
return false, nil
|
|
}
|
|
return false, fmt.Errorf("check artist cache freshness (settings): %w", err)
|
|
}
|
|
|
|
// GetExternalReleasesByArtistWithCache returns cached external_release rows for a given artist_id
|
|
// that are within the specified TTL.
|
|
func GetExternalReleasesByArtistWithCache(db *DB, artistID string, ttl time.Duration) ([]ExternalRelease, error) {
|
|
// A zero or negative TTL means "no cache" — always expire. Returning early
|
|
// here avoids the boundary pitfall where cutoff == now would treat rows
|
|
// cached in the current second as fresh.
|
|
if ttl <= 0 {
|
|
return nil, nil
|
|
}
|
|
// cached_at is stored in utcLayout via FormatCachedAt. Compare against an
|
|
// explicitly formatted cutoff string in the same layout so the
|
|
// lexicographic comparison is a valid time ordering.
|
|
cutoff := time.Now().UTC().Add(-ttl).Format(utcLayout)
|
|
rows, err := db.Conn().Query(
|
|
"SELECT rgid, artist_id, title, type, release_date, is_ignored, cached_at, secondary_types FROM external_releases WHERE artist_id = ? AND cached_at >= ?",
|
|
artistID, cutoff,
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query cached external releases: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var results []ExternalRelease
|
|
for rows.Next() {
|
|
var r ExternalRelease
|
|
var releaseType sql.NullString
|
|
var releaseDate sql.NullString
|
|
var cachedAt sql.NullTime
|
|
var secondaryTypes sql.NullString
|
|
if err := rows.Scan(&r.RGID, &r.ArtistID, &r.Title, &releaseType, &releaseDate, &r.IsIgnored, &cachedAt, &secondaryTypes); err != nil {
|
|
return nil, fmt.Errorf("scan cached external release: %w", err)
|
|
}
|
|
if releaseType.Valid {
|
|
r.Type = releaseType.String
|
|
}
|
|
if releaseDate.Valid {
|
|
r.ReleaseDate = releaseDate.String
|
|
}
|
|
if cachedAt.Valid {
|
|
r.CachedAt = cachedAt.Time
|
|
}
|
|
if secondaryTypes.Valid {
|
|
r.SecondaryTypes = splitSecondaryTypes(secondaryTypes.String)
|
|
}
|
|
results = append(results, r)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("iterate cached external releases: %w", err)
|
|
}
|
|
return results, nil
|
|
}
|