This commit is contained in:
Artem Tsyrulnikov
2026-01-18 13:59:47 +03:00
parent d8202e1141
commit 62e8f98429
62 changed files with 156 additions and 190 deletions

38
pkg/transaction/wrap.go Normal file
View File

@@ -0,0 +1,38 @@
package transaction
import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/rs/zerolog/log"
)
func Wrap(ctx context.Context, fn func(context.Context) error) error {
tx, err := pool.Begin(ctx)
if err != nil {
return fmt.Errorf("pool.Begin: %w", err)
}
defer func() {
err = tx.Rollback(ctx)
if err != nil && !errors.Is(err, pgx.ErrTxClosed) {
log.Error().Err(err).Msg("transaction: Rollback")
}
}()
ctx = context.WithValue(ctx, ctxKey{}, &Transaction{tx})
err = fn(ctx)
if err != nil {
return fmt.Errorf("fn: %w", err)
}
err = tx.Commit(ctx)
if err != nil {
return fmt.Errorf("tx.Commit: %w", err)
}
return nil
}