31 lines
506 B
Go
31 lines
506 B
Go
|
|
package database
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"queryorchestration/internal/database/repository"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (b *BaseConfig) ExecuteDBTransaction(ctx context.Context, executeQueries func(context.Context, *repository.Queries) error) error {
|
||
|
|
tx, err := b.DBPool.Begin(ctx)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
defer func() {
|
||
|
|
_ = tx.Rollback(ctx)
|
||
|
|
}()
|
||
|
|
|
||
|
|
qtx := b.DBQueries.WithTx(tx)
|
||
|
|
|
||
|
|
err = executeQueries(ctx, qtx)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
err = tx.Commit(ctx)
|
||
|
|
if err != nil {
|
||
|
|
return err
|
||
|
|
}
|
||
|
|
|
||
|
|
return nil
|
||
|
|
}
|