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)
| 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). |
| 61 | func 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. |
| 96 | func (d *DB) waitForDB(ctx context.Context) error { |
nothing calls this directly
no test coverage detected