|
|
|
|
@@ -9,6 +9,21 @@ import (
|
|
|
|
|
_ "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 {
|
|
|
|
|
@@ -54,10 +69,7 @@ func GetExternalRelease(db *DB, rgid string) (*ExternalRelease, error) {
|
|
|
|
|
|
|
|
|
|
// SaveExternalRelease inserts or replaces an external_release row.
|
|
|
|
|
func SaveExternalRelease(db *DB, release *ExternalRelease) error {
|
|
|
|
|
var cachedAt interface{}
|
|
|
|
|
if !release.CachedAt.IsZero() {
|
|
|
|
|
cachedAt = release.CachedAt
|
|
|
|
|
}
|
|
|
|
|
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),
|
|
|
|
|
@@ -157,10 +169,9 @@ 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) {
|
|
|
|
|
// 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.
|
|
|
|
|
// cached_at is stored in the "2006-01-02 15:04:05" UTC layout via
|
|
|
|
|
// FormatCachedAt. Compare against an explicitly formatted cutoff string in
|
|
|
|
|
// the same layout so the lexicographic comparison is a valid time ordering.
|
|
|
|
|
const layout = "2006-01-02 15:04:05"
|
|
|
|
|
cutoff := time.Now().UTC().Add(-ttl).Format(layout)
|
|
|
|
|
rows, err := db.Conn().Query(
|
|
|
|
|
|