-
Notifications
You must be signed in to change notification settings - Fork 301
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.
| 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; | |
| } |
Why make this change?
PostgreSQL health checks fail with
Keyword not supported: 'host'error. Direct assignment of connection strings toDbConnection.ConnectionStringbypasses driver-specific parsing, causing Npgsql to reject lowercase keywords likehost,port,database.What is this change?
Normalize connection strings through database-specific builders before assignment:
Utilities.NormalizeConnectionString(): Routes toNpgsqlConnectionStringBuilderfor PostgreSQL,SqlConnectionStringBuilderfor MSSQL/DWSQL, passthrough for othersHttpUtilities.ExecuteDbQueryAsync(): AcceptsDatabaseTypeparameter, applies normalization before connection initializationHealthCheckHelper.ExecuteDatasourceQueryCheckAsync(): ThreadsDatabaseTypethrough call chainThis mirrors the pattern used in
PostgreSqlExecutor,MsSqlQueryExecutor, and throughout the codebase for managed connection handling.How was this tested?
Sample Request(s)
Original prompt
data-source#2796💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.