fix: address code review findings
This commit is contained in:
@@ -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