From bf7a11076fc465ce82542faa9dbe7997b56f7f66 Mon Sep 17 00:00:00 2001 From: Masahiko AMANO Date: Thu, 9 Oct 2025 21:01:38 +0300 Subject: [PATCH] fix(backend/infrastructure/persistence/postgres): add testing connection to db and improve error messages --- .../internal/infrastructure/persistence/postgres/db.go | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/backend/internal/infrastructure/persistence/postgres/db.go b/backend/internal/infrastructure/persistence/postgres/db.go index 4103121..2cc4be4 100644 --- a/backend/internal/infrastructure/persistence/postgres/db.go +++ b/backend/internal/infrastructure/persistence/postgres/db.go @@ -15,7 +15,7 @@ import ( func New(dbURL string) (*pgxpool.Pool, error) { poolConfig, err := pgxpool.ParseConfig(dbURL) if err != nil { - return nil, fmt.Errorf("error while parsing connection string: %w", err) + return nil, fmt.Errorf("failed to parse connection string: %w", err) } poolConfig.MaxConns = 100 @@ -23,9 +23,13 @@ func New(dbURL string) (*pgxpool.Pool, error) { poolConfig.MaxConnLifetime = time.Hour poolConfig.HealthCheckPeriod = 30 * time.Second - db, err := pgxpool.NewWithConfig(context.Background(), poolConfig) + ctx := context.Background() + db, err := pgxpool.NewWithConfig(ctx, poolConfig) if err != nil { - return nil, fmt.Errorf("error while initializing DB connections pool: %w", err) + return nil, fmt.Errorf("failed to initialize DB connections pool: %w", err) + } + if err = db.Ping(ctx); err != nil { + return nil, fmt.Errorf("failed to ping database: %w", err) } return db, nil }