feat: add mbid column to artist_settings
Additive migration 008 adds an mbid column to artist_settings, extend the ArtistSettings struct and persistence functions (SaveArtistSettings, GetArtistSettings, GetAllArtistSettings, UpdateArtistSettings) to carry the MusicBrainz ID, and add round-trip tests covering empty and set MBID values.
This commit is contained in:
@@ -1,28 +1,33 @@
|
||||
package database
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// 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
|
||||
var (
|
||||
s ArtistSettings
|
||||
mbid sql.NullString
|
||||
)
|
||||
err := db.Conn().QueryRow(
|
||||
"SELECT id, name, ignore_singles, ignore_compilations, monitored FROM artist_settings WHERE id = ?",
|
||||
"SELECT id, name, mbid, ignore_singles, ignore_compilations, monitored FROM artist_settings WHERE id = ?",
|
||||
id,
|
||||
).Scan(&s.ID, &s.Name, &s.IgnoreSingles, &s.IgnoreCompilations, &s.Monitored)
|
||||
).Scan(&s.ID, &s.Name, &mbid, &s.IgnoreSingles, &s.IgnoreCompilations, &s.Monitored)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s.MBID = mbid.String
|
||||
return &s, 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, ignore_singles, ignore_compilations, monitored) VALUES (?, ?, ?, ?, ?)",
|
||||
settings.ID, settings.Name, settings.IgnoreSingles, settings.IgnoreCompilations, settings.Monitored,
|
||||
"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)
|
||||
@@ -33,7 +38,7 @@ func SaveArtistSettings(db *DB, settings *ArtistSettings) error {
|
||||
// GetAllArtistSettings returns all rows from artist_settings.
|
||||
func GetAllArtistSettings(db *DB) ([]ArtistSettings, error) {
|
||||
rows, err := db.Conn().Query(
|
||||
"SELECT id, name, ignore_singles, ignore_compilations, monitored FROM artist_settings",
|
||||
"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)
|
||||
@@ -43,7 +48,7 @@ func GetAllArtistSettings(db *DB) ([]ArtistSettings, error) {
|
||||
var results []ArtistSettings
|
||||
for rows.Next() {
|
||||
var s ArtistSettings
|
||||
if err := rows.Scan(&s.ID, &s.Name, &s.IgnoreSingles, &s.IgnoreCompilations, &s.Monitored); err != nil {
|
||||
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)
|
||||
@@ -74,6 +79,12 @@ func UpdateArtistSettings(db *DB, id string, updates map[string]interface{}) err
|
||||
}
|
||||
setClause += "name = ?"
|
||||
args = append(args, val)
|
||||
case "mbid":
|
||||
if setClause != "" {
|
||||
setClause += ", "
|
||||
}
|
||||
setClause += "mbid = ?"
|
||||
args = append(args, val)
|
||||
case "ignore_singles":
|
||||
if setClause != "" {
|
||||
setClause += ", "
|
||||
|
||||
Reference in New Issue
Block a user