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:
parent
4b0cdfb735
commit
5ec51aded3
@ -8,6 +8,8 @@ import (
|
|||||||
"database/sql"
|
"database/sql"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
@ -38,6 +40,14 @@ type Model struct {
|
|||||||
func OpenDB(ctx context.Context, dsn string) (*DB, error) {
|
func OpenDB(ctx context.Context, dsn string) (*DB, error) {
|
||||||
log.Info().Str("dsn", dsn).Msg("opening database")
|
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)
|
db, err := openDB(ctx, dsn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
Loading…
x
Reference in New Issue
Block a user