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.
This commit is contained in:
Sybren A. Stüvel 2025-08-01 17:50:39 +02:00
parent 4b0cdfb735
commit 5ec51aded3

View File

@ -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