46 lines
1 KiB
Go
46 lines
1 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
dbgen "drive.local/drivev2/internal/adapters/postgres/generated"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// Store owns the PostgreSQL connection pool and generated repositories.
|
|
type Store struct {
|
|
pool *pgxpool.Pool
|
|
queries *dbgen.Queries
|
|
}
|
|
|
|
func Open(ctx context.Context, databaseURL string) (*Store, error) {
|
|
poolConfig, err := pgxpool.ParseConfig(databaseURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse database configuration: %w", err)
|
|
}
|
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, poolConfig)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create database pool: %w", err)
|
|
}
|
|
if err := pool.Ping(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("ping database: %w", err)
|
|
}
|
|
|
|
return &Store{pool: pool, queries: dbgen.New(pool)}, nil
|
|
}
|
|
|
|
func (s *Store) Close() {
|
|
s.pool.Close()
|
|
}
|
|
|
|
func (s *Store) DatabaseHealth(ctx context.Context) error {
|
|
_, err := s.queries.DatabaseHealth(ctx)
|
|
return err
|
|
}
|
|
|
|
func (s *Store) OwnerExists(ctx context.Context) (bool, error) {
|
|
return s.queries.OwnerExists(ctx)
|
|
}
|