feat(postgres)!: promote to v1.0.0 — BeginTx with pgx options, Stats, bump all deps to v1.0.0
Add BeginTx(ctx, pgx.TxOptions) to Client interface for explicit isolation level and
read-only transaction control; Begin refactored as a convenience wrapper calling
BeginTx(ctx, pgx.TxOptions{}). Add Stats() *pgxpool.Stat to Component interface for
connection pool observability. Bump all micro-lib dependencies (logz, health, launcher,
xerrors) from v0.9.0 to v1.0.0. API committed as stable.
This commit is contained in:
20
postgres.go
20
postgres.go
@@ -36,6 +36,7 @@ type Client interface {
|
||||
// otherwise returns the pool.
|
||||
GetExecutor(ctx context.Context) Executor
|
||||
Begin(ctx context.Context) (Tx, error)
|
||||
BeginTx(ctx context.Context, opts pgx.TxOptions) (Tx, error)
|
||||
Ping(ctx context.Context) error
|
||||
HandleError(err error) error
|
||||
}
|
||||
@@ -45,6 +46,7 @@ type Component interface {
|
||||
launcher.Component
|
||||
health.Checkable
|
||||
Client
|
||||
Stats() *pgxpool.Stat
|
||||
}
|
||||
|
||||
// UnitOfWork wraps operations in a single database transaction.
|
||||
@@ -159,20 +161,34 @@ func (c *pgComponent) GetExecutor(ctx context.Context) Executor {
|
||||
return pool
|
||||
}
|
||||
|
||||
func (c *pgComponent) Begin(ctx context.Context) (Tx, error) {
|
||||
func (c *pgComponent) BeginTx(ctx context.Context, opts pgx.TxOptions) (Tx, error) {
|
||||
c.mu.RLock()
|
||||
pool := c.pool
|
||||
c.mu.RUnlock()
|
||||
if pool == nil {
|
||||
return nil, fmt.Errorf("postgres: pool not initialized")
|
||||
}
|
||||
tx, err := pool.Begin(ctx)
|
||||
tx, err := pool.BeginTx(ctx, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &pgTx{Tx: tx}, nil
|
||||
}
|
||||
|
||||
func (c *pgComponent) Begin(ctx context.Context) (Tx, error) {
|
||||
return c.BeginTx(ctx, pgx.TxOptions{})
|
||||
}
|
||||
|
||||
func (c *pgComponent) Stats() *pgxpool.Stat {
|
||||
c.mu.RLock()
|
||||
pool := c.pool
|
||||
c.mu.RUnlock()
|
||||
if pool == nil {
|
||||
return &pgxpool.Stat{}
|
||||
}
|
||||
return pool.Stat()
|
||||
}
|
||||
|
||||
func (c *pgComponent) Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error) {
|
||||
c.mu.RLock()
|
||||
pool := c.pool
|
||||
|
||||
Reference in New Issue
Block a user