Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 27, 2026

Fix PostgreSQL health check connection string handling

  • Understand the issue: Health check fails for PostgreSQL with "Keyword not supported: 'host'" error
  • Identify root cause: Connection string not properly parsed through NpgsqlConnectionStringBuilder
  • Update ExecuteDbQueryAsync to use connection string builder for PostgreSQL, MSSQL, and MySQL
  • Add NormalizeConnectionString helper method in Utilities.cs
  • Add exception handling to NormalizeConnectionString to handle malformed connection strings
  • Add MySQL connection string normalization support
  • Add logging for connection string parsing failures
  • Create comprehensive unit tests (9 test cases covering all database types)
  • Build succeeds with no errors
  • All unit tests pass (9/9)
  • Code review feedback addressed
  • Run CodeQL security check (0 vulnerabilities found)
  • Final verification complete

Summary

This PR fixes the health check endpoint failure for PostgreSQL data sources and adds comprehensive support for all database types.

Changes (4 files, 204 lines added, 3 lines modified):

  1. Utilities.cs:
    • Added MySQL connection string normalization support using MySqlConnectionStringBuilder
    • Added optional ILogger parameter for logging exceptions
    • Logs warnings when connection string parsing fails
  2. HttpUtilities.cs: Updated to pass logger to NormalizeConnectionString
  3. HealthCheckHelper.cs: Updated to pass DatabaseType parameter through the call chain
  4. HealthCheckUtilitiesUnitTests.cs (NEW): Added 9 comprehensive unit tests covering:
    • PostgreSQL normalization (including lowercase keywords bug fix)
    • MSSQL normalization
    • DWSQL normalization
    • MySQL normalization
    • Unsupported database types
    • Malformed connection strings with and without logger
    • Empty connection strings

Review Feedback Addressed

  • ✅ Added MySQL connection string normalization for completeness
  • ✅ Added logging instead of silently swallowing exceptions
  • ✅ Added comprehensive unit test coverage (9 tests, all passing)

Security Summary

  • No security vulnerabilities detected by CodeQL
  • Changes follow existing patterns in the codebase
  • Exception handling prevents health check failures from malformed connection strings
  • Logging provides visibility into connection string parsing issues without exposing sensitive data
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.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Applied the suggested exception handling in commit d858f2d. The NormalizeConnectionString method now wraps the connection string builder instantiation in a try-catch block and returns the original connection string if parsing fails, preventing the health check from failing due to malformed connection strings.

@JerryNixon
Copy link
Contributor

@copilot apply changes based on the comments in this thread

Co-authored-by: JerryNixon <1749983+JerryNixon@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jan 30, 2026

@copilot apply changes based on the comments in this thread

Changes applied in commit d858f2d. Added exception handling to the NormalizeConnectionString method to gracefully handle malformed connection strings.

@Aniruddh25
Copy link
Collaborator

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 6 pipeline(s).

@souvikghosh04
Copy link
Contributor

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 6 pipeline(s).

Copy link
Contributor

@souvikghosh04 souvikghosh04 left a comment

Choose a reason for hiding this comment

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

@copilot please address the review comments from my side. also add some unit test coverage.

return new SqlConnectionStringBuilder(connectionString).ToString();
default:
return connectionString;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot for sake of completeness, should you also normalize MySQL connection string?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added MySQL connection string normalization in commit d94b703. The method now uses MySqlConnectionStringBuilder for MySQL databases, consistent with the pattern used for PostgreSQL and MSSQL.

// If the connection string cannot be parsed by the builder,
// return the original string to avoid failing the health check.
return connectionString;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

@copilot at least log the exception or throw them instead of swallowing silently

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added logging in commit d94b703. The method now accepts an optional ILogger parameter and logs warnings when connection string parsing fails. The logger is passed from HttpUtilities where it's already available, providing visibility into parsing failures without exposing sensitive data in the connection string.

Co-authored-by: souvikghosh04 <210500244+souvikghosh04@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Feb 11, 2026

@copilot please address the review comments from my side. also add some unit test coverage.

Added comprehensive unit test coverage in commit d94b703. Created HealthCheckUtilitiesUnitTests.cs with 9 test cases covering:

  • PostgreSQL, MSSQL, DWSQL, and MySQL normalization
  • PostgreSQL lowercase keywords (the specific bug reported)
  • Unsupported database types
  • Malformed connection strings with/without logger
  • Empty connection strings

All tests pass successfully (9/9).

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

4 participants