Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions cmd/util/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"fmt"
"strings"
"time"

_ "github.com/jackc/pgx/v5/stdlib"
"github.com/pgplex/pgschema/internal/logger"
Expand Down Expand Up @@ -42,8 +43,10 @@ func Connect(config *ConnectionConfig) (*sql.DB, error) {
return nil, fmt.Errorf("failed to connect to database: %w", err)
}

// Test the connection
if err := conn.Ping(); err != nil {
// Test the connection with a timeout to fail fast if the database is unreachable
pingCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
Copy link

Copilot AI Feb 16, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ping timeout is set to 30 seconds, but the PR description states it should be 10 seconds. This timeout value should match the PR description to ensure consistency with the stated goal of preventing indefinite hangs.

Copilot uses AI. Check for mistakes.
defer cancel()
if err := conn.PingContext(pingCtx); err != nil {
log.Debug("Database ping failed", "error", err)
conn.Close()
return nil, fmt.Errorf("failed to ping database: %w", err)
Expand Down Expand Up @@ -74,6 +77,10 @@ func buildDSN(config *ConnectionConfig) string {
parts = append(parts, fmt.Sprintf("application_name=%s", config.ApplicationName))
}

// Set connect_timeout to fail fast if the database is unreachable.
// This is a libpq parameter that pgx respects for TCP connection establishment.
parts = append(parts, "connect_timeout=30")

return strings.Join(parts, " ")
}

Expand Down