Brijesh's Git Server — whodis @ fd45c6d452a86cba84f205a0ac4d3215ee91cfa1

built this as a refresher on handling webauthn

core/internal/database/transaction.go (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
package database

import (
	"context"
	"database/sql"

	_ "github.com/mattn/go-sqlite3"
)

func (s *service) withTransaction(ctx context.Context, fn func(*sql.Tx) error) error {
	tx, err := s.db.BeginTx(ctx, nil)
	if err != nil {
		return err
	}

	defer tx.Rollback()

	if err := fn(tx); err != nil {
		return err
	}

	return tx.Commit()
}