forked from mtusi2026/mtusi2026lab3
work 3
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/config"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/domain"
|
||||
"gitflic.ru/project/relationskatie/mtusi2026lab1-ashchkl/internal/store"
|
||||
)
|
||||
|
||||
var _ store.Store = (*DB)(nil)
|
||||
|
||||
type DB struct {
|
||||
*Users
|
||||
*Chats
|
||||
*Messages
|
||||
|
||||
pool *pgxpool.Pool
|
||||
}
|
||||
|
||||
func New(pool *pgxpool.Pool, queryTimeout time.Duration) *DB {
|
||||
c := conn{pool: pool, timeout: queryTimeout}
|
||||
return &DB{
|
||||
Users: &Users{conn: c},
|
||||
Chats: &Chats{conn: c},
|
||||
Messages: &Messages{conn: c},
|
||||
pool: pool,
|
||||
}
|
||||
}
|
||||
|
||||
func Connect(ctx context.Context, cfg config.Database) (*pgxpool.Pool, error) {
|
||||
dsn, err := dsnFrom(cfg)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
pcfg, err := pgxpool.ParseConfig(dsn)
|
||||
if err != nil {
|
||||
return nil, errors.New("db: invalid DSN")
|
||||
}
|
||||
pcfg.MaxConns = cfg.MaxConns
|
||||
pcfg.ConnConfig.ConnectTimeout = time.Duration(cfg.ConnectTimeoutMs) * time.Millisecond
|
||||
if !cfg.EnableBitmapScan {
|
||||
pcfg.ConnConfig.RuntimeParams["enable_bitmapscan"] = "off"
|
||||
}
|
||||
|
||||
pool, err := pgxpool.NewWithConfig(ctx, pcfg)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("db: pool: %w", err)
|
||||
}
|
||||
|
||||
pingCtx, cancel := context.WithTimeout(ctx, pcfg.ConnConfig.ConnectTimeout)
|
||||
defer cancel()
|
||||
if err := pool.Ping(pingCtx); err != nil {
|
||||
pool.Close()
|
||||
return nil, fmt.Errorf("db: ping: %w", err)
|
||||
}
|
||||
return pool, nil
|
||||
}
|
||||
|
||||
func dsnFrom(cfg config.Database) (string, error) {
|
||||
if cfg.URLFile != "" {
|
||||
b, err := os.ReadFile(cfg.URLFile)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("db: read DATABASE_URL_FILE: %w", err)
|
||||
}
|
||||
return strings.TrimSpace(string(b)), nil
|
||||
}
|
||||
if cfg.URL != "" {
|
||||
return cfg.URL, nil
|
||||
}
|
||||
return "", errors.New("db: DATABASE_URL_FILE or DATABASE_URL is required")
|
||||
}
|
||||
|
||||
func (d *DB) Ping(ctx context.Context) error {
|
||||
return d.pool.Ping(ctx)
|
||||
}
|
||||
|
||||
func (d *DB) Close() {
|
||||
d.pool.Close()
|
||||
}
|
||||
|
||||
type conn struct {
|
||||
pool *pgxpool.Pool
|
||||
timeout time.Duration
|
||||
}
|
||||
|
||||
func (c conn) withTimeout(parent context.Context) (context.Context, context.CancelFunc) {
|
||||
return context.WithTimeout(parent, c.timeout)
|
||||
}
|
||||
|
||||
|
||||
const (
|
||||
codeUniqueViolation = "23505"
|
||||
codeForeignKeyViolation = "23503"
|
||||
codeCheckViolation = "23514"
|
||||
codeInvalidTextRepr = "22P02"
|
||||
)
|
||||
|
||||
func mapErr(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return domain.ErrNotFound
|
||||
}
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
switch pgErr.Code {
|
||||
case codeUniqueViolation:
|
||||
return domain.ErrConflict
|
||||
case codeForeignKeyViolation, codeCheckViolation, codeInvalidTextRepr:
|
||||
return domain.ErrInvalidArgument
|
||||
}
|
||||
switch pgErr.Code[:2] {
|
||||
case "08", "53", "57":
|
||||
return fmt.Errorf("%w: db: %s", domain.ErrUnavailable, pgErr.Code)
|
||||
}
|
||||
return fmt.Errorf("db: %s: %w", pgErr.Code, err)
|
||||
}
|
||||
return fmt.Errorf("%w: db: %v", domain.ErrUnavailable, err)
|
||||
}
|
||||
|
||||
func isFKViolation(err error) bool {
|
||||
var pgErr *pgconn.PgError
|
||||
return errors.As(err, &pgErr) && pgErr.Code == codeForeignKeyViolation
|
||||
}
|
||||
Reference in New Issue
Block a user