diff --git a/server/db/meta.go b/server/db/meta.go index 397b466..2f85bef 100644 --- a/server/db/meta.go +++ b/server/db/meta.go @@ -6,12 +6,20 @@ import ( "gogs.mikescher.com/BlackForestBytes/goext/langext" ) -func (db *Database) ReadSchema(ctx context.Context) (int, error) { +func (db *Database) ReadSchema(ctx context.Context) (retval int, reterr error) { r1, err := db.db.QueryContext(ctx, "SELECT name FROM sqlite_master WHERE type='table' AND name='meta'") if err != nil { return 0, err } + defer func() { + err = r1.Close() + if err != nil { + // overwrite return values + retval = 0 + reterr = err + } + }() if !r1.Next() { return 0, nil @@ -21,6 +29,15 @@ func (db *Database) ReadSchema(ctx context.Context) (int, error) { if err != nil { return 0, err } + defer func() { + err = r2.Close() + if err != nil { + // overwrite return values + retval = 0 + reterr = err + } + }() + if !r2.Next() { return 0, errors.New("no schema entry in meta table") } @@ -78,11 +95,19 @@ func (db *Database) WriteMetaBlob(ctx context.Context, key string, value []byte) return nil } -func (db *Database) ReadMetaString(ctx context.Context, key string) (*string, error) { +func (db *Database) ReadMetaString(ctx context.Context, key string) (retval *string, reterr error) { r2, err := db.db.QueryContext(ctx, "SELECT value_txt FROM meta WHERE meta_key=?", key) if err != nil { return nil, err } + defer func() { + err = r2.Close() + if err != nil { + // overwrite return values + retval = nil + reterr = err + } + }() if !r2.Next() { return nil, errors.New("no matching entry in meta table") } @@ -96,11 +121,20 @@ func (db *Database) ReadMetaString(ctx context.Context, key string) (*string, er return langext.Ptr(value), nil } -func (db *Database) ReadMetaInt(ctx context.Context, key string) (*int64, error) { +func (db *Database) ReadMetaInt(ctx context.Context, key string) (retval *int64, reterr error) { r2, err := db.db.QueryContext(ctx, "SELECT value_int FROM meta WHERE meta_key=?", key) if err != nil { return nil, err } + defer func() { + err = r2.Close() + if err != nil { + // overwrite return values + retval = nil + reterr = err + } + }() + if !r2.Next() { return nil, errors.New("no matching entry in meta table") } @@ -114,11 +148,20 @@ func (db *Database) ReadMetaInt(ctx context.Context, key string) (*int64, error) return langext.Ptr(value), nil } -func (db *Database) ReadMetaReal(ctx context.Context, key string) (*float64, error) { +func (db *Database) ReadMetaReal(ctx context.Context, key string) (retval *float64, reterr error) { r2, err := db.db.QueryContext(ctx, "SELECT value_real FROM meta WHERE meta_key=?", key) if err != nil { return nil, err } + defer func() { + err = r2.Close() + if err != nil { + // overwrite return values + retval = nil + reterr = err + } + }() + if !r2.Next() { return nil, errors.New("no matching entry in meta table") } @@ -132,11 +175,20 @@ func (db *Database) ReadMetaReal(ctx context.Context, key string) (*float64, err return langext.Ptr(value), nil } -func (db *Database) ReadMetaBlob(ctx context.Context, key string) (*[]byte, error) { +func (db *Database) ReadMetaBlob(ctx context.Context, key string) (retval *[]byte, reterr error) { r2, err := db.db.QueryContext(ctx, "SELECT value_blob FROM meta WHERE meta_key=?", key) if err != nil { return nil, err } + defer func() { + err = r2.Close() + if err != nil { + // overwrite return values + retval = nil + reterr = err + } + }() + if !r2.Next() { return nil, errors.New("no matching entry in meta table") } diff --git a/server/test/util/init.go b/server/test/util/init.go new file mode 100644 index 0000000..3e06010 --- /dev/null +++ b/server/test/util/init.go @@ -0,0 +1,27 @@ +package util + +import ( + "github.com/gin-gonic/gin" + "github.com/rs/zerolog" + "github.com/rs/zerolog/log" + "os" +) + +func InitTests() { + cw := zerolog.ConsoleWriter{ + Out: os.Stdout, + TimeFormat: "2006-01-02 15:04:05.000 Z07:00", + } + + zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMs + multi := zerolog.MultiLevelWriter(cw) + logger := zerolog.New(multi).With(). + Timestamp(). + Caller(). + Logger() + + log.Logger = logger + + gin.SetMode(gin.TestMode) + zerolog.SetGlobalLevel(zerolog.DebugLevel) +} diff --git a/server/test/util/webserver.go b/server/test/util/webserver.go index e9303c6..fc19800 100644 --- a/server/test/util/webserver.go +++ b/server/test/util/webserver.go @@ -10,9 +10,6 @@ import ( "blackforestbytes.com/simplecloudnotifier/logic" "blackforestbytes.com/simplecloudnotifier/push" "fmt" - "github.com/gin-gonic/gin" - "github.com/rs/zerolog" - "github.com/rs/zerolog/log" "gogs.mikescher.com/BlackForestBytes/goext/langext" "os" "path/filepath" @@ -23,22 +20,7 @@ import ( type Void = struct{} func StartSimpleWebserver(t *testing.T) (*logic.Application, func()) { - cw := zerolog.ConsoleWriter{ - Out: os.Stdout, - TimeFormat: "2006-01-02 15:04:05 Z07:00", - } - - zerolog.TimeFieldFormat = zerolog.TimeFormatUnix - multi := zerolog.MultiLevelWriter(cw) - logger := zerolog.New(multi).With(). - Timestamp(). - Caller(). - Logger() - - log.Logger = logger - - gin.SetMode(gin.TestMode) - zerolog.SetGlobalLevel(zerolog.DebugLevel) + InitTests() uuid2, _ := langext.NewHexUUID() dbdir := t.TempDir()