Skip to content

Commit

Permalink
fix: invalidate functions use original ctx
Browse files Browse the repository at this point in the history
Invalidate functions should not use scoped query context, because
when the operation is executed inside a transaction, invalidate
functions happens after commitment, when original queries have been
stopped and those scoped contexts have already been cancelled.
  • Loading branch information
Stumble committed Jul 11, 2024
1 parent 1811445 commit 3991503
Showing 1 changed file with 14 additions and 14 deletions.
28 changes: 14 additions & 14 deletions internal/codegen/golang/templates/wpgx/queryCode.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}}
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}} {{.InvalidateArgs}}) (*{{.Ret.Type}}, error) {
{{- if gt .Option.Timeout.Milliseconds 0 }}
ctx, cancel := context.WithTimeout(ctx, time.Millisecond * {{.Option.Timeout.Milliseconds}})
qctx, cancel := context.WithTimeout(ctx, time.Millisecond * {{.Option.Timeout.Milliseconds}})
defer cancel()
{{- end}}
{{- if .CountIntent }}
q.db.CountIntent("{{.UniqueLabel}}")
{{- end}}
{{- if eq .Option.Cache.Milliseconds 0}}
row := q.db.WQueryRow(ctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
row := q.db.WQueryRow(qctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
var {{.Ret.Name}} *{{.Ret.Type}} = new({{.Ret.Type}})
err := row.Scan({{.Ret.Scan}})
if err == pgx.ErrNoRows {
Expand All @@ -55,7 +55,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}} {{.Invalida
{{else}}
dbRead := func() (any, time.Duration, error) {
cacheDuration := time.Duration(time.Millisecond * {{.Option.Cache.Milliseconds}})
row := q.db.WQueryRow(ctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
row := q.db.WQueryRow(qctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
var {{.Ret.Name}} *{{.Ret.Type}} = new({{.Ret.Type}})
err := row.Scan({{.Ret.Scan}})
if err == pgx.ErrNoRows {
Expand All @@ -69,7 +69,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}} {{.Invalida
}

var {{.Ret.Name}} *{{.Ret.Type}}
err := q.cache.GetWithTtl(ctx, {{.CacheKey}}, &{{.Ret.Name}}, dbRead, false, false)
err := q.cache.GetWithTtl(qctx, {{.CacheKey}}, &{{.Ret.Name}}, dbRead, false, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -108,14 +108,14 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}} {{.Invalida
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}} {{.InvalidateArgs}}) ([]{{.Ret.Type}}, error) {
{{- if gt .Option.Timeout.Milliseconds 0 }}
ctx, cancel := context.WithTimeout(ctx, time.Millisecond * {{.Option.Timeout.Milliseconds}})
qctx, cancel := context.WithTimeout(ctx, time.Millisecond * {{.Option.Timeout.Milliseconds}})
defer cancel()
{{- end}}
{{- if .CountIntent }}
q.db.CountIntent("{{.UniqueLabel}}")
{{- end}}
{{- if eq .Option.Cache.Milliseconds 0}}
rows, err := q.db.WQuery(ctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
rows, err := q.db.WQuery(qctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
if err != nil {
return nil, err
}
Expand All @@ -134,7 +134,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}} {{.Invalida
{{else}}
dbRead := func() (any, time.Duration, error) {
cacheDuration := time.Duration(time.Millisecond * {{.Option.Cache.Milliseconds}})
rows, err := q.db.WQuery(ctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
rows, err := q.db.WQuery(qctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
if err != nil {
return nil, 0, err
}
Expand All @@ -157,7 +157,7 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}} {{.Invalida
return items.([]{{.Ret.Type}}), err
}
var items []{{.Ret.Type}}
err := q.cache.GetWithTtl(ctx, {{.CacheKey}}, &items, dbRead, false, false)
err := q.cache.GetWithTtl(qctx, {{.CacheKey}}, &items, dbRead, false, false)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -196,10 +196,10 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}} {{.Invalida
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}} {{.InvalidateArgs}}) error {
{{- if gt .Option.Timeout.Milliseconds 0 }}
ctx, cancel := context.WithTimeout(ctx, time.Millisecond * {{.Option.Timeout.Milliseconds}})
qctx, cancel := context.WithTimeout(ctx, time.Millisecond * {{.Option.Timeout.Milliseconds}})
defer cancel()
{{- end}}
_, err := q.db.WExec(ctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
_, err := q.db.WExec(qctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
if err != nil {
return err
}
Expand Down Expand Up @@ -236,10 +236,10 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}} {{.Invalida
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}} {{.InvalidateArgs}}) (int64, error) {
{{- if gt .Option.Timeout.Milliseconds 0 }}
ctx, cancel := context.WithTimeout(ctx, time.Millisecond * {{.Option.Timeout.Milliseconds}})
qctx, cancel := context.WithTimeout(ctx, time.Millisecond * {{.Option.Timeout.Milliseconds}})
defer cancel()
{{- end}}
result, err := q.db.WExec(ctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
result, err := q.db.WExec(qctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
if err != nil {
return 0, err
}
Expand Down Expand Up @@ -276,10 +276,10 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}} {{.Invalida
{{end -}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}} {{.InvalidateArgs}}) (pgconn.CommandTag, error) {
{{- if gt .Option.Timeout.Milliseconds 0 }}
ctx, cancel := context.WithTimeout(ctx, time.Millisecond * {{.Option.Timeout.Milliseconds}})
qctx, cancel := context.WithTimeout(ctx, time.Millisecond * {{.Option.Timeout.Milliseconds}})
defer cancel()
{{- end}}
rv, err := q.db.WExec(ctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
rv, err := q.db.WExec(qctx, "{{.UniqueLabel}}", {{.ConstantName}}, {{.Arg.Params}})
if err != nil {
return rv, err
}
Expand Down

0 comments on commit 3991503

Please sign in to comment.