fix: address code review findings

This commit is contained in:
2026-07-19 19:07:20 +03:00
parent a4c426f640
commit c70f46af27
10 changed files with 77 additions and 124 deletions

View File

@@ -15,7 +15,11 @@ type DB struct {
// New opens a SQLite database at dbPath and runs schema migrations.
func New(dbPath string) (*DB, error) {
conn, err := sql.Open("sqlite3", dbPath)
// The _foreign_keys=on DSN parameter enables foreign key enforcement on
// EVERY connection in the pool. A one-off "PRAGMA foreign_keys=ON" executed
// on the pooled *sql.DB only applies to the first connection and is lost on
// connections opened later by the pool, silently disabling the safety net.
conn, err := sql.Open("sqlite3", dbPath+"?_foreign_keys=on")
if err != nil {
return nil, fmt.Errorf("open database: %w", err)
}
@@ -26,12 +30,6 @@ func New(dbPath string) (*DB, error) {
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()

View File

@@ -119,28 +119,6 @@ func SetReleaseIgnored(db *DB, rgid string, ignored bool) error {
return nil
}
// DeleteExternalReleasesByArtist removes all external_release rows for a given artist_id.
func DeleteExternalReleasesByArtist(db *DB, artistID string) error {
_, err := db.Conn().Exec(
"DELETE FROM external_releases WHERE artist_id = ?",
artistID,
)
if err != nil {
return fmt.Errorf("delete external releases by artist: %w", err)
}
return nil
}
// CountExternalReleases returns the total number of external_release rows.
func CountExternalReleases(db *DB) (int, error) {
var count int
err := db.Conn().QueryRow("SELECT COUNT(*) FROM external_releases").Scan(&count)
if err != nil {
return 0, fmt.Errorf("count external releases: %w", err)
}
return count, nil
}
// 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) {