Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 27, 2026

Why make this change?

PostgreSQL health checks fail with Keyword not supported: 'host' error. Direct assignment of connection strings to DbConnection.ConnectionString bypasses driver-specific parsing, causing Npgsql to reject lowercase keywords like host, port, database.

What is this change?

Normalize connection strings through database-specific builders before assignment:

  • Added Utilities.NormalizeConnectionString(): Routes to NpgsqlConnectionStringBuilder for PostgreSQL, SqlConnectionStringBuilder for MSSQL/DWSQL, passthrough for others
  • Updated HttpUtilities.ExecuteDbQueryAsync(): Accepts DatabaseType parameter, applies normalization before connection initialization
  • Updated HealthCheckHelper.ExecuteDatasourceQueryCheckAsync(): Threads DatabaseType through call chain

This mirrors the pattern used in PostgreSqlExecutor, MsSqlQueryExecutor, and throughout the codebase for managed connection handling.

How was this tested?

  • Integration Tests (existing health endpoint tests)
  • Unit Tests

Sample Request(s)

GET /health HTTP/1.1
Host: localhost:5000

# Returns comprehensive health report with datasource check
# Previously failed with connection string parsing error for PostgreSQL
Original prompt

This section details on the original issue you should resolve

<issue_title>[Bug]: Health check fails to check postgres data-source</issue_title>
<issue_description>## What

When the health check attempts to check a Postgres data-source, it gets an error.

Image

Image</issue_description>

<agent_instructions>Fix this with as few lines of new code as possible.</agent_instructions>

Comments on the Issue (you are @copilot in this section)

@JerryNixon # Solution

https://youtu.be/Js06lpu_YsM @ 5:00 time

#2797</comment_new>


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Co-authored-by: JerryNixon <1749983+JerryNixon@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix health check error for postgres data-source Fix PostgreSQL health check connection string parsing Jan 27, 2026
Copilot AI requested a review from JerryNixon January 27, 2026 16:59
@JerryNixon JerryNixon marked this pull request as ready for review January 27, 2026 20:12
Copilot AI review requested due to automatic review settings January 27, 2026 20:12
@JerryNixon JerryNixon added pgsql an issue thats specific to pgsql cri Customer Reported issue labels Jan 27, 2026
@JerryNixon JerryNixon moved this from Todo to Review In Progress in Data API builder Jan 27, 2026
@JerryNixon JerryNixon added this to the Feb 2026 milestone Jan 27, 2026
@JerryNixon JerryNixon moved this from Review In Progress to In Progress in Data API builder Jan 27, 2026
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes PostgreSQL health check failures caused by connection string parsing errors. The issue occurred when direct assignment to DbConnection.ConnectionString bypassed Npgsql's parser, causing it to reject lowercase connection string keywords like host, port, and database.

Changes:

  • Added Utilities.NormalizeConnectionString() method to route connection strings through database-specific builders (NpgsqlConnectionStringBuilder for PostgreSQL, SqlConnectionStringBuilder for MSSQL/DWSQL)
  • Updated HttpUtilities.ExecuteDbQueryAsync() to accept DatabaseType parameter and apply connection string normalization
  • Threaded DatabaseType through HealthCheckHelper.ExecuteDatasourceQueryCheckAsync() to enable normalization

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/Service/HealthCheck/Utilities.cs Added NormalizeConnectionString method to parse and normalize connection strings using database-specific builders
src/Service/HealthCheck/HttpUtilities.cs Updated ExecuteDbQueryAsync to accept DatabaseType parameter and apply connection string normalization before creating database connection
src/Service/HealthCheck/HealthCheckHelper.cs Updated ExecuteDatasourceQueryCheckAsync to pass DatabaseType parameter through to HttpUtilities for connection string normalization

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +75 to +84
switch (dbType)
{
case DatabaseType.PostgreSQL:
return new NpgsqlConnectionStringBuilder(connectionString).ToString();
case DatabaseType.MSSQL:
case DatabaseType.DWSQL:
return new SqlConnectionStringBuilder(connectionString).ToString();
default:
return connectionString;
}
Copy link

Copilot AI Jan 27, 2026

Choose a reason for hiding this comment

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

The NormalizeConnectionString method should handle exceptions from the connection string builders. Throughout the codebase (e.g., PostgreSqlMetadataProvider.TryGetSchemaFromConnectionString at src/Core/Services/MetadataProviders/PostgreSqlMetadataProvider.cs:47-58, RuntimeConfigLoader.GetPgSqlConnectionStringWithApplicationName at src/Config/RuntimeConfigLoader.cs:416-427), connection string builder instantiation is wrapped in try-catch blocks to handle potential parsing errors. If a malformed connection string is passed here, the exception will propagate up and may not be handled appropriately in the health check context. Consider wrapping the builder instantiation in a try-catch block and either logging the error and returning the original connection string, or allowing the exception to propagate with proper context.

Suggested change
switch (dbType)
{
case DatabaseType.PostgreSQL:
return new NpgsqlConnectionStringBuilder(connectionString).ToString();
case DatabaseType.MSSQL:
case DatabaseType.DWSQL:
return new SqlConnectionStringBuilder(connectionString).ToString();
default:
return connectionString;
}
try
{
switch (dbType)
{
case DatabaseType.PostgreSQL:
return new NpgsqlConnectionStringBuilder(connectionString).ToString();
case DatabaseType.MSSQL:
case DatabaseType.DWSQL:
return new SqlConnectionStringBuilder(connectionString).ToString();
default:
return connectionString;
}
}
catch (Exception)
{
// If the connection string cannot be parsed by the builder,
// return the original string to avoid failing the health check.
return connectionString;
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cri Customer Reported issue pgsql an issue thats specific to pgsql

Projects

Status: In Progress

Development

Successfully merging this pull request may close these issues.

[Bug]: Health check fails to check postgres data-source

3 participants