fix(backend/infrastructure/persistence/postgres): add testing connection to db and improve error messages

This commit is contained in:
Masahiko AMANO 2025-10-09 21:01:38 +03:00
parent c7176fadf6
commit bf7a11076f

View File

@ -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
}