Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Jan 16, 2026

  • Understand the issue: Environment variables with hyphens are invalid in Linux
  • Create a helper method to sanitize resource names for environment variables (replace hyphens with underscores)
  • Update MySQL DbGate integration to use sanitized connection ID
  • Update PostgreSQL DbGate integration to use sanitized connection ID
  • Update MongoDB DbGate integration to use sanitized connection ID
  • Update Redis DbGate integration to use sanitized connection ID
  • Update SqlServer DbGate integration to use sanitized connection ID
  • Add tests to verify resource names with hyphens work correctly
  • Run tests to verify the changes work (all unit tests pass)
  • Request code review (no issues found)
  • Add unit tests for SanitizeConnectionId method
  • Improve documentation to explain limitations of sanitization
  • Clarify documentation to match actual implementation behavior
  • Further refine documentation based on code review feedback
Original prompt

This section details on the original issue you should resolve

<issue_title>DbGate does not work with MySql hosting integration</issue_title>
<issue_description>### Describe the bug

Pretty vanilla setup:

<PackageVersion Include="Aspire.Hosting.MySql" Version="13.1.0" />
<PackageVersion Include="Aspire.MySqlConnector" Version="13.1.0" />
<PackageVersion Include="CommunityToolkit.Aspire.Hosting.MySql.Extensions" Version="13.1.0" />
var passwordResource = builder.AddParameter("password", "P@ssw0rd");
var database = builder.AddMySql("mysql-db", passwordResource, port: 12562)
    .WithContainerName("mysql-db-volume")
    .WithDbGate()
    .WithAdminer();

DbGate seems to get wrong connection string, here's the console log from DbGate resource:

{"pid":21,"caller":"connections","name":"serverConnection","connections":["mysql-db"],"level":40,"msg":"Invalid CONNECTIONS configutation, missing ENGINE for connection ID","time":1768482135538}

                                {"pid":21,"caller":"srvconnProcess","name":"serverConnection","errorMessage":"Could not get driver from connection {\"_id\":\"***\",\"useDatabaseUrl\":false,\"defaultDatabase\":null,\"singleDatabase\":false,\"databases\":null,\"useSeparateSchemas\":false,\"globalSettings\":\"***\"}","errorObject":{},"errorStack":"Error: Could not get driver from connection {\"_id\":\"***\",\"useDatabaseUrl\":false,\"defaultDatabase\":null,\"singleDatabase\":false,\"databases\":null,\"useSeparateSchemas\":false,\"globalSettings\":\"***\"}\n    at e.exports (/home/dbgate-docker/bundle.js:2:1320315)\n    at connect (/home/dbgate-docker/bundle.js:2:1255295)\n    at /home/dbgate-docker/bundle.js:2:1256270\n    at process.<anonymous> (/home/dbgate-docker/bundle.js:2:1256275)\n    at process.emit (node:events:514:28)\n    at emit (node:internal/child_process:937:14)\n    at process.processTicksAndRejections (node:internal/process/task_queues:83:21)","level":50,"msg":"Error processing message connect","time":1768482135553}

                                {"pid":1,"caller":"useController","name":"main","errorMessage":"Cannot read properties of null (reading 'passwordMode')","errorObject":{},"errorStack":"TypeError: Cannot read properties of null (reading 'passwordMode')\n    at Object.ensureOpened (/home/dbgate-docker/bundle.js:2:1207185)\n    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)\n    at async Object.structure (/home/dbgate-docker/bundle.js:2:1213085)\n    at async /home/dbgate-docker/bundle.js:2:1324687","level":50,"msg":"Error when processing route /database-connections/structure","time":1768482144252}

Adminer with that setup works correctly

Regression

No response

Steps to reproduce

Use Aspire 13.1.0

<PackageVersion Include="Aspire.Hosting.MySql" Version="13.1.0" />
<PackageVersion Include="Aspire.MySqlConnector" Version="13.1.0" />
<PackageVersion Include="CommunityToolkit.Aspire.Hosting.MySql.Extensions" Version="13.1.0" />

and setup mysql with dbgate:

var passwordResource = builder.AddParameter("password", "P@ssw0rd");
var database = builder.AddMySql("mysql-db", passwordResource, port: 12562)
    .WithContainerName("mysql-db-volume")
    .WithDbGate()
    .WithAdminer();

Expected behavior

DbGate is correctly connected to the mysql database

Screenshots

Image

IDE and version

Rider

IDE version

No response

Nuget packages

Aspire.Hosting.MySql Version="13.1.0"
Aspire.MySqlConnector Version="13.1.0"
CommunityToolkit.Aspire.Hosting.MySql.Extensions Version="13.1.0"

Additional context

No response

Help us help you

Yes, but only if others can assist</issue_description>

<agent_instructions>Refer to the most recent comment for the instructions on what needs to be done to fix it</agent_instructions>

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

@aaronpowell Ooo nice catch! It turns out that the problem is because the way we map the resources through as connections is to just copy the resource name to the `CONNECTIONS` environment variable, and then create the other environment variables DbGate requires. The result of this is that the resource `mysql-db` generates an environment variable `ENGINE_mysql-db` which is not a valid environment variable name, since they can't have `-` in them.

The easiest fix to unblock you right now would be to change the resource name to mysql or mysqldb (without the -).

By we should add some resiliency into the integration so that we clean the resource name up (not just for MySQL, but for any resource that supports DbGate) to ensure that the connection ID that is provided is valid in a Linux environment variable.</comment_new>


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 2 commits January 16, 2026 04:00
…ith hyphens

Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com>
Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix DbGate connection issue with MySql hosting integration Fix DbGate integration to handle resource names with hyphens Jan 16, 2026
Copilot AI requested a review from aaronpowell January 16, 2026 04:11
@aaronpowell aaronpowell marked this pull request as ready for review January 16, 2026 04:47
@aaronpowell aaronpowell requested a review from Alirexaa as a code owner January 16, 2026 04:47
Copilot AI review requested due to automatic review settings January 16, 2026 04:47
Copilot AI temporarily deployed to azure-artifacts January 16, 2026 04:56 Inactive
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 a bug where DbGate integration failed when database resources were named with hyphens (e.g., "mysql-db") because hyphens are not valid characters in Linux environment variable names. The fix introduces a SanitizeConnectionId method that replaces hyphens with underscores for environment variable names while preserving the original resource names in display labels.

Changes:

  • Added SanitizeConnectionId method to DbGateBuilderExtensions to sanitize resource names for use in environment variables
  • Updated all DbGate integration implementations (MySQL, PostgreSQL, MongoDB, Redis, SqlServer) to use sanitized connection IDs
  • Added test coverage for MySQL integration with hyphenated resource names
  • Fixed a typo in Redis integration (lalbellabel)

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/CommunityToolkit.Aspire.Hosting.DbGate/DbGateBuilderExtensions.cs Added SanitizeConnectionId public static method to replace hyphens with underscores
src/CommunityToolkit.Aspire.Hosting.MySql.Extensions/MySqlBuilderExtensions.cs Updated DbGate configuration to use sanitized connection IDs
src/CommunityToolkit.Aspire.Hosting.PostgreSQL.Extensions/PostgresBuilderExtensions.cs Updated DbGate configuration to use sanitized connection IDs
src/CommunityToolkit.Aspire.Hosting.MongoDB.Extensions/MongoDBBuilderExtensions.cs Updated DbGate configuration to use sanitized connection IDs
src/CommunityToolkit.Aspire.Hosting.Redis.Extensions/RedisBuilderExtensions.cs Updated DbGate configuration to use sanitized connection IDs and fixed typo lalbellabel
src/CommunityToolkit.Aspire.Hosting.SqlServer.Extensions/SqlServerBuilderExtensions.cs Updated DbGate configuration to use sanitized connection IDs
tests/CommunityToolkit.Aspire.Hosting.MySql.Extensions.Tests/ResourceCreationTests.cs Added test verifying hyphenated resource names generate valid environment variables

Comment on lines +100 to +104
public static string SanitizeConnectionId(string resourceName)
{
ArgumentNullException.ThrowIfNull(resourceName);
return resourceName.Replace('-', '_');
}
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

The SanitizeConnectionId method only replaces hyphens with underscores, but Linux environment variable names have additional constraints beyond just hyphens. Valid environment variable names can only contain letters, numbers, and underscores, and cannot start with a number. Consider validating or sanitizing other invalid characters as well (e.g., dots, spaces, special characters) to prevent future issues. At minimum, add a comment explaining that only hyphens are currently addressed.

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.

Updated the documentation to explain that only hyphens are currently addressed and noted the limitations. The remarks section now clarifies that other invalid characters (dots, spaces, special characters) are not handled and resource names should follow standard naming conventions. (commit 46c1820)

/// Environment variable names in Linux cannot contain hyphens, so they are replaced with underscores.
/// This ensures that resource names like "mysql-db" become "mysql_db" when used in environment variable names.
/// </remarks>
public static string SanitizeConnectionId(string resourceName)
Copy link

Copilot AI Jan 16, 2026

Choose a reason for hiding this comment

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

The new SanitizeConnectionId method lacks direct unit tests. While integration tests verify the sanitization works end-to-end in MySQL, there should be unit tests specifically for this method to cover edge cases like multiple hyphens, leading/trailing hyphens, empty strings (if allowed), and strings without hyphens. Consider adding tests to tests/CommunityToolkit.Aspire.Hosting.DbGate.Tests/DbGatePublicApiTests.cs.

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.

Added comprehensive unit tests for SanitizeConnectionId to DbGatePublicApiTests.cs, covering edge cases including multiple hyphens, leading/trailing hyphens, empty strings, and strings without hyphens. All tests pass. (commit 46c1820)

@aaronpowell
Copy link
Member

@copilot can you action the two comments

Copilot AI and others added 3 commits January 19, 2026 01:36
Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com>
Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com>
Co-authored-by: aaronpowell <434140+aaronpowell@users.noreply.github.com>
@github-actions
Copy link
Contributor

Code Coverage

Package Line Rate Branch Rate Complexity Health
CommunityToolkit.Aspire.GoFeatureFlag 100% 97% 74
CommunityToolkit.Aspire.Hosting.ActiveMQ 78% 40% 108
CommunityToolkit.Aspire.Hosting.ActiveMQ.MassTransit 1% 0% 14
CommunityToolkit.Aspire.Hosting.Adminer 74% 50% 20
CommunityToolkit.Aspire.Hosting.Azure.Dapr 29% 7% 124
CommunityToolkit.Aspire.Hosting.Azure.Dapr.Redis 62% 38% 80
CommunityToolkit.Aspire.Hosting.Azure.DataApiBuilder 100% 100% 22
CommunityToolkit.Aspire.Hosting.Azure.Extensions 64% 30% 54
CommunityToolkit.Aspire.Hosting.Bun 96% 83% 28
CommunityToolkit.Aspire.Hosting.Dapr 60% 37% 856
CommunityToolkit.Aspire.Hosting.DbGate 95% 50% 20
CommunityToolkit.Aspire.Hosting.Deno 98% 85% 44
CommunityToolkit.Aspire.Hosting.Elasticsearch.Extensions 99% 93% 46
CommunityToolkit.Aspire.Hosting.Flagd 79% 100% 32
CommunityToolkit.Aspire.Hosting.Flyway 94% 100% 8
CommunityToolkit.Aspire.Hosting.GoFeatureFlag 88% 73% 36
CommunityToolkit.Aspire.Hosting.Golang 60% 48% 120
CommunityToolkit.Aspire.Hosting.Java 70% 75% 130
CommunityToolkit.Aspire.Hosting.JavaScript.Extensions 97% 85% 200
CommunityToolkit.Aspire.Hosting.k6 58% 12% 20
CommunityToolkit.Aspire.Hosting.Keycloak.Extensions 100% 100% 22
CommunityToolkit.Aspire.Hosting.KurrentDB 71% 75% 34
CommunityToolkit.Aspire.Hosting.LavinMQ 74% 50% 26
CommunityToolkit.Aspire.Hosting.LavinMQ.MassTransit 1% 0% 14
CommunityToolkit.Aspire.Hosting.MailPit 85% 50% 22
CommunityToolkit.Aspire.Hosting.McpInspector 78% 45% 210
CommunityToolkit.Aspire.Hosting.Meilisearch 71% 57% 58
CommunityToolkit.Aspire.Hosting.Minio 88% 75% 56
CommunityToolkit.Aspire.Hosting.MongoDB.Extensions 95% 90% 20
CommunityToolkit.Aspire.Hosting.MySql.Extensions 98% 92% 62
CommunityToolkit.Aspire.Hosting.Ngrok 52% 35% 82
CommunityToolkit.Aspire.Hosting.Ollama 65% 69% 260
CommunityToolkit.Aspire.Hosting.OpenTelemetryCollector 78% 61% 77
CommunityToolkit.Aspire.Hosting.PapercutSmtp 81% 50% 18
CommunityToolkit.Aspire.Hosting.PostgreSQL.Extensions 98% 90% 74
CommunityToolkit.Aspire.Hosting.Python.Extensions 45% 29% 100
CommunityToolkit.Aspire.Hosting.RavenDB 56% 45% 184
CommunityToolkit.Aspire.Hosting.Redis.Extensions 100% 71% 28
CommunityToolkit.Aspire.Hosting.Rust 94% 83% 16
CommunityToolkit.Aspire.Hosting.Sftp 59% 32% 40
CommunityToolkit.Aspire.Hosting.Solr 72% 100% 22
CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects 51% 48% 192
CommunityToolkit.Aspire.Hosting.Sqlite 89% 89% 52
CommunityToolkit.Aspire.Hosting.SqlServer.Extensions 98% 90% 62
CommunityToolkit.Aspire.Hosting.Stripe 51% 30% 82
CommunityToolkit.Aspire.Hosting.SurrealDb 54% 38% 256
CommunityToolkit.Aspire.Hosting.Umami 71% 75% 16
CommunityToolkit.Aspire.KurrentDB 94% 92% 54
CommunityToolkit.Aspire.MassTransit.RabbitMQ 100% 100% 30
CommunityToolkit.Aspire.Meilisearch 97% 92% 68
CommunityToolkit.Aspire.Microsoft.Data.Sqlite 89% 85% 52
CommunityToolkit.Aspire.Microsoft.EntityFrameworkCore.Sqlite 61% 58% 114
CommunityToolkit.Aspire.Minio.Client 90% 85% 112
CommunityToolkit.Aspire.OllamaSharp 78% 71% 134
CommunityToolkit.Aspire.RavenDB.Client 60% 53% 237
CommunityToolkit.Aspire.Sftp 86% 78% 86
CommunityToolkit.Aspire.SurrealDb 79% 63% 78
Summary 68% (8039 / 11872) 55% (2181 / 3954) 5086

Minimum allowed line rate is 60%

Copilot AI temporarily deployed to azure-artifacts January 19, 2026 23:16 Inactive
@github-actions github-actions bot added the Stale label Jan 25, 2026
@github-actions github-actions bot closed this Jan 28, 2026
@aaronpowell aaronpowell reopened this Jan 29, 2026
@aaronpowell aaronpowell merged commit 115cf28 into main Jan 29, 2026
236 of 238 checks passed
@aaronpowell aaronpowell deleted the copilot/fix-dbgate-mysql-connection branch January 29, 2026 02:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

DbGate does not work with MySql hosting integration

2 participants