Files
Crussell/backend/internal/dav/service_prod.go
T
popertots 4e64e32f09 fix: dav prod-tag test panic (DB init in package init), README SPV-only VAT claim
Quick wins from the Loop B close-out:
- backend/internal/dav/service_prod.go: package-level init() connected to
  postgres and panicked when the DB was unreachable — breaking bare-shell
  'go test -tags test,!dev' and any CI test-prod run without a live service.
  init() now skips connecting under GO_TESTING (the pipeline sets it) or
  DAV_SKIP_INIT, and defaults POSTGRES_HOST to 127.0.0.1 (the pipeline
  postgres-service address, matching testutils/testdb) so real prod builds
  still connect. The test suite wires its own pool in TestMain.
- README.md:21: corrected stale 'Gift card SPV/MPV VAT treatment configurable'
  to the SPV-only posture (a stored MPV is overridden to SPV at read time).

Verified: 26/26 dev packages, both vet tags clean.
2026-08-22 00:34:50 +01:00

63 lines
1.3 KiB
Go

//go:build !dev
package dav
import (
"context"
"fmt"
"os"
"github.com/jackc/pgx/v5/pgxpool"
)
var Service *BaseService
func init() {
// Test builds wire their own pool in TestMain (testutils/testdb) — a real
// connect here would race that and panic a bare-shell `go test`. Skip.
if os.Getenv("GO_TESTING") != "" || os.Getenv("DAV_SKIP_INIT") != "" {
return
}
if err := connect(); err != nil {
panic("failed to initialize prod service: " + err.Error())
}
}
func connect() error {
host := getEnv("POSTGRES_HOST")
if host == "" {
host = "127.0.0.1" // pipeline postgres service default
}
dsn := fmt.Sprintf(
"postgres://%s:%s@%s:5432/%s?timezone=UTC&require_auth=scram-sha-256",
getEnv("POSTGRES_USER"),
getEnv("POSTGRES_PASSWORD"),
host,
getEnv("POSTGRES_DB"),
)
pool, err := pgxpool.New(context.Background(), dsn)
if err != nil {
return err
}
Service = newBaseService(pool)
return testDB(pool)
}
func testDB(pool *pgxpool.Pool) error {
var n int
err := pool.QueryRow(context.Background(), "SELECT 1").Scan(&n)
if err != nil || n != 1 {
return fmt.Errorf("db test failed: %w", err)
}
return nil
}
func getEnv(key string) string {
if v := os.Getenv(key); v != "" {
return v
}
// Return empty string instead of fatal error - allows tests to run without prod env vars
return ""
}