Initial phase 1 baseline implementation
This commit is contained in:
parent
099c53badf
commit
077cf7601a
29 changed files with 1339 additions and 39 deletions
46
internal/adapters/postgres/store.go
Normal file
46
internal/adapters/postgres/store.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
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)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue