feat: add CRUD operations for notifications_sent table
Implement MarkNotificationSent, IsNotificationSent, and GetUnnotifiedReleases with LEFT JOIN query. Includes 10 table-driven tests covering success, error, idempotent, and edge cases. Also go fmt cleanup on existing files. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -107,12 +107,12 @@ Build the foundation layer of NaviWatcher: a greenfield Go project with zero exi
|
|||||||
- [x] run tests — must pass before task 6
|
- [x] run tests — must pass before task 6
|
||||||
|
|
||||||
### Task 6: Database layer — CRUD operations for notifications_sent
|
### Task 6: Database layer — CRUD operations for notifications_sent
|
||||||
- [ ] implement `MarkNotificationSent(db *DB, rgid string) error`
|
- [x] implement `MarkNotificationSent(db *DB, rgid string) error`
|
||||||
- [ ] implement `IsNotificationSent(db *DB, rgid string) (bool, error)`
|
- [x] implement `IsNotificationSent(db *DB, rgid string) (bool, error)`
|
||||||
- [ ] implement `GetUnnotifiedReleases(db *DB) ([]ExternalRelease, error)` — joins external_releases with notifications_sent to find unsent
|
- [x] implement `GetUnnotifiedReleases(db *DB) ([]ExternalRelease, error)` — joins external_releases with notifications_sent to find unsent
|
||||||
- [ ] write tests for MarkNotificationSent and IsNotificationSent
|
- [x] write tests for MarkNotificationSent and IsNotificationSent
|
||||||
- [ ] write tests for GetUnnotifiedReleases (with and without existing notifications)
|
- [x] write tests for GetUnnotifiedReleases (with and without existing notifications)
|
||||||
- [ ] run tests — must pass before task 7
|
- [x] run tests — must pass before task 7
|
||||||
|
|
||||||
### Task 7: Docker setup
|
### Task 7: Docker setup
|
||||||
- [ ] create `Dockerfile` with multi-stage build: build stage (golang:1.21-alpine) + runtime stage (alpine:latest)
|
- [ ] create `Dockerfile` with multi-stage build: build stage (golang:1.21-alpine) + runtime stage (alpine:latest)
|
||||||
|
|||||||
69
internal/database/notifications.go
Normal file
69
internal/database/notifications.go
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
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 IGNORE 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 = ?", rgid,
|
||||||
|
).Scan(&sentAt)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return sentAt, nil
|
||||||
|
}
|
||||||
265
internal/database/notifications_test.go
Normal file
265
internal/database/notifications_test.go
Normal file
@@ -0,0 +1,265 @@
|
|||||||
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestMarkNotificationSent_New verifies inserting a new notification record.
|
||||||
|
func TestMarkNotificationSent_New(t *testing.T) {
|
||||||
|
db, err := New(":memory:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("New() error: %v", err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
if err := MarkNotificationSent(db, "rgid-1"); err != nil {
|
||||||
|
t.Fatalf("MarkNotificationSent() error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sentAt, err := GetNotificationSentAt(db, "rgid-1")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetNotificationSentAt() error: %v", err)
|
||||||
|
}
|
||||||
|
if sentAt == "" {
|
||||||
|
t.Error("expected sent_at to be non-empty")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMarkNotificationSent_Idempotent verifies that marking the same RGID twice does not fail.
|
||||||
|
func TestMarkNotificationSent_Idempotent(t *testing.T) {
|
||||||
|
db, err := New(":memory:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("New() error: %v", err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
if err := MarkNotificationSent(db, "rgid-1"); err != nil {
|
||||||
|
t.Fatalf("first MarkNotificationSent() error: %v", err)
|
||||||
|
}
|
||||||
|
if err := MarkNotificationSent(db, "rgid-1"); err != nil {
|
||||||
|
t.Fatalf("second MarkNotificationSent() error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should still have exactly one row.
|
||||||
|
var count int
|
||||||
|
err = db.Conn().QueryRow("SELECT COUNT(*) FROM notifications_sent WHERE rgid = ?", "rgid-1").Scan(&count)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("count query error: %v", err)
|
||||||
|
}
|
||||||
|
if count != 1 {
|
||||||
|
t.Errorf("expected 1 notification row, got %d", count)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestIsNotificationSent_True verifies true for a sent notification.
|
||||||
|
func TestIsNotificationSent_True(t *testing.T) {
|
||||||
|
db, err := New(":memory:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("New() error: %v", err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
if err := MarkNotificationSent(db, "rgid-1"); err != nil {
|
||||||
|
t.Fatalf("MarkNotificationSent() error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
sent, err := IsNotificationSent(db, "rgid-1")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("IsNotificationSent() error: %v", err)
|
||||||
|
}
|
||||||
|
if !sent {
|
||||||
|
t.Error("expected IsNotificationSent to return true")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestIsNotificationSent_False verifies false for an unsent RGID.
|
||||||
|
func TestIsNotificationSent_False(t *testing.T) {
|
||||||
|
db, err := New(":memory:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("New() error: %v", err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
sent, err := IsNotificationSent(db, "nonexistent")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("IsNotificationSent() error: %v", err)
|
||||||
|
}
|
||||||
|
if sent {
|
||||||
|
t.Error("expected IsNotificationSent to return false for nonexistent RGID")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestGetNotificationSentAt_NotFound verifies sql.ErrNoRows for unsent RGID.
|
||||||
|
func TestGetNotificationSentAt_NotFound(t *testing.T) {
|
||||||
|
db, err := New(":memory:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("New() error: %v", err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
_, err = GetNotificationSentAt(db, "nonexistent")
|
||||||
|
if err != sql.ErrNoRows {
|
||||||
|
t.Errorf("expected sql.ErrNoRows, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestGetUnnotifiedReleases_AllUnnotified verifies all releases returned when no notifications sent.
|
||||||
|
func TestGetUnnotifiedReleases_AllUnnotified(t *testing.T) {
|
||||||
|
db, err := New(":memory:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("New() error: %v", err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
releases := []ExternalRelease{
|
||||||
|
{RGID: "rg1", ArtistID: "artist-1", Title: "Album 1", Type: "album", ReleaseDate: "2024-01-01"},
|
||||||
|
{RGID: "rg2", ArtistID: "artist-1", Title: "Album 2", Type: "album", ReleaseDate: "2024-06-01"},
|
||||||
|
{RGID: "rg3", ArtistID: "artist-2", Title: "Single 1", Type: "single", ReleaseDate: "2024-03-01"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range releases {
|
||||||
|
if err := SaveExternalRelease(db, &r); err != nil {
|
||||||
|
t.Fatalf("SaveExternalRelease(%s) error: %v", r.RGID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
results, err := GetUnnotifiedReleases(db)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetUnnotifiedReleases() error: %v", err)
|
||||||
|
}
|
||||||
|
if len(results) != 3 {
|
||||||
|
t.Fatalf("expected 3 unnotified releases, got %d", len(results))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestGetUnnotifiedReleases_SomeNotified verifies only unnotified releases are returned.
|
||||||
|
func TestGetUnnotifiedReleases_SomeNotified(t *testing.T) {
|
||||||
|
db, err := New(":memory:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("New() error: %v", err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
releases := []ExternalRelease{
|
||||||
|
{RGID: "rg1", ArtistID: "artist-1", Title: "Album 1", Type: "album", ReleaseDate: "2024-01-01"},
|
||||||
|
{RGID: "rg2", ArtistID: "artist-1", Title: "Album 2", Type: "album", ReleaseDate: "2024-06-01"},
|
||||||
|
{RGID: "rg3", ArtistID: "artist-2", Title: "Single 1", Type: "single", ReleaseDate: "2024-03-01"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range releases {
|
||||||
|
if err := SaveExternalRelease(db, &r); err != nil {
|
||||||
|
t.Fatalf("SaveExternalRelease(%s) error: %v", r.RGID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark rg1 and rg3 as notified.
|
||||||
|
if err := MarkNotificationSent(db, "rg1"); err != nil {
|
||||||
|
t.Fatalf("MarkNotificationSent(rg1) error: %v", err)
|
||||||
|
}
|
||||||
|
if err := MarkNotificationSent(db, "rg3"); err != nil {
|
||||||
|
t.Fatalf("MarkNotificationSent(rg3) error: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
results, err := GetUnnotifiedReleases(db)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetUnnotifiedReleases() error: %v", err)
|
||||||
|
}
|
||||||
|
if len(results) != 1 {
|
||||||
|
t.Fatalf("expected 1 unnotified release, got %d", len(results))
|
||||||
|
}
|
||||||
|
if results[0].RGID != "rg2" {
|
||||||
|
t.Errorf("expected unnotified release rg2, got %s", results[0].RGID)
|
||||||
|
}
|
||||||
|
if results[0].Title != "Album 2" {
|
||||||
|
t.Errorf("expected Title 'Album 2', got %q", results[0].Title)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestGetUnnotifiedReleases_AllNotified verifies empty result when all releases are notified.
|
||||||
|
func TestGetUnnotifiedReleases_AllNotified(t *testing.T) {
|
||||||
|
db, err := New(":memory:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("New() error: %v", err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
releases := []ExternalRelease{
|
||||||
|
{RGID: "rg1", ArtistID: "artist-1", Title: "Album 1"},
|
||||||
|
{RGID: "rg2", ArtistID: "artist-1", Title: "Album 2"},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, r := range releases {
|
||||||
|
if err := SaveExternalRelease(db, &r); err != nil {
|
||||||
|
t.Fatalf("SaveExternalRelease(%s) error: %v", r.RGID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark all as notified.
|
||||||
|
for _, r := range releases {
|
||||||
|
if err := MarkNotificationSent(db, r.RGID); err != nil {
|
||||||
|
t.Fatalf("MarkNotificationSent(%s) error: %v", r.RGID, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
results, err := GetUnnotifiedReleases(db)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetUnnotifiedReleases() error: %v", err)
|
||||||
|
}
|
||||||
|
if len(results) != 0 {
|
||||||
|
t.Errorf("expected 0 unnotified releases, got %d", len(results))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestGetUnnotifiedReleases_NoReleases verifies empty result when no releases exist.
|
||||||
|
func TestGetUnnotifiedReleases_NoReleases(t *testing.T) {
|
||||||
|
db, err := New(":memory:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("New() error: %v", err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
results, err := GetUnnotifiedReleases(db)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("GetUnnotifiedReleases() error: %v", err)
|
||||||
|
}
|
||||||
|
if len(results) != 0 {
|
||||||
|
t.Errorf("expected 0 unnotified releases, got %d", len(results))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestMarkNotificationSent_MultipleReleases verifies marking multiple different RGIDs.
|
||||||
|
func TestMarkNotificationSent_MultipleReleases(t *testing.T) {
|
||||||
|
db, err := New(":memory:")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("New() error: %v", err)
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
rgids := []string{"rg1", "rg2", "rg3"}
|
||||||
|
for _, rgid := range rgids {
|
||||||
|
if err := MarkNotificationSent(db, rgid); err != nil {
|
||||||
|
t.Fatalf("MarkNotificationSent(%s) error: %v", rgid, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, rgid := range rgids {
|
||||||
|
sent, err := IsNotificationSent(db, rgid)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("IsNotificationSent(%s) error: %v", rgid, err)
|
||||||
|
}
|
||||||
|
if !sent {
|
||||||
|
t.Errorf("expected IsNotificationSent(%s) to return true", rgid)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Verify count.
|
||||||
|
var count int
|
||||||
|
err = db.Conn().QueryRow("SELECT COUNT(*) FROM notifications_sent").Scan(&count)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("count query error: %v", err)
|
||||||
|
}
|
||||||
|
if count != 3 {
|
||||||
|
t.Errorf("expected 3 notification rows, got %d", count)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user