From ef381377024f14f6ce69c60c3a6e75f46ecc4c8c Mon Sep 17 00:00:00 2001 From: Thanatat Tamtan Date: Sat, 1 Apr 2023 14:05:44 +0700 Subject: [PATCH] pgctx: add RunInReadOnlyTx --- pgctx/pgctx.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/pgctx/pgctx.go b/pgctx/pgctx.go index 6721bef..59ac411 100644 --- a/pgctx/pgctx.go +++ b/pgctx/pgctx.go @@ -104,6 +104,13 @@ func RunInTx(ctx context.Context, f func(ctx context.Context) error) error { return RunInTxOptions(ctx, nil, f) } +// RunInReadOnlyTx calls RunInTxOptions with read only options +func RunInReadOnlyTx(ctx context.Context, f func(ctx context.Context) error) error { + var opts pgsql.TxOptions + opts.TxOptions.ReadOnly = true + return RunInTxOptions(ctx, &opts, f) +} + // IsInTx checks is context inside RunInTx func IsInTx(ctx context.Context) bool { _, ok := ctx.Value(ctxKeyQueryer{}).(*wrapTx)