fix: address code review findings
This commit is contained in:
@@ -117,6 +117,10 @@ func (db *DB) migrate() error {
|
||||
name: "005_add_cached_at_to_external_releases",
|
||||
sql: `ALTER TABLE external_releases ADD COLUMN cached_at DATETIME;`,
|
||||
},
|
||||
{
|
||||
name: "006_add_secondary_types_to_external_releases",
|
||||
sql: `ALTER TABLE external_releases ADD COLUMN secondary_types TEXT;`,
|
||||
},
|
||||
}
|
||||
|
||||
for _, m := range migrations {
|
||||
@@ -179,13 +183,14 @@ type LocalAlbum struct {
|
||||
|
||||
// ExternalRelease represents a row in the external_releases table.
|
||||
type ExternalRelease struct {
|
||||
RGID string `json:"rgid"`
|
||||
ArtistID string `json:"artist_id"`
|
||||
Title string `json:"title"`
|
||||
Type string `json:"type"`
|
||||
ReleaseDate string `json:"release_date"`
|
||||
IsIgnored bool `json:"is_ignored"`
|
||||
CachedAt time.Time `json:"cached_at"`
|
||||
RGID string `json:"rgid"`
|
||||
ArtistID string `json:"artist_id"`
|
||||
Title string `json:"title"`
|
||||
Type string `json:"type"`
|
||||
ReleaseDate string `json:"release_date"`
|
||||
IsIgnored bool `json:"is_ignored"`
|
||||
CachedAt time.Time `json:"cached_at"`
|
||||
SecondaryTypes []string `json:"secondary_types"`
|
||||
}
|
||||
|
||||
// NotificationSent represents a row in the notifications_sent table.
|
||||
|
||||
@@ -205,8 +205,9 @@ func TestMigrationTracking(t *testing.T) {
|
||||
t.Fatalf("query migrations count: %v", err)
|
||||
}
|
||||
|
||||
// We have 5 recorded migrations: artist_settings, external_releases, local_albums, notifications_sent, cached_at column.
|
||||
if count != 5 {
|
||||
t.Errorf("expected 5 applied migrations, got %d", count)
|
||||
// We have 6 recorded migrations: artist_settings, external_releases,
|
||||
// local_albums, notifications_sent, cached_at column, secondary_types column.
|
||||
if count != 6 {
|
||||
t.Errorf("expected 6 applied migrations, got %d", count)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,25 +3,52 @@ package database
|
||||
import (
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
)
|
||||
|
||||
// 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, type, release_date, is_ignored, cached_at FROM external_releases WHERE rgid = ?",
|
||||
"SELECT rgid, artist_id, title, type, 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)
|
||||
).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
|
||||
}
|
||||
|
||||
@@ -32,8 +59,8 @@ func SaveExternalRelease(db *DB, release *ExternalRelease) error {
|
||||
cachedAt = release.CachedAt
|
||||
}
|
||||
_, err := db.Conn().Exec(
|
||||
"INSERT OR REPLACE INTO external_releases (rgid, artist_id, title, type, release_date, is_ignored, cached_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
release.RGID, release.ArtistID, release.Title, release.Type, release.ReleaseDate, release.IsIgnored, cachedAt,
|
||||
"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)
|
||||
@@ -44,7 +71,7 @@ func SaveExternalRelease(db *DB, release *ExternalRelease) error {
|
||||
// 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, type, release_date, is_ignored, cached_at FROM external_releases WHERE artist_id = ?",
|
||||
"SELECT rgid, artist_id, title, type, release_date, is_ignored, cached_at, secondary_types FROM external_releases WHERE artist_id = ?",
|
||||
artistID,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -56,12 +83,16 @@ func GetExternalReleasesByArtist(db *DB, artistID string) ([]ExternalRelease, er
|
||||
for rows.Next() {
|
||||
var r ExternalRelease
|
||||
var cachedAt sql.NullTime
|
||||
if err := rows.Scan(&r.RGID, &r.ArtistID, &r.Title, &r.Type, &r.ReleaseDate, &r.IsIgnored, &cachedAt); err != nil {
|
||||
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 {
|
||||
@@ -73,7 +104,7 @@ func GetExternalReleasesByArtist(db *DB, artistID string) ([]ExternalRelease, er
|
||||
// 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, type, release_date, is_ignored, cached_at FROM external_releases WHERE is_ignored = 1",
|
||||
"SELECT rgid, artist_id, title, type, 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)
|
||||
@@ -84,12 +115,16 @@ func GetIgnoredReleases(db *DB) ([]ExternalRelease, error) {
|
||||
for rows.Next() {
|
||||
var r ExternalRelease
|
||||
var cachedAt sql.NullTime
|
||||
if err := rows.Scan(&r.RGID, &r.ArtistID, &r.Title, &r.Type, &r.ReleaseDate, &r.IsIgnored, &cachedAt); err != nil {
|
||||
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 {
|
||||
@@ -122,9 +157,14 @@ func SetReleaseIgnored(db *DB, rgid string, ignored bool) error {
|
||||
// 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) {
|
||||
cutoff := time.Now().UTC().Add(-ttl)
|
||||
// cached_at is a TEXT DATETIME column serialized by the driver in the
|
||||
// "2006-01-02 15:04:05" UTC layout. Compare against an explicitly
|
||||
// formatted cutoff string in the same layout so the lexicographic
|
||||
// comparison does not depend on the driver's time serialization behavior.
|
||||
const layout = "2006-01-02 15:04:05"
|
||||
cutoff := time.Now().UTC().Add(-ttl).Format(layout)
|
||||
rows, err := db.Conn().Query(
|
||||
"SELECT rgid, artist_id, title, type, release_date, is_ignored, cached_at FROM external_releases WHERE artist_id = ? AND cached_at >= ?",
|
||||
"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 {
|
||||
@@ -138,7 +178,8 @@ func GetExternalReleasesByArtistWithCache(db *DB, artistID string, ttl time.Dura
|
||||
var releaseType sql.NullString
|
||||
var releaseDate sql.NullString
|
||||
var cachedAt sql.NullTime
|
||||
if err := rows.Scan(&r.RGID, &r.ArtistID, &r.Title, &releaseType, &releaseDate, &r.IsIgnored, &cachedAt); err != nil {
|
||||
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 {
|
||||
@@ -150,6 +191,9 @@ func GetExternalReleasesByArtistWithCache(db *DB, artistID string, ttl time.Dura
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user