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()