- Fix notifications_sent PK: changed from (rgid, sent_at) to rgid-only PK to prevent duplicate RGID rows across seconds. Use INSERT OR REPLACE instead of INSERT OR IGNORE for true idempotency. - Add foreign key constraints to DDL (artist_id references artist_settings, rgid references external_releases) per specification. - Enable PRAGMA foreign_keys=ON and PRAGMA busy_timeout=5000 for concurrent access safety. - Fix GetNotificationSentAt query: add ORDER BY sent_at DESC LIMIT 1 for deterministic results. - Fix config test: change YAML key from 'chat' to 'chat_id' to match struct tag, add ChatID assertion. - Fix migration tracking test: correct error message from "expected 4" to "expected 3". - Remove dead code in TestLoadConfig_InvalidPort: eliminate unused YAML template and remove port 0 case (valid, not invalid). - Remove unused path parameter from buildConfigWithPort helper. - Remove pointless 100ms sleep in run() and unused time import. - Remove tautological TestDefaultConfigPath test. - Update README.md: Go version 1.21+ to 1.25+, placeholder passwords to CHANGE_ME. - Update config.yaml.example: placeholder passwords to CHANGE_ME. - Update all database tests to insert parent rows first for FK satisfaction. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
70 lines
2.0 KiB
Go
70 lines
2.0 KiB
Go
package database
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
// MarkNotificationSent records that a notification has been sent for the given RGID.
|
|
func MarkNotificationSent(db *DB, rgid string) error {
|
|
_, err := db.Conn().Exec(
|
|
"INSERT OR REPLACE INTO notifications_sent (rgid) VALUES (?)",
|
|
rgid,
|
|
)
|
|
if err != nil {
|
|
return fmt.Errorf("mark notification sent: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// IsNotificationSent checks whether a notification has already been sent for the given RGID.
|
|
func IsNotificationSent(db *DB, rgid string) (bool, error) {
|
|
var count int
|
|
err := db.Conn().QueryRow(
|
|
"SELECT COUNT(*) FROM notifications_sent WHERE rgid = ?", rgid,
|
|
).Scan(&count)
|
|
if err != nil {
|
|
return false, fmt.Errorf("check notification sent: %w", err)
|
|
}
|
|
return count > 0, nil
|
|
}
|
|
|
|
// GetUnnotifiedReleases returns all external_release rows that have no entry in notifications_sent.
|
|
func GetUnnotifiedReleases(db *DB) ([]ExternalRelease, error) {
|
|
rows, err := db.Conn().Query(`
|
|
SELECT e.rgid, e.artist_id, e.title, e.type, e.release_date, e.is_ignored
|
|
FROM external_releases e
|
|
LEFT JOIN notifications_sent n ON e.rgid = n.rgid
|
|
WHERE n.rgid IS NULL
|
|
`)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("query unnotified releases: %w", err)
|
|
}
|
|
defer rows.Close()
|
|
|
|
var results []ExternalRelease
|
|
for rows.Next() {
|
|
var r ExternalRelease
|
|
if err := rows.Scan(&r.RGID, &r.ArtistID, &r.Title, &r.Type, &r.ReleaseDate, &r.IsIgnored); err != nil {
|
|
return nil, fmt.Errorf("scan unnotified release: %w", err)
|
|
}
|
|
results = append(results, r)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, fmt.Errorf("iterate unnotified releases: %w", err)
|
|
}
|
|
return results, nil
|
|
}
|
|
|
|
// GetNotificationSentAt returns the sent_at time for a given RGID.
|
|
// Returns sql.ErrNoRows if no notification has been sent.
|
|
func GetNotificationSentAt(db *DB, rgid string) (string, error) {
|
|
var sentAt string
|
|
err := db.Conn().QueryRow(
|
|
"SELECT sent_at FROM notifications_sent WHERE rgid = ? ORDER BY sent_at DESC LIMIT 1", rgid,
|
|
).Scan(&sentAt)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return sentAt, nil
|
|
}
|