cache: create parent directory before opening SQLite database

Signed-off-by: NotAShelf <raf@notashelf.dev>
Change-Id: I935eeccd78986cf40aebafff6d28da746a6a6964
This commit is contained in:
raf 2026-03-06 22:47:45 +03:00
commit 64e18cbab5
Signed by: NotAShelf
GPG key ID: 29D95B64378DB4BF

View file

@ -3,6 +3,8 @@ package cache
import ( import (
"database/sql" "database/sql"
"fmt" "fmt"
"os"
"path/filepath"
"strings" "strings"
"time" "time"
@ -36,7 +38,13 @@ type DB struct {
} }
// Opens or creates the SQLite database at path with WAL mode. // Opens or creates the SQLite database at path with WAL mode.
// Creates parent directories as needed (unless path is ":memory:").
func Open(path string, maxEntries int) (*DB, error) { func Open(path string, maxEntries int) (*DB, error) {
if path != ":memory:" {
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
return nil, fmt.Errorf("create db dir: %w", err)
}
}
db, err := sql.Open("sqlite", path+"?_journal=WAL&_busy_timeout=5000") db, err := sql.Open("sqlite", path+"?_journal=WAL&_busy_timeout=5000")
if err != nil { if err != nil {
return nil, fmt.Errorf("open sqlite: %w", err) return nil, fmt.Errorf("open sqlite: %w", err)