Files
Crussell/backend/db/db.go
T
popertotsandSisyphus 510828c924
CI / Go vulnerabilities (push) Successful in 1m10s
CI / Build & Vet (push) Successful in 1m39s
CI / Frontend build (gate) (push) Successful in 1m42s
CI / Frontend QC (audit) (push) Successful in 56s
CI / Frontend QC (typecheck) (push) Successful in 1m36s
CI / Frontend QC (lint) (push) Successful in 1m51s
CI / Tests (prod) (push) Has been cancelled
CI / Tests (dev) (push) Has been cancelled
CI / Race (prod) (push) Has been cancelled
CI / Race (dev) (push) Has been cancelled
chore: run go fix for Go 1.26 modernization
106 files: interface{}→any, strings.Split→SplitSeq, CutPrefix/Cut, strings.Builder, slices.Contains, remove redundant // +build directives, gofmt import ordering and indentation.

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

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-07-09 17:25:23 +01:00

69 lines
1.1 KiB
Go

//go: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"),
)
poolCfg, err := pgxpool.ParseConfig(dsn)
if err != nil {
return err
}
poolCfg.ConnConfig.RuntimeParams["timezone"] = "UTC"
pool, err := pgxpool.NewWithConfig(context.Background(), poolCfg)
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 ""
}