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

Function NewFromURL

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

NewFromURL creates a pgx pool and sqlc Queries from a raw database URL. Used for per-host pools; no retry logic. Caller must ensure the database is reachable. maxConns and minConns override pool size (0 uses defaults: 5 max, 1 min).

(ctx context.Context, databaseURL string, maxConns, minConns int, cfg *config.Config)

Source from the content-addressed store, hash-verified

59// Used for per-host pools; no retry logic. Caller must ensure the database is reachable.
60// maxConns and minConns override pool size (0 uses defaults: 5 max, 1 min).
61func NewFromURL(ctx context.Context, databaseURL string, maxConns, minConns int, cfg *config.Config) (*DB, error) {
62 if databaseURL == "" {
63 return nil, fmt.Errorf("database URL is required")
64 }
65 poolCfg, err := pgxpool.ParseConfig(databaseURL)
66 if err != nil {
67 return nil, fmt.Errorf("parse pool config: %w", err)
68 }
69 if maxConns <= 0 {
70 maxConns = 5
71 }
72 if minConns < 0 {
73 minConns = 1
74 }
75 poolCfg.MaxConns = safeconv.ClampToInt32(maxConns)
76 poolCfg.MinConns = safeconv.ClampToInt32(minConns)
77 poolCfg.ConnConfig.ConnectTimeout = time.Duration(cfg.DBConnectTimeout) * time.Second
78 poolCfg.HealthCheckPeriod = 30 * time.Second
79 setUTCTimeZone(poolCfg)
80 pool, err := pgxpool.NewWithConfig(ctx, poolCfg)
81 if err != nil {
82 return nil, fmt.Errorf("create pgx pool: %w", err)
83 }
84 if err := pool.Ping(ctx); err != nil {
85 pool.Close()
86 return nil, fmt.Errorf("ping host database: %w", err)
87 }
88 return &DB{
89 pool: pool,
90 Queries: db.New(pool),
91 cfg: cfg,
92 }, nil
93}
94
95// waitForDB retries connection until DB is available.
96func (d *DB) waitForDB(ctx context.Context) error {

Callers

nothing calls this directly

Calls 3

setUTCTimeZoneFunction · 0.85
PingMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected