Учесть замечания ревью: читать timestamptz в UTC, встроить списки колонок, не требовать каноническую PHC-строку, уточнить замеры отказов

This commit is contained in:
2026-10-21 15:08:17 +03:00
parent 956b84f2d7
commit 7aa86d36ba
7 changed files with 37 additions and 23 deletions
+16 -3
View File
@@ -14,7 +14,9 @@ import (
"strconv"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"github.com/jackc/pgx/v5/pgtype"
"github.com/jackc/pgx/v5/pgxpool"
"github.com/jackc/pgx/v5/stdlib"
"github.com/pressly/goose/v3"
@@ -40,9 +42,10 @@ func NewPool(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
}
cfg.MaxConns = maxConns
cfg.ConnConfig.ConnectTimeout = connectTimeout
// pgconn applies ConnectTimeout to dialling but resolves the host name
// before that with the caller's context. Docker DNS takes seconds to give
// up on a stopped container, so the lookup gets the same bound.
// Unlike net.Dialer, pgx resolves the host before ConnectTimeout starts.
// Docker DNS takes seconds to give up on a stopped container: unbounded,
// every request would wait out its deadline and log only "context
// deadline exceeded" instead of the failed lookup.
cfg.ConnConfig.LookupFunc = func(ctx context.Context, host string) ([]string, error) {
ctx, cancel := context.WithTimeout(ctx, connectTimeout)
defer cancel()
@@ -50,6 +53,16 @@ func NewPool(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
}
cfg.ConnConfig.RuntimeParams["statement_timeout"] = strconv.FormatInt(statementTimeout.Milliseconds(), 10)
cfg.ConnConfig.RuntimeParams["idle_in_transaction_session_timeout"] = strconv.FormatInt(idleInTxTimeout.Milliseconds(), 10)
// The application keeps every time in UTC; pgx would otherwise scan
// timestamptz into time.Local.
cfg.AfterConnect = func(_ context.Context, conn *pgx.Conn) error {
conn.TypeMap().RegisterType(&pgtype.Type{
Name: "timestamptz",
OID: pgtype.TimestamptzOID,
Codec: &pgtype.TimestamptzCodec{ScanLocation: time.UTC},
})
return nil
}
pool, err := pgxpool.NewWithConfig(ctx, cfg)
if err != nil {
@@ -31,6 +31,16 @@ func TestMain(m *testing.M) {
os.Exit(code)
}
func TestPoolScansTimestamptzInUTC(t *testing.T) {
var got time.Time
if err := db.Pool.QueryRow(context.Background(), `SELECT now()`).Scan(&got); err != nil {
t.Fatal(err)
}
if got.Location() != time.UTC {
t.Fatalf("timestamptz scanned in %v, want UTC", got.Location())
}
}
func TestMigrateIsRepeatable(t *testing.T) {
ctx := context.Background()
applied, err := postgres.Migrate(ctx, db.Pool)