From 6399196fe5c9c6706a0fd58b6e26c1ffe6d56be4 Mon Sep 17 00:00:00 2001 From: William Petit Date: Fri, 3 Mar 2023 16:37:19 +0100 Subject: [PATCH] chore(sqlite): add Open() utility func --- cmd/cli/command/app/run.go | 7 ++----- pkg/storage/sqlite/sql.go | 11 +++++++++++ 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/cmd/cli/command/app/run.go b/cmd/cli/command/app/run.go index 89eb0a6..3f7b28f 100644 --- a/cmd/cli/command/app/run.go +++ b/cmd/cli/command/app/run.go @@ -1,7 +1,6 @@ package app import ( - "database/sql" "fmt" "net/http" "path/filepath" @@ -26,8 +25,6 @@ import ( "github.com/golang-jwt/jwt" "github.com/pkg/errors" "github.com/urfave/cli/v2" - - _ "modernc.org/sqlite" ) func RunCommand() *cli.Command { @@ -115,9 +112,9 @@ func RunCommand() *cli.Command { bus := memory.NewBus() - db, err := sql.Open("sqlite", storageFile) + db, err := sqlite.Open(storageFile) if err != nil { - return errors.Wrapf(err, "could not open database with path '%s'", storageFile) + return errors.WithStack(err) } ds := sqlite.NewDocumentStoreWithDB(db) diff --git a/pkg/storage/sqlite/sql.go b/pkg/storage/sqlite/sql.go index f8d1e96..bd4f34b 100644 --- a/pkg/storage/sqlite/sql.go +++ b/pkg/storage/sqlite/sql.go @@ -7,8 +7,19 @@ import ( "github.com/pkg/errors" "gitlab.com/wpetit/goweb/logger" + + _ "modernc.org/sqlite" ) +func Open(path string) (*sql.DB, error) { + db, err := sql.Open("sqlite", path) + if err != nil { + return nil, errors.Wrapf(err, "could not open database with path '%s'", path) + } + + return db, nil +} + func withTx(ctx context.Context, db *sql.DB, fn func(tx *sql.Tx) error) error { var tx *sql.Tx