Files
popertotsandSisyphus c69a243f75 feat(backend): add PoolProxy, Querier, and per-test transaction infrastructure
New types and helpers for context-aware DB routing and per-test transactions:

- PoolProxy: wraps pgxpool.Pool, routes queries through context transaction when active
- Querier: interface accepted by fixture/helper functions for decoupling
- ContextWithTx / TxFromContext: store/extract pgx.Tx in context.Context
- SetupTestTx (in testutils): begins tx, stores in context, auto-rolls back on cleanup
- SetupTestTx (in testtx): package-level variant with semaphore for parallel safety

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-06-21 19:28:18 +01:00

33 lines
700 B
Go

//go:build test
// +build test
package testtx
import (
"context"
"testing"
"crussell/db"
)
// txSemaphore limits concurrent test transactions per package.
var txSemaphore = make(chan struct{}, 8)
// SetupTestTx begins a transaction, stores it in context, and auto-rolls back.
func SetupTestTx(t *testing.T) (context.Context, db.Querier) {
t.Helper()
txSemaphore <- struct{}{}
pool := db.Conn.Pool()
tx, err := pool.Begin(context.Background())
if err != nil {
<-txSemaphore
t.Fatalf("SetupTestTx: failed to begin transaction: %v", err)
}
ctx := db.ContextWithTx(context.Background(), tx)
t.Cleanup(func() {
tx.Rollback(context.Background())
<-txSemaphore
})
return ctx, tx
}