From 5ec51aded372e24091f23035ab67fd8e9e4f56bf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sybren=20A=2E=20St=C3=BCvel?= Date: Fri, 1 Aug 2025 17:50:39 +0200 Subject: [PATCH] Create sqlite directory if it doesn't exist yet If the dir doesn't exist, sqlite will come back with a cryptic error message "unable to open database file: out of memory (14)". Better to just create it. --- internal/manager/persistence/db.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/manager/persistence/db.go b/internal/manager/persistence/db.go index d68e7761..183610ce 100644 --- a/internal/manager/persistence/db.go +++ b/internal/manager/persistence/db.go @@ -8,6 +8,8 @@ import ( "database/sql" "errors" "fmt" + "os" + "path/filepath" "time" "github.com/rs/zerolog/log" @@ -38,6 +40,14 @@ type Model struct { func OpenDB(ctx context.Context, dsn string) (*DB, error) { log.Info().Str("dsn", dsn).Msg("opening database") + // 'dsn' should just be a file path to a sqlite file. If its directory doesn't + // exist yet, create it. Otherwise sqlite will come back with a cryptic error + // message "unable to open database file: out of memory (14)". + dbDirectory := filepath.Dir(dsn) + if err := os.MkdirAll(dbDirectory, os.ModePerm); err != nil { + return nil, fmt.Errorf("creating database directory %s: %w", dbDirectory, err) + } + db, err := openDB(ctx, dsn) if err != nil { return nil, err