37 lines
958 B
Go
37 lines
958 B
Go
package pgstore
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// Store is the platform's database handle.
|
|
type Store struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
// Open builds the pool. It does NOT connect: pgxpool dials lazily, so a database that is down at
|
|
// boot makes the service unready rather than dead — readiness is the gate, not the process.
|
|
func Open(ctx context.Context, dsn string) (*Store, error) {
|
|
cfg, err := pgxpool.ParseConfig(dsn)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("pgstore: parse dsn: %w", err)
|
|
}
|
|
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("pgstore: pool: %w", err)
|
|
}
|
|
return &Store{pool: pool}, nil
|
|
}
|
|
|
|
// Ping reports whether the database is reachable; it backs /readyz.
|
|
func (s *Store) Ping(ctx context.Context) error {
|
|
if err := s.pool.Ping(ctx); err != nil {
|
|
return fmt.Errorf("pgstore: ping: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *Store) Close() { s.pool.Close() }
|