- Fix duplicate Telegram notifications: SyncArtistDiscography no longer wipes notifications_sent for the whole artist on every cache-miss re-sync; only markers for releases that disappear are pruned (FK-safe via INSERT OR REPLACE + rgid NOT IN (...)). - Cache empty MusicBrainz discographies via a new artist_settings.last_synced column (migration 009) so zero-release artists honor the TTL instead of being re-fetched every cycle. - Wire the Web UI server and Telegram notifier scheduler into main.run/NewApp. - Guard startPeriodicSync against overlapping syncs with a done-channel slot. - Add server.public_url config; NewServerWithConfig derives reachable links and no longer advertises the 0.0.0.0 bind address. - Web handlers: use scanner.ScanArtist per artist, drop always-false releaseIgnored lookup and dead endsWith, thread configured threshold. - Limit :memory: DB pool to one connection so migrations and queries share the same in-memory store.
157 lines
4.4 KiB
Go
157 lines
4.4 KiB
Go
package database
|
|
|
|
import (
|
|
"database/sql"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
)
|
|
|
|
// DBer is the minimal query interface satisfied by both *sql.DB and *sql.Tx,
|
|
// so callers can run statements inside or outside a transaction.
|
|
type DBer interface {
|
|
Exec(query string, args ...interface{}) (sql.Result, error)
|
|
}
|
|
|
|
// GetArtistSettings retrieves an artist_settings row by ID.
|
|
// Returns sql.ErrNoRows if the artist is not found.
|
|
func GetArtistSettings(db *DB, id string) (*ArtistSettings, error) {
|
|
var (
|
|
s ArtistSettings
|
|
mbid sql.NullString
|
|
lastSynced sql.NullTime
|
|
)
|
|
err := db.Conn().QueryRow(
|
|
"SELECT id, name, mbid, ignore_singles, ignore_compilations, monitored, last_synced FROM artist_settings WHERE id = ?",
|
|
id,
|
|
).Scan(&s.ID, &s.Name, &mbid, &s.IgnoreSingles, &s.IgnoreCompilations, &s.Monitored, &lastSynced)
|
|
if err != nil {
|
|
if errors.Is(err, sql.ErrNoRows) {
|
|
return nil, ErrArtistNotFound
|
|
}
|
|
return nil, err
|
|
}
|
|
s.MBID = mbid.String
|
|
if lastSynced.Valid {
|
|
s.LastSynced = lastSynced.Time
|
|
}
|
|
return &s, nil
|
|
}
|
|
|
|
// TouchArtistSynced records that the artist was synced at the given time. It
|
|
// is used by the MusicBrainz pipeline to mark a successful sync (even one that
|
|
// found zero release groups) so the cache TTL is honoured.
|
|
func TouchArtistSynced(db DBer, artistID string, syncedAt time.Time) error {
|
|
_, err := db.Exec(
|
|
"UPDATE artist_settings SET last_synced = ? WHERE id = ?",
|
|
FormatCachedAt(syncedAt), artistID,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("touch artist synced: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// SaveArtistSettings inserts or replaces an artist_settings row.
|
|
func SaveArtistSettings(db *DB, settings *ArtistSettings) error {
|
|
_, err := db.Conn().Exec(
|
|
"INSERT OR REPLACE INTO artist_settings (id, name, mbid, ignore_singles, ignore_compilations, monitored) VALUES (?, ?, ?, ?, ?, ?)",
|
|
settings.ID, settings.Name, settings.MBID, settings.IgnoreSingles, settings.IgnoreCompilations, settings.Monitored,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("save artist settings: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetAllArtistSettings returns all rows from artist_settings.
|
|
func GetAllArtistSettings(db *DB) ([]ArtistSettings, error) {
|
|
rows, err := db.Conn().Query(
|
|
"SELECT id, name, mbid, ignore_singles, ignore_compilations, monitored FROM artist_settings",
|
|
)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query all artist settings: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var results []ArtistSettings
|
|
for rows.Next() {
|
|
var s ArtistSettings
|
|
if err := rows.Scan(&s.ID, &s.Name, &s.MBID, &s.IgnoreSingles, &s.IgnoreCompilations, &s.Monitored); err != nil {
|
|
return nil, fmt.Errorf("scan artist settings: %w", err)
|
|
}
|
|
results = append(results, s)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("iterate artist settings: %w", err)
|
|
}
|
|
return results, nil
|
|
}
|
|
|
|
// UpdateArtistSettings updates specific fields of an artist_settings row by ID.
|
|
// The updates map keys must match column names: "name", "ignore_singles", "ignore_compilations", "monitored".
|
|
func UpdateArtistSettings(db *DB, id string, updates map[string]interface{}) error {
|
|
if len(updates) == 0 {
|
|
return fmt.Errorf("no updates provided")
|
|
}
|
|
|
|
// Build the query using a fixed set of allowed columns to avoid dynamic SQL.
|
|
const baseQuery = "UPDATE artist_settings SET"
|
|
|
|
var args []interface{}
|
|
setClause := ""
|
|
for col, val := range updates {
|
|
switch col {
|
|
case "name":
|
|
if setClause != "" {
|
|
setClause += ", "
|
|
}
|
|
setClause += "name = ?"
|
|
args = append(args, val)
|
|
case "mbid":
|
|
if setClause != "" {
|
|
setClause += ", "
|
|
}
|
|
setClause += "mbid = ?"
|
|
args = append(args, val)
|
|
case "ignore_singles":
|
|
if setClause != "" {
|
|
setClause += ", "
|
|
}
|
|
setClause += "ignore_singles = ?"
|
|
args = append(args, val)
|
|
case "ignore_compilations":
|
|
if setClause != "" {
|
|
setClause += ", "
|
|
}
|
|
setClause += "ignore_compilations = ?"
|
|
args = append(args, val)
|
|
case "monitored":
|
|
if setClause != "" {
|
|
setClause += ", "
|
|
}
|
|
setClause += "monitored = ?"
|
|
args = append(args, val)
|
|
default:
|
|
return fmt.Errorf("unknown column: %s", col)
|
|
}
|
|
}
|
|
|
|
args = append(args, id)
|
|
query := fmt.Sprintf("%s %s WHERE id = ?", baseQuery, setClause)
|
|
result, err := db.Conn().Exec(query, args...)
|
|
if err != nil {
|
|
return fmt.Errorf("update artist settings: %w", err)
|
|
}
|
|
|
|
rowsAffected, err := result.RowsAffected()
|
|
if err != nil {
|
|
return fmt.Errorf("rows affected: %w", err)
|
|
}
|
|
if rowsAffected == 0 {
|
|
return fmt.Errorf("artist not found: %s", id)
|
|
}
|
|
|
|
return nil
|
|
}
|