Replace direct *pgxpool.Pool usage with PoolProxy wrapper across the entire backend: - db.DB renamed to db.Conn (*pgxpool.Pool -> *PoolProxy) - JWT functions now accept context.Context instead of using context.Background() - Handler DB calls route through PoolProxy for per-test transaction support - Fixture/helper/testdb functions accept Querier interface for decoupling - Query ordering fixed in bookings handlers: COUNT after data query to avoid pgx conn busy - Time truncation fixed: time.Date instead of Truncate(24*time.Hour) for week start calc - testmain_test.go files updated with SeedBaseline and NewPoolProxy Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
65 lines
1.0 KiB
Go
65 lines
1.0 KiB
Go
//go:build !dev
|
|
// +build !dev
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
var Conn *PoolProxy
|
|
|
|
func Connect() error {
|
|
// Connect to Postgres on local network (127.x.x.x)
|
|
dsn := fmt.Sprintf(
|
|
"postgres://%s:%s@%s:5432/%s?require_auth=scram-sha-256",
|
|
getEnv("POSTGRES_USER"),
|
|
getEnv("POSTGRES_PASSWORD"),
|
|
getEnv("POSTGRES_HOST"),
|
|
getEnv("POSTGRES_DB"),
|
|
)
|
|
|
|
pool, err := pgxpool.New(context.Background(), dsn)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
Conn = NewPoolProxy(pool)
|
|
|
|
err = testDB()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func testDB() error {
|
|
ctx := context.Background()
|
|
conn, err := Conn.Acquire(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer conn.Release()
|
|
|
|
row := conn.QueryRow(ctx, "SELECT 1")
|
|
var result int
|
|
err = row.Scan(&result)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getEnv(key string) string {
|
|
if val := os.Getenv(key); val != "" {
|
|
return val
|
|
}
|
|
// Return empty string instead of fatal error - allows tests to run without prod env vars
|
|
return ""
|
|
}
|