Skip to content

Commit

Permalink
(OraklNode) Remove context.Background() from the db pkg (#1779)
Browse files Browse the repository at this point in the history
* fix: remove context.Background() from the package

* test: remove configs before insert
  • Loading branch information
nick-bisonai authored Jul 11, 2024
1 parent 0912c0b commit 46be64c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
2 changes: 2 additions & 0 deletions node/pkg/aggregator/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ func setup(ctx context.Context) (func() error, *TestItems, error) {
}

func insertSampleData(ctx context.Context, app *App) (*TmpData, error) {
_ = db.QueryWithoutResult(ctx, DeleteConfigs, nil)

var tmpData = new(TmpData)

tmpConfig, err := db.QueryRow[Config](ctx, InsertConfigQuery, map[string]any{"name": "test_pair", "fetch_interval": 2000, "aggregate_interval": 5000, "submit_interval": 15000})
Expand Down
21 changes: 10 additions & 11 deletions node/pkg/db/pgsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (

// singleton pattern
// make sure env is loaded from main before calling this

var (
initPgxOnce sync.Once
pool *pgxpool.Pool
Expand Down Expand Up @@ -56,7 +55,7 @@ func QueryWithoutResult(ctx context.Context, queryString string, args map[string
return err
}

rows, err := query(currentPool, queryString, args)
rows, err := query(ctx, currentPool, queryString, args)
if err != nil {
log.Error().Err(err).Msg("Error querying")
return err
Expand All @@ -73,7 +72,7 @@ func Query(ctx context.Context, queryString string, args map[string]any) (pgx.Ro
return nil, err
}

return query(currentPool, queryString, args)
return query(ctx, currentPool, queryString, args)
}

func QueryRow[T any](ctx context.Context, queryString string, args map[string]any) (T, error) {
Expand All @@ -84,7 +83,7 @@ func QueryRow[T any](ctx context.Context, queryString string, args map[string]an
return t, err
}

return queryRow[T](currentPool, queryString, args)
return queryRow[T](ctx, currentPool, queryString, args)
}

func QueryRows[T any](ctx context.Context, queryString string, args map[string]any) ([]T, error) {
Expand All @@ -94,16 +93,16 @@ func QueryRows[T any](ctx context.Context, queryString string, args map[string]a
return nil, err
}

return queryRows[T](currentPool, queryString, args)
return queryRows[T](ctx, currentPool, queryString, args)
}

func query(pool *pgxpool.Pool, query string, args map[string]any) (pgx.Rows, error) {
return pool.Query(context.Background(), query, pgx.NamedArgs(args))
func query(ctx context.Context, pool *pgxpool.Pool, query string, args map[string]any) (pgx.Rows, error) {
return pool.Query(ctx, query, pgx.NamedArgs(args))
}

func queryRow[T any](pool *pgxpool.Pool, queryString string, args map[string]any) (T, error) {
func queryRow[T any](ctx context.Context, pool *pgxpool.Pool, queryString string, args map[string]any) (T, error) {
var result T
rows, err := query(pool, queryString, args)
rows, err := query(ctx, pool, queryString, args)
if err != nil {
log.Error().Err(err).Str("query", queryString).Msg("Error querying")
return result, err
Expand All @@ -117,10 +116,10 @@ func queryRow[T any](pool *pgxpool.Pool, queryString string, args map[string]any
return result, err
}

func queryRows[T any](pool *pgxpool.Pool, queryString string, args map[string]any) ([]T, error) {
func queryRows[T any](ctx context.Context, pool *pgxpool.Pool, queryString string, args map[string]any) ([]T, error) {
results := []T{}

rows, err := query(pool, queryString, args)
rows, err := query(ctx, pool, queryString, args)
if err != nil {
log.Error().Err(err).Str("query", queryString).Msg("Error querying")
return results, err
Expand Down

0 comments on commit 46be64c

Please sign in to comment.