From 3f33b1dc53688c61ecce3cd33cd14bcb6b7bc608 Mon Sep 17 00:00:00 2001 From: shimonp21 Date: Wed, 2 Mar 2022 09:37:07 +0200 Subject: [PATCH] fix: Validate DB connection with an explicit timeout, rather than the default timeout - The default timeout is many minutes, way too long to give the user feedback about failed db connection. - Note that cq-provider-sdk/database/postgres uses lazy-connect, so it won't return an error in case of failed DB connection. --- pkg/client/database/postgres/executor.go | 13 +++++++++++++ pkg/client/database/timescale/timescale.go | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/pkg/client/database/postgres/executor.go b/pkg/client/database/postgres/executor.go index c4a8474c74a365..36dec4d8c4698a 100644 --- a/pkg/client/database/postgres/executor.go +++ b/pkg/client/database/postgres/executor.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "strings" + "time" sdkpg "github.com/cloudquery/cq-provider-sdk/database/postgres" "github.com/hashicorp/go-hclog" @@ -36,6 +37,10 @@ func (e Executor) Validate(ctx context.Context) (bool, error) { return false, err } + if err := ValidatePostgresConnection(ctx, pool); err != nil { + return false, err + } + if err := ValidatePostgresVersion(ctx, pool, MinPostgresVersion); err != nil { return true, err } @@ -51,6 +56,14 @@ func (e Executor) Finalize(_ context.Context, err error) error { return err } +// ValidatePostgresConnection validates that we can actually connect to the postgres database. +func ValidatePostgresConnection(ctx context.Context, pool *pgxpool.Pool) error { + ctx, cancel := context.WithTimeout(ctx, time.Second*10) + defer cancel() + + return pool.Ping(ctx) +} + // queryRower helps with unit tests type queryRower interface { QueryRow(ctx context.Context, sql string, args ...interface{}) pgx.Row diff --git a/pkg/client/database/timescale/timescale.go b/pkg/client/database/timescale/timescale.go index 41f8657c9a312b..962a849177e0e3 100644 --- a/pkg/client/database/timescale/timescale.go +++ b/pkg/client/database/timescale/timescale.go @@ -61,6 +61,10 @@ func (e Executor) Validate(ctx context.Context) (bool, error) { } defer pool.Close() + if err := postgres.ValidatePostgresConnection(ctx, pool); err != nil { + return false, err + } + if err := postgres.ValidatePostgresVersion(ctx, pool, postgres.MinPostgresVersion); err != nil { return false, err }