Brijesh's Git Server — whodis @ 750adae7608f07dd20689326fb3f378911f9a9c4

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()
}