feat(db-postgres): initial implementation — pgx pool with lifecycle and UnitOfWork (v1.0.0)
Introduces code.nochebuena.dev/einherjar/db-postgres — the PostgreSQL database starter for the Einherjar framework. Absorbs the postgres package from micro-lib, replacing fmt.Errorf wrapping with core/xerrors and migrating from pgx v4 to pgx v5. Interfaces (CT-6: one TypeSpec per file): - Executor — Exec, Query, QueryRow (pgx-native types) - Tx — Executor + Commit(ctx), Rollback(ctx) - Provider — GetExecutor, Begin, BeginTx, Ping, HandleError - Component — lifecycle.Component + observability.Checkable + Provider + Stats() - UnitOfWork — Do(ctx, fn) Implementation: - New(logger, cfg) Component — pool not created until OnInit - OnInit: pgxpool.NewWithConfig with duration parsing; 30s timeout - OnStart: PING with 5s timeout; logs "postgres: connected" - OnStop: pool.Close(); logs "postgres: closing pool" - GetExecutor: returns active Tx from context (ctxTxKey) or pool; nil-safe - Begin/BeginTx: wraps pgx.TxOptions; wrapped in xerrors on error - HealthCheck: delegates to Ping; Priority LevelCritical - Stats() *pgxpool.Stat — zero value when pool uninitialized - NewUnitOfWork(logger, provider) UnitOfWork — Begin+inject+commit/rollback - HandleError: UniqueViolation→ErrAlreadyExists, ForeignKey/Check→ErrInvalidInput, pgx.ErrNoRows→ErrNotFound, all others→ErrInternal Config (EINHERJAR_PG_* env vars): Host, Port(5432), User, Password, Name, SSLMode(disable), Timezone(UTC), MaxConns(5), MinConns(2), MaxConnLifetime(1h), MaxConnIdleTime(30m), HealthCheckPeriod(1m) - Component interface embeds observability.Identifiable; identifiable.go implements ModulePath and ModuleVersion via runtime/debug.ReadBuildInfo() — prints in launcher banner
This commit is contained in:
45
errors.go
Normal file
45
errors.go
Normal file
@@ -0,0 +1,45 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/jackc/pgerrcode"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgconn"
|
||||
|
||||
"code.nochebuena.dev/einherjar/core/xerrors"
|
||||
)
|
||||
|
||||
// HandleError maps pgx and PostgreSQL errors to typed [xerrors] values.
|
||||
// Returns nil when err is nil. Also available as [Provider.HandleError].
|
||||
//
|
||||
// Mapped codes:
|
||||
// - UniqueViolation → ErrAlreadyExists
|
||||
// - ForeignKeyViolation → ErrInvalidInput
|
||||
// - CheckViolation → ErrInvalidInput
|
||||
// - pgx.ErrNoRows → ErrNotFound
|
||||
// - all others → ErrInternal
|
||||
func HandleError(err error) error {
|
||||
if err == nil {
|
||||
return nil
|
||||
}
|
||||
var pgErr *pgconn.PgError
|
||||
if errors.As(err, &pgErr) {
|
||||
switch pgErr.Code {
|
||||
case pgerrcode.UniqueViolation:
|
||||
return xerrors.New(xerrors.ErrAlreadyExists, "record already exists").
|
||||
WithContext("constraint", pgErr.ConstraintName).WithError(err)
|
||||
case pgerrcode.ForeignKeyViolation:
|
||||
return xerrors.New(xerrors.ErrInvalidInput, "data integrity violation").
|
||||
WithContext("table", pgErr.TableName).WithError(err)
|
||||
case pgerrcode.CheckViolation:
|
||||
return xerrors.New(xerrors.ErrInvalidInput, "data constraint violation").
|
||||
WithContext("table", pgErr.TableName).
|
||||
WithContext("column", pgErr.ColumnName).WithError(err)
|
||||
}
|
||||
}
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return xerrors.New(xerrors.ErrNotFound, "record not found").WithError(err)
|
||||
}
|
||||
return xerrors.New(xerrors.ErrInternal, "unexpected database error").WithError(err)
|
||||
}
|
||||
Reference in New Issue
Block a user