-
Notifications
You must be signed in to change notification settings - Fork 304
Fix PostgreSQL health check connection string parsing #3083
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: JerryNixon <1749983+JerryNixon@users.noreply.github.com>
There was a problem hiding this 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 acceptDatabaseTypeparameter and apply connection string normalization - Threaded
DatabaseTypethroughHealthCheckHelper.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.
src/Service/HealthCheck/Utilities.cs
Outdated
| switch (dbType) | ||
| { | ||
| case DatabaseType.PostgreSQL: | ||
| return new NpgsqlConnectionStringBuilder(connectionString).ToString(); | ||
| case DatabaseType.MSSQL: | ||
| case DatabaseType.DWSQL: | ||
| return new SqlConnectionStringBuilder(connectionString).ToString(); | ||
| default: | ||
| return connectionString; | ||
| } |
Copilot
AI
Jan 27, 2026
There was a problem hiding this comment.
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.
| 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; | |
| } |
There was a problem hiding this comment.
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.
|
@copilot apply changes based on the comments in this thread |
Co-authored-by: JerryNixon <1749983+JerryNixon@users.noreply.github.com>
Changes applied in commit d858f2d. Added exception handling to the |
|
/azp run |
|
Azure Pipelines successfully started running 6 pipeline(s). |
|
/azp run |
|
Azure Pipelines successfully started running 6 pipeline(s). |
souvikghosh04
left a comment
There was a problem hiding this 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; | ||
| } |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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; | ||
| } |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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>
Added comprehensive unit test coverage in commit d94b703. Created
All tests pass successfully (9/9). |
Fix PostgreSQL health check connection string handling
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):
MySqlConnectionStringBuilderILoggerparameter for logging exceptionsReview Feedback Addressed
Security Summary
Original prompt
data-source#2796💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.