Skip to content

Commit ac3e6f1

Browse files
committed
fix: solved minor error on PoolHandler interface (part II)
1 parent b3c37a3 commit ac3e6f1

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

sql/pgxpool/service.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type (
1414
Service interface {
1515
Pool() *pgxpool.Pool
1616
Migrate(queries ...string) error
17-
CreateTransaction(fn func(tx pgx.Tx) error) error
17+
CreateTransaction(fn func(ctx context.Context, tx pgx.Tx) error) error
1818
CreateTransactionWithCtx(
1919
ctx context.Context,
2020
fn func(ctx context.Context, tx pgx.Tx) error,
@@ -69,7 +69,7 @@ func (d *DefaultService) Migrate(queries ...string) error {
6969

7070
// Create a new transaction
7171
return d.CreateTransaction(
72-
func(tx pgx.Tx) error {
72+
func(ctx context.Context, tx pgx.Tx) error {
7373
// Execute the migration
7474
for _, query := range queries {
7575
if _, err := tx.Exec(context.Background(), query); err != nil {
@@ -82,14 +82,19 @@ func (d *DefaultService) Migrate(queries ...string) error {
8282
}
8383

8484
// CreateTransaction creates a transaction for the database
85-
func (d *DefaultService) CreateTransaction(fn func(tx pgx.Tx) error) error {
85+
func (d *DefaultService) CreateTransaction(
86+
fn func(
87+
ctx context.Context,
88+
tx pgx.Tx,
89+
) error,
90+
) error {
8691
return CreateTransaction(d.pool, fn)
8792
}
8893

8994
// CreateTransactionWithCtx creates a transaction for the database with a context
9095
func (d *DefaultService) CreateTransactionWithCtx(
9196
ctx context.Context,
92-
fn func(tx pgx.Tx) error,
97+
fn func(ctx context.Context, tx pgx.Tx) error,
9398
) error {
9499
return CreateTransactionWithCtx(ctx, d.pool, fn)
95100
}

sql/pgxpool/transaction.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
func CreateTransactionWithCtx(
1212
ctx context.Context,
1313
pool *pgxpool.Pool,
14-
fn func(tx pgx.Tx) error,
14+
fn func(ctx context.Context, tx pgx.Tx) error,
1515
) error {
1616
// Check if the pool is nil
1717
if pool == nil {
@@ -25,7 +25,7 @@ func CreateTransactionWithCtx(
2525
}
2626

2727
// Execute the transaction function
28-
if fnErr := fn(tx); fnErr != nil {
28+
if fnErr := fn(ctx, tx); fnErr != nil {
2929
err = tx.Rollback(ctx)
3030
if err != nil {
3131
return err
@@ -38,6 +38,9 @@ func CreateTransactionWithCtx(
3838
}
3939

4040
// CreateTransaction creates a transaction for the database
41-
func CreateTransaction(pool *pgxpool.Pool, fn func(tx pgx.Tx) error) error {
41+
func CreateTransaction(
42+
pool *pgxpool.Pool,
43+
fn func(ctx context.Context, tx pgx.Tx) error,
44+
) error {
4245
return CreateTransactionWithCtx(context.Background(), pool, fn)
4346
}

0 commit comments

Comments
 (0)