2025-01-31 13:43:55 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"queryorchestration/internal/database/repository"
|
|
|
|
|
)
|
|
|
|
|
|
2025-02-03 17:30:50 +00:00
|
|
|
func (b *DBConfig) ExecuteDBTransaction(ctx context.Context, executeQueries func(context.Context, *repository.Queries) error) error {
|
2025-01-31 13:43:55 +00:00
|
|
|
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
|
|
|
|
|
}
|