fix(cli): fix flakes related to context cancellation when establishing pg connections (#18246)

Since https://github.com/coder/coder/pull/18195 was merged, we started
running CLI tests with postgres instead of just dbmem. This surfaced
errors related to context cancellation while establishing postgres
connections.

This PR should fix https://github.com/coder/internal/issues/672. Related
to https://github.com/coder/coder/issues/15109.
This commit is contained in:
Hugo Dutka
2025-06-05 15:54:13 +02:00
committed by GitHub
parent b5fd3dd855
commit 623dcd97dc
3 changed files with 17 additions and 9 deletions

View File

@ -2359,6 +2359,10 @@ func ConnectToPostgres(ctx context.Context, logger slog.Logger, driver string, d
if err != nil {
return nil, xerrors.Errorf("get postgres version: %w", err)
}
defer version.Close()
if version.Err() != nil {
return nil, xerrors.Errorf("version select: %w", version.Err())
}
if !version.Next() {
return nil, xerrors.Errorf("no rows returned for version select")
}
@ -2367,7 +2371,6 @@ func ConnectToPostgres(ctx context.Context, logger slog.Logger, driver string, d
if err != nil {
return nil, xerrors.Errorf("scan version: %w", err)
}
_ = version.Close()
if versionNum < 130000 {
return nil, xerrors.Errorf("PostgreSQL version must be v13.0.0 or higher! Got: %d", versionNum)