refactor(backend): migrate db.DB to db.Conn PoolProxy across all handlers

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>
This commit is contained in:
2026-06-21 19:28:54 +01:00
co-authored by Sisyphus
parent c69a243f75
commit 3d0e2afc4c
39 changed files with 751 additions and 638 deletions
+10 -10
View File
@@ -120,8 +120,15 @@ func GetNotifications(w http.ResponseWriter, r *http.Request) {
baseQuery += fmt.Sprintf(" LIMIT $%d", param)
args = append(args, perPage+1)
var total int
countWhere := ""
if !includeAcknowledged {
countWhere += " WHERE an.acknowledged_at IS NULL"
}
db.Conn.QueryRow(r.Context(), "SELECT COUNT(*) FROM admin_notifications an"+countWhere).Scan(&total)
// Query
rows, err := db.DB.Query(r.Context(), baseQuery, args...)
rows, err := db.Conn.Query(r.Context(), baseQuery, args...)
if err != nil {
log.Printf("Failed to fetch notifications: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
@@ -129,13 +136,6 @@ func GetNotifications(w http.ResponseWriter, r *http.Request) {
}
defer rows.Close()
var total int
countWhere := ""
if !includeAcknowledged {
countWhere += " WHERE an.acknowledged_at IS NULL"
}
db.DB.QueryRow(r.Context(), "SELECT COUNT(*) FROM admin_notifications an"+countWhere).Scan(&total)
var notifications []AdminNotification
for rows.Next() {
@@ -208,7 +208,7 @@ func GetNotifications(w http.ResponseWriter, r *http.Request) {
// Returns the count of unacknowledged notifications for the bell icon.
func GetUnreadCount(w http.ResponseWriter, r *http.Request) {
var count int
err := db.DB.QueryRow(r.Context(),
err := db.Conn.QueryRow(r.Context(),
`SELECT COUNT(*) FROM admin_notifications WHERE acknowledged_at IS NULL`,
).Scan(&count)
if err != nil {
@@ -238,7 +238,7 @@ func AcknowledgeNotification(w http.ResponseWriter, r *http.Request) {
WHERE id = $1 AND acknowledged_at IS NULL
`
cmdTag, err := db.DB.Exec(r.Context(), query, idStr)
cmdTag, err := db.Conn.Exec(r.Context(), query, idStr)
if err != nil {
log.Printf("Failed to acknowledge notification %s: %v", idStr, err)
http.Error(w, "Internal server error", http.StatusInternalServerError)