MCPcopy Index your code
hub / github.com/PatchMon/PatchMon / NewDB

Function NewDB

server-source-code/internal/database/db.go:30–56  ·  view source on GitHub ↗

NewDB creates a pgx pool and sqlc Queries with retry logic.

(ctx context.Context, cfg *config.Config)

Source from the content-addressed store, hash-verified

28
29// NewDB creates a pgx pool and sqlc Queries with retry logic.
30func NewDB(ctx context.Context, cfg *config.Config) (*DB, error) {
31 poolCfg, err := pgxpool.ParseConfig(cfg.DatabaseURL)
32 if err != nil {
33 return nil, fmt.Errorf("parse pool config: %w", err)
34 }
35 poolCfg.MaxConns = safeconv.ClampToInt32(cfg.DBConnectionLimit)
36 poolCfg.ConnConfig.ConnectTimeout = time.Duration(cfg.DBConnectTimeout) * time.Second
37 // Force UTC session time zone so NOW()/CURRENT_TIMESTAMP stored into tz-naive
38 // TIMESTAMP columns are UTC wall-clock, matching pgx's UTC assumption on read.
39 setUTCTimeZone(poolCfg)
40 pool, err := pgxpool.NewWithConfig(ctx, poolCfg)
41 if err != nil {
42 return nil, fmt.Errorf("create pgx pool: %w", err)
43 }
44
45 d := &DB{
46 pool: pool,
47 Queries: db.New(pool),
48 cfg: cfg,
49 }
50 if err := d.waitForDB(ctx); err != nil {
51 pool.Close()
52 return nil, err
53 }
54
55 return d, nil
56}
57
58// NewFromURL creates a pgx pool and sqlc Queries from a raw database URL.
59// Used for per-host pools; no retry logic. Caller must ensure the database is reachable.

Callers

nothing calls this directly

Calls 3

waitForDBMethod · 0.95
setUTCTimeZoneFunction · 0.85
CloseMethod · 0.45

Tested by

no test coverage detected