Set UTC timezone in pgxpool config to ensure consistent timestamp handling. Add detailed doc comment to Querier interface clarifying QueryRow vs Querier distinction. Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent) Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
69 lines
1.1 KiB
Go
69 lines
1.1 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 inside Docker network
|
|
dsn := fmt.Sprintf(
|
|
"postgres://%s:%s@localhost:5432/%s?sslmode=disable&require_auth=scram-sha-256",
|
|
getEnv("POSTGRES_USER"),
|
|
getEnv("POSTGRES_PASSWORD"),
|
|
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 ""
|
|
}
|