Export Transaction from the database

This commit is contained in:
2025-05-10 21:51:35 +02:00
parent a9a9b4d9bb
commit eac3bc4ff5

View File

@ -5,7 +5,6 @@
package database package database
import ( import (
"context"
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
@ -135,8 +134,12 @@ func (db *DB) CheckSchemaVersion() error {
return nil return nil
} }
func (db *DB) transaction(ctx context.Context, fn func(*sql.Tx) error) error { // Transaction executes the provided function within a SQL transaction.
tx, err := db.conn.BeginTx(ctx, nil) // If the function returns an error, the transaction is rolled back.
// If the function panics, the transaction is rolled back and the panic is re-thrown.
// The function receives a *sql.Tx that can be used for database operations.
func (db *DB) Transaction(fn func(*sql.Tx) error) error {
tx, err := db.conn.Begin()
if err != nil { if err != nil {
return fmt.Errorf("failed to begin transaction: %w", err) return fmt.Errorf("failed to begin transaction: %w", err)
} }