fix: address code review findings
- 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>
This commit is contained in:
10
README.md
10
README.md
@@ -28,7 +28,7 @@ NaviWatcher is an autonomous service daemon that monitors your Navidrome music c
|
|||||||
|
|
||||||
### Technology Stack
|
### Technology Stack
|
||||||
|
|
||||||
- **Language:** Go 1.21+
|
- **Language:** Go 1.25+
|
||||||
- **Database:** SQLite 3
|
- **Database:** SQLite 3
|
||||||
- **HTTP Server:** Go standard library (`net/http` + `html/template`)
|
- **HTTP Server:** Go standard library (`net/http` + `html/template`)
|
||||||
- **Key dependencies:**
|
- **Key dependencies:**
|
||||||
@@ -69,12 +69,12 @@ server:
|
|||||||
host: "0.0.0.0"
|
host: "0.0.0.0"
|
||||||
port: 8080
|
port: 8080
|
||||||
username: "admin"
|
username: "admin"
|
||||||
password: "password123"
|
password: "CHANGE_ME"
|
||||||
|
|
||||||
navidrome:
|
navidrome:
|
||||||
url: "http://localhost:4533"
|
url: "http://localhost:4533"
|
||||||
user: "watcher_service"
|
user: "watcher_service"
|
||||||
password: "user_password"
|
password: "CHANGE_ME"
|
||||||
|
|
||||||
musicbrainz:
|
musicbrainz:
|
||||||
user_agent: "NaviWatcher/1.0 ( mail@example.com )"
|
user_agent: "NaviWatcher/1.0 ( mail@example.com )"
|
||||||
@@ -176,12 +176,12 @@ server:
|
|||||||
host: "0.0.0.0"
|
host: "0.0.0.0"
|
||||||
port: 8080
|
port: 8080
|
||||||
username: "admin"
|
username: "admin"
|
||||||
password: "password123"
|
password: "CHANGE_ME"
|
||||||
|
|
||||||
navidrome:
|
navidrome:
|
||||||
url: "http://localhost:4533"
|
url: "http://localhost:4533"
|
||||||
user: "watcher_service"
|
user: "watcher_service"
|
||||||
password: "user_password"
|
password: "CHANGE_ME"
|
||||||
|
|
||||||
musicbrainz:
|
musicbrainz:
|
||||||
user_agent: "NaviWatcher/1.0 ( mail@example.com )"
|
user_agent: "NaviWatcher/1.0 ( mail@example.com )"
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
"syscall"
|
"syscall"
|
||||||
"time"
|
|
||||||
|
|
||||||
"naviwatcher/internal/config"
|
"naviwatcher/internal/config"
|
||||||
)
|
)
|
||||||
@@ -51,7 +50,5 @@ func run(ctx context.Context, cfg *config.Config) error {
|
|||||||
// Business logic will be added in future tasks.
|
// Business logic will be added in future tasks.
|
||||||
<-ctx.Done()
|
<-ctx.Done()
|
||||||
|
|
||||||
// Graceful shutdown delay to let goroutines finish.
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,12 +9,6 @@ import (
|
|||||||
"naviwatcher/internal/config"
|
"naviwatcher/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestDefaultConfigPath(t *testing.T) {
|
|
||||||
if defaultConfigPath != "config.yaml" {
|
|
||||||
t.Errorf("expected default config path 'config.yaml', got %q", defaultConfigPath)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestRun_GracefulShutdown(t *testing.T) {
|
func TestRun_GracefulShutdown(t *testing.T) {
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
cancel()
|
cancel()
|
||||||
|
|||||||
@@ -3,12 +3,12 @@ server:
|
|||||||
port: 8080
|
port: 8080
|
||||||
# Basic Auth for Web UI access
|
# Basic Auth for Web UI access
|
||||||
username: "admin"
|
username: "admin"
|
||||||
password: "password123"
|
password: "CHANGE_ME"
|
||||||
|
|
||||||
navidrome:
|
navidrome:
|
||||||
url: "http://localhost:4533"
|
url: "http://localhost:4533"
|
||||||
user: "watcher_service"
|
user: "watcher_service"
|
||||||
password: "user_password"
|
password: "CHANGE_ME"
|
||||||
|
|
||||||
musicbrainz:
|
musicbrainz:
|
||||||
user_agent: "NaviWatcher/1.0 ( your-email@example.com )"
|
user_agent: "NaviWatcher/1.0 ( your-email@example.com )"
|
||||||
|
|||||||
139
coverage.out
Normal file
139
coverage.out
Normal file
@@ -0,0 +1,139 @@
|
|||||||
|
mode: set
|
||||||
|
naviwatcher/internal/config/config.go:58.47,60.16 2 1
|
||||||
|
naviwatcher/internal/config/config.go:60.16,62.3 1 1
|
||||||
|
naviwatcher/internal/config/config.go:64.2,65.51 2 1
|
||||||
|
naviwatcher/internal/config/config.go:65.51,67.3 1 1
|
||||||
|
naviwatcher/internal/config/config.go:69.2,71.39 2 1
|
||||||
|
naviwatcher/internal/config/config.go:71.39,73.3 1 1
|
||||||
|
naviwatcher/internal/config/config.go:75.2,75.18 1 1
|
||||||
|
naviwatcher/internal/config/config.go:79.33,80.27 1 1
|
||||||
|
naviwatcher/internal/config/config.go:80.27,82.3 1 1
|
||||||
|
naviwatcher/internal/config/config.go:83.2,83.26 1 1
|
||||||
|
naviwatcher/internal/config/config.go:83.26,85.3 1 1
|
||||||
|
naviwatcher/internal/config/config.go:86.2,86.37 1 1
|
||||||
|
naviwatcher/internal/config/config.go:86.37,88.3 1 1
|
||||||
|
naviwatcher/internal/config/config.go:92.34,93.29 1 1
|
||||||
|
naviwatcher/internal/config/config.go:93.29,95.3 1 1
|
||||||
|
naviwatcher/internal/config/config.go:96.2,96.30 1 1
|
||||||
|
naviwatcher/internal/config/config.go:96.30,98.3 1 1
|
||||||
|
naviwatcher/internal/config/config.go:99.2,99.34 1 1
|
||||||
|
naviwatcher/internal/config/config.go:99.34,101.3 1 1
|
||||||
|
naviwatcher/internal/config/config.go:102.2,102.52 1 1
|
||||||
|
naviwatcher/internal/config/config.go:102.52,104.3 1 1
|
||||||
|
naviwatcher/internal/config/config.go:105.2,105.74 1 1
|
||||||
|
naviwatcher/internal/config/config.go:105.74,107.3 1 1
|
||||||
|
naviwatcher/internal/config/config.go:108.2,108.37 1 1
|
||||||
|
naviwatcher/internal/config/config.go:108.37,110.3 1 1
|
||||||
|
naviwatcher/internal/config/config.go:111.2,111.12 1 1
|
||||||
|
naviwatcher/cmd/naviwatcher/main.go:17.13,24.16 5 0
|
||||||
|
naviwatcher/cmd/naviwatcher/main.go:24.16,26.3 1 0
|
||||||
|
naviwatcher/cmd/naviwatcher/main.go:28.2,36.12 6 0
|
||||||
|
naviwatcher/cmd/naviwatcher/main.go:36.12,40.3 3 0
|
||||||
|
naviwatcher/cmd/naviwatcher/main.go:42.2,42.38 1 0
|
||||||
|
naviwatcher/cmd/naviwatcher/main.go:42.38,44.3 1 0
|
||||||
|
naviwatcher/cmd/naviwatcher/main.go:46.2,46.37 1 0
|
||||||
|
naviwatcher/cmd/naviwatcher/main.go:49.57,57.2 3 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:9.68,15.16 3 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:15.16,17.3 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:18.2,18.16 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:22.65,27.16 2 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:27.16,29.3 1 0
|
||||||
|
naviwatcher/internal/database/artist_settings.go:30.2,30.12 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:34.61,38.16 2 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:38.16,40.3 1 0
|
||||||
|
naviwatcher/internal/database/artist_settings.go:41.2,44.18 3 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:44.18,46.106 2 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:46.106,48.4 1 0
|
||||||
|
naviwatcher/internal/database/artist_settings.go:49.3,49.31 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:51.2,51.35 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:51.35,53.3 1 0
|
||||||
|
naviwatcher/internal/database/artist_settings.go:54.2,54.21 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:59.84,70.32 4 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:70.32,71.20 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:71.20,73.4 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:74.3,74.22 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:74.22,76.4 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:77.3,78.27 2 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:81.2,81.20 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:81.20,83.3 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:85.2,88.16 4 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:88.16,90.3 1 0
|
||||||
|
naviwatcher/internal/database/artist_settings.go:92.2,93.16 2 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:93.16,95.3 1 0
|
||||||
|
naviwatcher/internal/database/artist_settings.go:96.2,96.23 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:96.23,98.3 1 1
|
||||||
|
naviwatcher/internal/database/artist_settings.go:100.2,100.12 1 1
|
||||||
|
naviwatcher/internal/database/database.go:17.38,19.16 2 1
|
||||||
|
naviwatcher/internal/database/database.go:19.16,21.3 1 0
|
||||||
|
naviwatcher/internal/database/database.go:24.2,24.64 1 1
|
||||||
|
naviwatcher/internal/database/database.go:24.64,27.3 2 0
|
||||||
|
naviwatcher/internal/database/database.go:29.2,30.37 2 1
|
||||||
|
naviwatcher/internal/database/database.go:30.37,33.3 2 0
|
||||||
|
naviwatcher/internal/database/database.go:35.2,35.16 1 1
|
||||||
|
naviwatcher/internal/database/database.go:39.29,41.2 1 1
|
||||||
|
naviwatcher/internal/database/database.go:44.30,46.2 1 1
|
||||||
|
naviwatcher/internal/database/database.go:49.31,55.19 1 1
|
||||||
|
naviwatcher/internal/database/database.go:55.19,57.3 1 0
|
||||||
|
naviwatcher/internal/database/database.go:59.2,94.31 2 1
|
||||||
|
naviwatcher/internal/database/database.go:94.31,96.17 2 1
|
||||||
|
naviwatcher/internal/database/database.go:96.17,98.4 1 0
|
||||||
|
naviwatcher/internal/database/database.go:99.3,99.14 1 1
|
||||||
|
naviwatcher/internal/database/database.go:99.14,100.12 1 1
|
||||||
|
naviwatcher/internal/database/database.go:103.3,103.48 1 1
|
||||||
|
naviwatcher/internal/database/database.go:103.48,105.4 1 0
|
||||||
|
naviwatcher/internal/database/database.go:107.3,107.57 1 1
|
||||||
|
naviwatcher/internal/database/database.go:107.57,109.4 1 0
|
||||||
|
naviwatcher/internal/database/database.go:112.2,112.12 1 1
|
||||||
|
naviwatcher/internal/database/database.go:116.61,119.16 3 1
|
||||||
|
naviwatcher/internal/database/database.go:119.16,121.3 1 0
|
||||||
|
naviwatcher/internal/database/database.go:122.2,122.23 1 1
|
||||||
|
naviwatcher/internal/database/database.go:126.55,129.2 2 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:9.72,15.16 3 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:15.16,17.3 1 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:18.2,18.16 1 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:22.66,27.16 2 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:27.16,29.3 1 0
|
||||||
|
naviwatcher/internal/database/external_releases.go:30.2,30.12 1 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:34.86,39.16 2 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:39.16,41.3 1 0
|
||||||
|
naviwatcher/internal/database/external_releases.go:42.2,45.18 3 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:45.18,47.106 2 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:47.106,49.4 1 0
|
||||||
|
naviwatcher/internal/database/external_releases.go:50.3,50.31 1 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:52.2,52.35 1 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:52.35,54.3 1 0
|
||||||
|
naviwatcher/internal/database/external_releases.go:55.2,55.21 1 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:59.60,63.16 2 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:63.16,65.3 1 0
|
||||||
|
naviwatcher/internal/database/external_releases.go:66.2,69.18 3 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:69.18,71.106 2 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:71.106,73.4 1 0
|
||||||
|
naviwatcher/internal/database/external_releases.go:74.3,74.31 1 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:76.2,76.35 1 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:76.35,78.3 1 0
|
||||||
|
naviwatcher/internal/database/external_releases.go:79.2,79.21 1 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:83.65,88.16 2 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:88.16,90.3 1 0
|
||||||
|
naviwatcher/internal/database/external_releases.go:92.2,93.16 2 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:93.16,95.3 1 0
|
||||||
|
naviwatcher/internal/database/external_releases.go:96.2,96.23 1 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:96.23,98.3 1 1
|
||||||
|
naviwatcher/internal/database/external_releases.go:100.2,100.12 1 1
|
||||||
|
naviwatcher/internal/database/notifications.go:8.54,13.16 2 1
|
||||||
|
naviwatcher/internal/database/notifications.go:13.16,15.3 1 0
|
||||||
|
naviwatcher/internal/database/notifications.go:16.2,16.12 1 1
|
||||||
|
naviwatcher/internal/database/notifications.go:20.60,25.16 3 1
|
||||||
|
naviwatcher/internal/database/notifications.go:25.16,27.3 1 0
|
||||||
|
naviwatcher/internal/database/notifications.go:28.2,28.23 1 1
|
||||||
|
naviwatcher/internal/database/notifications.go:32.63,39.16 2 1
|
||||||
|
naviwatcher/internal/database/notifications.go:39.16,41.3 1 0
|
||||||
|
naviwatcher/internal/database/notifications.go:42.2,45.18 3 1
|
||||||
|
naviwatcher/internal/database/notifications.go:45.18,47.106 2 1
|
||||||
|
naviwatcher/internal/database/notifications.go:47.106,49.4 1 0
|
||||||
|
naviwatcher/internal/database/notifications.go:50.3,50.31 1 1
|
||||||
|
naviwatcher/internal/database/notifications.go:52.2,52.35 1 1
|
||||||
|
naviwatcher/internal/database/notifications.go:52.35,54.3 1 0
|
||||||
|
naviwatcher/internal/database/notifications.go:55.2,55.21 1 1
|
||||||
|
naviwatcher/internal/database/notifications.go:60.65,65.16 3 1
|
||||||
|
naviwatcher/internal/database/notifications.go:65.16,67.3 1 1
|
||||||
|
naviwatcher/internal/database/notifications.go:68.2,68.20 1 1
|
||||||
@@ -30,7 +30,7 @@ musicbrainz:
|
|||||||
telegram:
|
telegram:
|
||||||
enabled: true
|
enabled: true
|
||||||
token: "abc123"
|
token: "abc123"
|
||||||
chat: "chat456"
|
chat_id: "chat456"
|
||||||
cron_schedule: "0 10 * * *"
|
cron_schedule: "0 10 * * *"
|
||||||
|
|
||||||
scanner:
|
scanner:
|
||||||
@@ -59,6 +59,9 @@ scanner:
|
|||||||
if cfg.Navidrome.URL != "http://navidrome:4533" {
|
if cfg.Navidrome.URL != "http://navidrome:4533" {
|
||||||
t.Errorf("expected navidrome url http://navidrome:4533, got %s", cfg.Navidrome.URL)
|
t.Errorf("expected navidrome url http://navidrome:4533, got %s", cfg.Navidrome.URL)
|
||||||
}
|
}
|
||||||
|
if cfg.Telegram.ChatID != "chat456" {
|
||||||
|
t.Errorf("expected telegram chat_id chat456, got %s", cfg.Telegram.ChatID)
|
||||||
|
}
|
||||||
if cfg.Navidrome.User != "service" {
|
if cfg.Navidrome.User != "service" {
|
||||||
t.Errorf("expected navidrome user service, got %s", cfg.Navidrome.User)
|
t.Errorf("expected navidrome user service, got %s", cfg.Navidrome.User)
|
||||||
}
|
}
|
||||||
@@ -137,7 +140,6 @@ func TestLoadConfig_InvalidPort(t *testing.T) {
|
|||||||
name string
|
name string
|
||||||
port int
|
port int
|
||||||
}{
|
}{
|
||||||
{"port zero with explicit 0 and no default override", 0},
|
|
||||||
{"port too high", 70000},
|
{"port too high", 70000},
|
||||||
{"port negative", -1},
|
{"port negative", -1},
|
||||||
}
|
}
|
||||||
@@ -147,32 +149,12 @@ func TestLoadConfig_InvalidPort(t *testing.T) {
|
|||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
path := filepath.Join(dir, "config.yaml")
|
path := filepath.Join(dir, "config.yaml")
|
||||||
|
|
||||||
yaml := `
|
yaml := buildConfigWithPort(tt.port)
|
||||||
navidrome:
|
|
||||||
url: "http://localhost:4533"
|
|
||||||
user: "u"
|
|
||||||
password: "p"
|
|
||||||
|
|
||||||
musicbrainz:
|
|
||||||
user_agent: "NaviWatcher/1.0 ( test@example.com )"
|
|
||||||
|
|
||||||
server:
|
|
||||||
port: ` + string(rune('0'+tt.port%10)) + `
|
|
||||||
`
|
|
||||||
// Use Sprintf for proper port formatting
|
|
||||||
yaml = buildConfigWithPort(path, tt.port)
|
|
||||||
if err := os.WriteFile(path, []byte(yaml), 0644); err != nil {
|
if err := os.WriteFile(path, []byte(yaml), 0644); err != nil {
|
||||||
t.Fatalf("failed to write config: %v", err)
|
t.Fatalf("failed to write config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
_, err := LoadConfig(path)
|
_, err := LoadConfig(path)
|
||||||
if tt.port == 0 {
|
|
||||||
// port 0 gets defaulted to 8080, which is valid
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("port 0 should get default, got error: %v", err)
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("expected error for port %d, got nil", tt.port)
|
t.Fatalf("expected error for port %d, got nil", tt.port)
|
||||||
}
|
}
|
||||||
@@ -180,7 +162,7 @@ server:
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildConfigWithPort(path string, port int) string {
|
func buildConfigWithPort(port int) string {
|
||||||
return "navidrome:\n url: \"http://localhost:4533\"\n user: \"u\"\n password: \"p\"\n\nmusicbrainz:\n user_agent: \"NaviWatcher/1.0 ( test@example.com )\"\n\nserver:\n port: " + fmt.Sprintf("%d", port) + "\n"
|
return "navidrome:\n url: \"http://localhost:4533\"\n user: \"u\"\n password: \"p\"\n\nmusicbrainz:\n user_agent: \"NaviWatcher/1.0 ( test@example.com )\"\n\nserver:\n port: " + fmt.Sprintf("%d", port) + "\n"
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -349,7 +331,7 @@ func TestValidate_BoundaryPort(t *testing.T) {
|
|||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
path := filepath.Join(dir, "config.yaml")
|
path := filepath.Join(dir, "config.yaml")
|
||||||
|
|
||||||
yaml := buildConfigWithPort(path, tt.port)
|
yaml := buildConfigWithPort(tt.port)
|
||||||
if err := os.WriteFile(path, []byte(yaml), 0644); err != nil {
|
if err := os.WriteFile(path, []byte(yaml), 0644); err != nil {
|
||||||
t.Fatalf("failed to write config: %v", err)
|
t.Fatalf("failed to write config: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,6 +26,18 @@ func New(dbPath string) (*DB, error) {
|
|||||||
return nil, fmt.Errorf("set WAL mode: %w", err)
|
return nil, fmt.Errorf("set WAL mode: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Enable foreign key enforcement.
|
||||||
|
if _, err := conn.Exec("PRAGMA foreign_keys=ON"); err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return nil, fmt.Errorf("enable foreign keys: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set busy timeout to handle concurrent write contention.
|
||||||
|
if _, err := conn.Exec("PRAGMA busy_timeout=5000"); err != nil {
|
||||||
|
conn.Close()
|
||||||
|
return nil, fmt.Errorf("set busy timeout: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
db := &DB{conn: conn}
|
db := &DB{conn: conn}
|
||||||
if err := db.migrate(); err != nil {
|
if err := db.migrate(); err != nil {
|
||||||
conn.Close()
|
conn.Close()
|
||||||
@@ -74,7 +86,7 @@ func (db *DB) migrate() error {
|
|||||||
name: "002_create_external_releases",
|
name: "002_create_external_releases",
|
||||||
sql: `CREATE TABLE IF NOT EXISTS external_releases (
|
sql: `CREATE TABLE IF NOT EXISTS external_releases (
|
||||||
rgid TEXT PRIMARY KEY,
|
rgid TEXT PRIMARY KEY,
|
||||||
artist_id TEXT NOT NULL,
|
artist_id TEXT NOT NULL REFERENCES artist_settings(id),
|
||||||
title TEXT NOT NULL,
|
title TEXT NOT NULL,
|
||||||
type TEXT,
|
type TEXT,
|
||||||
release_date TEXT,
|
release_date TEXT,
|
||||||
@@ -84,9 +96,8 @@ func (db *DB) migrate() error {
|
|||||||
{
|
{
|
||||||
name: "003_create_notifications_sent",
|
name: "003_create_notifications_sent",
|
||||||
sql: `CREATE TABLE IF NOT EXISTS notifications_sent (
|
sql: `CREATE TABLE IF NOT EXISTS notifications_sent (
|
||||||
rgid TEXT NOT NULL,
|
rgid TEXT PRIMARY KEY REFERENCES external_releases(rgid),
|
||||||
sent_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
sent_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
||||||
PRIMARY KEY (rgid, sent_at)
|
|
||||||
);`,
|
);`,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -119,6 +119,11 @@ func TestExternalReleasesSchema(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
// Insert parent artist first (FK requirement).
|
||||||
|
_, err = db.Conn().Exec("INSERT INTO artist_settings (id, name) VALUES (?, ?)", "artist-1", "Test Artist")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("insert artist: %v", err)
|
||||||
|
}
|
||||||
_, err = db.Conn().Exec(
|
_, err = db.Conn().Exec(
|
||||||
"INSERT INTO external_releases (rgid, artist_id, title, type, release_date, is_ignored) VALUES (?, ?, ?, ?, ?, ?)",
|
"INSERT INTO external_releases (rgid, artist_id, title, type, release_date, is_ignored) VALUES (?, ?, ?, ?, ?, ?)",
|
||||||
"rgid-1", "artist-1", "Test Album", "album", "2024-01-01", false,
|
"rgid-1", "artist-1", "Test Album", "album", "2024-01-01", false,
|
||||||
@@ -150,6 +155,15 @@ func TestNotificationsSentSchema(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
// Insert parent artist and release first (FK requirements).
|
||||||
|
_, err = db.Conn().Exec("INSERT INTO artist_settings (id, name) VALUES (?, ?)", "artist-1", "Test Artist")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("insert artist: %v", err)
|
||||||
|
}
|
||||||
|
_, err = db.Conn().Exec("INSERT INTO external_releases (rgid, artist_id, title) VALUES (?, ?, ?)", "rgid-1", "artist-1", "Test Album")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("insert release: %v", err)
|
||||||
|
}
|
||||||
_, err = db.Conn().Exec(
|
_, err = db.Conn().Exec(
|
||||||
"INSERT INTO notifications_sent (rgid) VALUES (?)",
|
"INSERT INTO notifications_sent (rgid) VALUES (?)",
|
||||||
"rgid-1",
|
"rgid-1",
|
||||||
@@ -192,6 +206,6 @@ func TestMigrationTracking(t *testing.T) {
|
|||||||
|
|
||||||
// We have 3 recorded migrations: artist_settings, external_releases, notifications_sent.
|
// We have 3 recorded migrations: artist_settings, external_releases, notifications_sent.
|
||||||
if count != 3 {
|
if count != 3 {
|
||||||
t.Errorf("expected 4 applied migrations, got %d", count)
|
t.Errorf("expected 3 applied migrations, got %d", count)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,6 +5,15 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// insertTestArtist inserts a minimal artist_settings row for use in tests that need FK satisfaction.
|
||||||
|
func insertTestArtist(db *DB, id string) error {
|
||||||
|
_, err := db.Conn().Exec(
|
||||||
|
"INSERT OR IGNORE INTO artist_settings (id, name) VALUES (?, ?)",
|
||||||
|
id, "Test Artist "+id,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// TestGetExternalRelease_Found verifies retrieving an existing release.
|
// TestGetExternalRelease_Found verifies retrieving an existing release.
|
||||||
func TestGetExternalRelease_Found(t *testing.T) {
|
func TestGetExternalRelease_Found(t *testing.T) {
|
||||||
db, err := New(":memory:")
|
db, err := New(":memory:")
|
||||||
@@ -13,7 +22,9 @@ func TestGetExternalRelease_Found(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
// Insert a row directly.
|
if err := insertTestArtist(db, "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist: %v", err)
|
||||||
|
}
|
||||||
_, err = db.Conn().Exec(
|
_, err = db.Conn().Exec(
|
||||||
"INSERT INTO external_releases (rgid, artist_id, title, type, release_date, is_ignored) VALUES (?, ?, ?, ?, ?, ?)",
|
"INSERT INTO external_releases (rgid, artist_id, title, type, release_date, is_ignored) VALUES (?, ?, ?, ?, ?, ?)",
|
||||||
"rgid-1", "artist-1", "Test Album", "album", "2024-01-01", false,
|
"rgid-1", "artist-1", "Test Album", "album", "2024-01-01", false,
|
||||||
@@ -78,6 +89,9 @@ func TestSaveExternalRelease_Insert(t *testing.T) {
|
|||||||
IsIgnored: false,
|
IsIgnored: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := insertTestArtist(db, "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist: %v", err)
|
||||||
|
}
|
||||||
if err := SaveExternalRelease(db, r); err != nil {
|
if err := SaveExternalRelease(db, r); err != nil {
|
||||||
t.Fatalf("SaveExternalRelease() error: %v", err)
|
t.Fatalf("SaveExternalRelease() error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -102,6 +116,10 @@ func TestSaveExternalRelease_Update(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
if err := insertTestArtist(db, "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Insert initial row.
|
// Insert initial row.
|
||||||
r1 := &ExternalRelease{
|
r1 := &ExternalRelease{
|
||||||
RGID: "rgid-1",
|
RGID: "rgid-1",
|
||||||
@@ -177,6 +195,12 @@ func TestGetExternalReleasesByArtist_Populated(t *testing.T) {
|
|||||||
{RGID: "rg3", ArtistID: "artist-b", Title: "Album B1", Type: "single", ReleaseDate: "2024-03-01"},
|
{RGID: "rg3", ArtistID: "artist-b", Title: "Album B1", Type: "single", ReleaseDate: "2024-03-01"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := insertTestArtist(db, "artist-a"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist(artist-a): %v", err)
|
||||||
|
}
|
||||||
|
if err := insertTestArtist(db, "artist-b"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist(artist-b): %v", err)
|
||||||
|
}
|
||||||
for _, r := range releases {
|
for _, r := range releases {
|
||||||
if err := SaveExternalRelease(db, &r); err != nil {
|
if err := SaveExternalRelease(db, &r); err != nil {
|
||||||
t.Fatalf("SaveExternalRelease(%s) error: %v", r.RGID, err)
|
t.Fatalf("SaveExternalRelease(%s) error: %v", r.RGID, err)
|
||||||
@@ -243,6 +267,12 @@ func TestGetIgnoredReleases_Populated(t *testing.T) {
|
|||||||
{RGID: "rg3", ArtistID: "a2", Title: "Another Ignored", IsIgnored: true},
|
{RGID: "rg3", ArtistID: "a2", Title: "Another Ignored", IsIgnored: true},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := insertTestArtist(db, "a1"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist(a1): %v", err)
|
||||||
|
}
|
||||||
|
if err := insertTestArtist(db, "a2"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist(a2): %v", err)
|
||||||
|
}
|
||||||
for _, r := range releases {
|
for _, r := range releases {
|
||||||
if err := SaveExternalRelease(db, &r); err != nil {
|
if err := SaveExternalRelease(db, &r); err != nil {
|
||||||
t.Fatalf("SaveExternalRelease(%s) error: %v", r.RGID, err)
|
t.Fatalf("SaveExternalRelease(%s) error: %v", r.RGID, err)
|
||||||
@@ -279,6 +309,9 @@ func TestSetReleaseIgnored_SetTrue(t *testing.T) {
|
|||||||
Title: "Test Album",
|
Title: "Test Album",
|
||||||
IsIgnored: false,
|
IsIgnored: false,
|
||||||
}
|
}
|
||||||
|
if err := insertTestArtist(db, "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist: %v", err)
|
||||||
|
}
|
||||||
if err := SaveExternalRelease(db, r); err != nil {
|
if err := SaveExternalRelease(db, r); err != nil {
|
||||||
t.Fatalf("SaveExternalRelease() error: %v", err)
|
t.Fatalf("SaveExternalRelease() error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -310,6 +343,9 @@ func TestSetReleaseIgnored_SetFalse(t *testing.T) {
|
|||||||
Title: "Test Album",
|
Title: "Test Album",
|
||||||
IsIgnored: true,
|
IsIgnored: true,
|
||||||
}
|
}
|
||||||
|
if err := insertTestArtist(db, "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist: %v", err)
|
||||||
|
}
|
||||||
if err := SaveExternalRelease(db, r); err != nil {
|
if err := SaveExternalRelease(db, r); err != nil {
|
||||||
t.Fatalf("SaveExternalRelease() error: %v", err)
|
t.Fatalf("SaveExternalRelease() error: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
// MarkNotificationSent records that a notification has been sent for the given RGID.
|
// MarkNotificationSent records that a notification has been sent for the given RGID.
|
||||||
func MarkNotificationSent(db *DB, rgid string) error {
|
func MarkNotificationSent(db *DB, rgid string) error {
|
||||||
_, err := db.Conn().Exec(
|
_, err := db.Conn().Exec(
|
||||||
"INSERT OR IGNORE INTO notifications_sent (rgid) VALUES (?)",
|
"INSERT OR REPLACE INTO notifications_sent (rgid) VALUES (?)",
|
||||||
rgid,
|
rgid,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -60,7 +60,7 @@ func GetUnnotifiedReleases(db *DB) ([]ExternalRelease, error) {
|
|||||||
func GetNotificationSentAt(db *DB, rgid string) (string, error) {
|
func GetNotificationSentAt(db *DB, rgid string) (string, error) {
|
||||||
var sentAt string
|
var sentAt string
|
||||||
err := db.Conn().QueryRow(
|
err := db.Conn().QueryRow(
|
||||||
"SELECT sent_at FROM notifications_sent WHERE rgid = ?", rgid,
|
"SELECT sent_at FROM notifications_sent WHERE rgid = ? ORDER BY sent_at DESC LIMIT 1", rgid,
|
||||||
).Scan(&sentAt)
|
).Scan(&sentAt)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|||||||
@@ -5,6 +5,15 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// insertTestRelease inserts a minimal external_releases row for use in tests that need FK satisfaction.
|
||||||
|
func insertTestRelease(db *DB, rgid, artistID string) error {
|
||||||
|
_, err := db.Conn().Exec(
|
||||||
|
"INSERT OR IGNORE INTO external_releases (rgid, artist_id, title) VALUES (?, ?, ?)",
|
||||||
|
rgid, artistID, "Test Release "+rgid,
|
||||||
|
)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
// TestMarkNotificationSent_New verifies inserting a new notification record.
|
// TestMarkNotificationSent_New verifies inserting a new notification record.
|
||||||
func TestMarkNotificationSent_New(t *testing.T) {
|
func TestMarkNotificationSent_New(t *testing.T) {
|
||||||
db, err := New(":memory:")
|
db, err := New(":memory:")
|
||||||
@@ -13,6 +22,12 @@ func TestMarkNotificationSent_New(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
if err := insertTestArtist(db, "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist: %v", err)
|
||||||
|
}
|
||||||
|
if err := insertTestRelease(db, "rgid-1", "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestRelease: %v", err)
|
||||||
|
}
|
||||||
if err := MarkNotificationSent(db, "rgid-1"); err != nil {
|
if err := MarkNotificationSent(db, "rgid-1"); err != nil {
|
||||||
t.Fatalf("MarkNotificationSent() error: %v", err)
|
t.Fatalf("MarkNotificationSent() error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -34,6 +49,12 @@ func TestMarkNotificationSent_Idempotent(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
if err := insertTestArtist(db, "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist: %v", err)
|
||||||
|
}
|
||||||
|
if err := insertTestRelease(db, "rgid-1", "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestRelease: %v", err)
|
||||||
|
}
|
||||||
if err := MarkNotificationSent(db, "rgid-1"); err != nil {
|
if err := MarkNotificationSent(db, "rgid-1"); err != nil {
|
||||||
t.Fatalf("first MarkNotificationSent() error: %v", err)
|
t.Fatalf("first MarkNotificationSent() error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -60,6 +81,12 @@ func TestIsNotificationSent_True(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
if err := insertTestArtist(db, "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist: %v", err)
|
||||||
|
}
|
||||||
|
if err := insertTestRelease(db, "rgid-1", "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestRelease: %v", err)
|
||||||
|
}
|
||||||
if err := MarkNotificationSent(db, "rgid-1"); err != nil {
|
if err := MarkNotificationSent(db, "rgid-1"); err != nil {
|
||||||
t.Fatalf("MarkNotificationSent() error: %v", err)
|
t.Fatalf("MarkNotificationSent() error: %v", err)
|
||||||
}
|
}
|
||||||
@@ -118,6 +145,12 @@ func TestGetUnnotifiedReleases_AllUnnotified(t *testing.T) {
|
|||||||
{RGID: "rg3", ArtistID: "artist-2", Title: "Single 1", Type: "single", ReleaseDate: "2024-03-01"},
|
{RGID: "rg3", ArtistID: "artist-2", Title: "Single 1", Type: "single", ReleaseDate: "2024-03-01"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := insertTestArtist(db, "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist(artist-1): %v", err)
|
||||||
|
}
|
||||||
|
if err := insertTestArtist(db, "artist-2"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist(artist-2): %v", err)
|
||||||
|
}
|
||||||
for _, r := range releases {
|
for _, r := range releases {
|
||||||
if err := SaveExternalRelease(db, &r); err != nil {
|
if err := SaveExternalRelease(db, &r); err != nil {
|
||||||
t.Fatalf("SaveExternalRelease(%s) error: %v", r.RGID, err)
|
t.Fatalf("SaveExternalRelease(%s) error: %v", r.RGID, err)
|
||||||
@@ -147,6 +180,12 @@ func TestGetUnnotifiedReleases_SomeNotified(t *testing.T) {
|
|||||||
{RGID: "rg3", ArtistID: "artist-2", Title: "Single 1", Type: "single", ReleaseDate: "2024-03-01"},
|
{RGID: "rg3", ArtistID: "artist-2", Title: "Single 1", Type: "single", ReleaseDate: "2024-03-01"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := insertTestArtist(db, "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist(artist-1): %v", err)
|
||||||
|
}
|
||||||
|
if err := insertTestArtist(db, "artist-2"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist(artist-2): %v", err)
|
||||||
|
}
|
||||||
for _, r := range releases {
|
for _, r := range releases {
|
||||||
if err := SaveExternalRelease(db, &r); err != nil {
|
if err := SaveExternalRelease(db, &r); err != nil {
|
||||||
t.Fatalf("SaveExternalRelease(%s) error: %v", r.RGID, err)
|
t.Fatalf("SaveExternalRelease(%s) error: %v", r.RGID, err)
|
||||||
@@ -189,6 +228,9 @@ func TestGetUnnotifiedReleases_AllNotified(t *testing.T) {
|
|||||||
{RGID: "rg2", ArtistID: "artist-1", Title: "Album 2"},
|
{RGID: "rg2", ArtistID: "artist-1", Title: "Album 2"},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := insertTestArtist(db, "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist(artist-1): %v", err)
|
||||||
|
}
|
||||||
for _, r := range releases {
|
for _, r := range releases {
|
||||||
if err := SaveExternalRelease(db, &r); err != nil {
|
if err := SaveExternalRelease(db, &r); err != nil {
|
||||||
t.Fatalf("SaveExternalRelease(%s) error: %v", r.RGID, err)
|
t.Fatalf("SaveExternalRelease(%s) error: %v", r.RGID, err)
|
||||||
@@ -236,6 +278,15 @@ func TestMarkNotificationSent_MultipleReleases(t *testing.T) {
|
|||||||
}
|
}
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
|
||||||
|
if err := insertTestArtist(db, "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestArtist: %v", err)
|
||||||
|
}
|
||||||
|
for _, rgid := range []string{"rg1", "rg2", "rg3"} {
|
||||||
|
if err := insertTestRelease(db, rgid, "artist-1"); err != nil {
|
||||||
|
t.Fatalf("insertTestRelease(%s): %v", rgid, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rgids := []string{"rg1", "rg2", "rg3"}
|
rgids := []string{"rg1", "rg2", "rg3"}
|
||||||
for _, rgid := range rgids {
|
for _, rgid := range rgids {
|
||||||
if err := MarkNotificationSent(db, rgid); err != nil {
|
if err := MarkNotificationSent(db, rgid); err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user