Skip to content

Add Adminer support for MySql #686

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

Merged
merged 2 commits into from
May 20, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,24 @@ jobs:
name: [
# Hosting integration tests
Hosting.ActiveMQ.Tests,
Hosting.Adminer.Tests,
Hosting.Azure.Dapr.Redis.Tests,
Hosting.Azure.Dapr.Tests,
Hosting.Azure.DataApiBuilder.Tests,
Hosting.Azure.StaticWebApps.Tests,
Hosting.Bun.Tests,
Hosting.Azure.Dapr.Tests,
Hosting.Azure.Dapr.Redis.Tests,
Hosting.Dapr.Tests,
Hosting.DbGate.Tests,
Hosting.Deno.Tests,
Hosting.EventStore.Tests,
Hosting.GoFeatureFlag.Tests,
Hosting.Golang.Tests,
Hosting.Java.Tests,
Hosting.k6.Tests,
Hosting.LavinMQ.Tests,
Hosting.MailPit.Tests,
Hosting.Meilisearch.Tests,
Hosting.MongoDB.Extensions.Tests,
Hosting.MySql.Extensions.Tests,
Hosting.Ngrok.Tests,
Hosting.NodeJS.Extensions.Tests,
Hosting.Ollama.Tests,
Expand All @@ -47,9 +48,9 @@ jobs:
Hosting.Redis.Extensions.Tests,
Hosting.Rust.Tests,
Hosting.SqlDatabaseProjects.Tests,
Hosting.Sqlite.Tests,
Hosting.SqlServer.Extensions.Tests,
Hosting.Adminer.Tests,
Hosting.Sqlite.Tests,
Hosting.k6.Tests,

# Client integration tests
EventStore.Tests,
Expand Down
24 changes: 24 additions & 0 deletions CommunityToolkit.Aspire.sln
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hos
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.Adminer.Tests", "tests\CommunityToolkit.Aspire.Hosting.Adminer.Tests\CommunityToolkit.Aspire.Hosting.Adminer.Tests.csproj", "{4973B296-C644-4737-BBCC-6666C077CEBA}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.MySql.Extensions", "src\CommunityToolkit.Aspire.Hosting.MySql.Extensions\CommunityToolkit.Aspire.Hosting.MySql.Extensions.csproj", "{79707CDB-9334-4E3F-89BE-486DC467F46B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.MySql.Extensions.AppHost", "examples\mysql-ext\CommunityToolkit.Aspire.Hosting.MySql.Extensions.AppHost\CommunityToolkit.Aspire.Hosting.MySql.Extensions.AppHost.csproj", "{DE2AB6D9-383A-45EA-8628-8473641054B3}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "mysql-ext", "mysql-ext", "{11658448-BDD9-4C57-BF02-CEA70CEB8A55}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CommunityToolkit.Aspire.Hosting.MySql.Extensions.Tests", "tests\CommunityToolkit.Aspire.Hosting.MySql.Extensions.Tests\CommunityToolkit.Aspire.Hosting.MySql.Extensions.Tests.csproj", "{D511F986-68DF-4285-A099-A34AA9224FB3}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -999,6 +1007,18 @@ Global
{4973B296-C644-4737-BBCC-6666C077CEBA}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4973B296-C644-4737-BBCC-6666C077CEBA}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4973B296-C644-4737-BBCC-6666C077CEBA}.Release|Any CPU.Build.0 = Release|Any CPU
{79707CDB-9334-4E3F-89BE-486DC467F46B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79707CDB-9334-4E3F-89BE-486DC467F46B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79707CDB-9334-4E3F-89BE-486DC467F46B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79707CDB-9334-4E3F-89BE-486DC467F46B}.Release|Any CPU.Build.0 = Release|Any CPU
{DE2AB6D9-383A-45EA-8628-8473641054B3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DE2AB6D9-383A-45EA-8628-8473641054B3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DE2AB6D9-383A-45EA-8628-8473641054B3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DE2AB6D9-383A-45EA-8628-8473641054B3}.Release|Any CPU.Build.0 = Release|Any CPU
{D511F986-68DF-4285-A099-A34AA9224FB3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D511F986-68DF-4285-A099-A34AA9224FB3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D511F986-68DF-4285-A099-A34AA9224FB3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D511F986-68DF-4285-A099-A34AA9224FB3}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -1190,6 +1210,10 @@ Global
{A62E017D-5474-4CAC-84CC-974755145B52} = {8519CC01-1370-47C8-AD94-B0F326B1563F}
{85E52133-F4CC-45BC-AEC5-7FE19D3817F2} = {A62E017D-5474-4CAC-84CC-974755145B52}
{4973B296-C644-4737-BBCC-6666C077CEBA} = {899F0713-7FC6-4750-BAFC-AC650B35B453}
{79707CDB-9334-4E3F-89BE-486DC467F46B} = {414151D4-7009-4E78-A5C6-D99EBD1E67D1}
{DE2AB6D9-383A-45EA-8628-8473641054B3} = {11658448-BDD9-4C57-BF02-CEA70CEB8A55}
{11658448-BDD9-4C57-BF02-CEA70CEB8A55} = {8519CC01-1370-47C8-AD94-B0F326B1563F}
{D511F986-68DF-4285-A099-A34AA9224FB3} = {899F0713-7FC6-4750-BAFC-AC650B35B453}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {08B1D4B8-D2C5-4A64-BB8B-E1A2B29525F0}
Expand Down
1 change: 1 addition & 0 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<PackageVersion Include="Aspire.Hosting.Rabbitmq" Version="$(AspireVersion)" />
<PackageVersion Include="Aspire.Hosting.Redis" Version="$(AspireVersion)" />
<PackageVersion Include="Aspire.Hosting.MongoDB" Version="$(AspireVersion)" />
<PackageVersion Include="Aspire.Hosting.MySql" Version="$(AspireVersion)" />
<PackageVersion Include="Aspire.Hosting.SqlServer" Version="$(AspireVersion)" />
</ItemGroup>
<ItemGroup Label="Core Packages">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<ProjectReference Include="..\..\..\src\CommunityToolkit.Aspire.Hosting.Adminer\CommunityToolkit.Aspire.Hosting.Adminer.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\..\..\src\CommunityToolkit.Aspire.Hosting.PostgreSQL.Extensions\CommunityToolkit.Aspire.Hosting.PostgreSQL.Extensions.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\..\..\src\CommunityToolkit.Aspire.Hosting.SqlServer.Extensions\CommunityToolkit.Aspire.Hosting.SqlServer.Extensions.csproj" IsAspireProjectResource="false" />
<ProjectReference Include="..\..\..\src\CommunityToolkit.Aspire.Hosting.MySql.Extensions\CommunityToolkit.Aspire.Hosting.MySql.Extensions.csproj" IsAspireProjectResource="false" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,14 @@
sqlserver2.AddDatabase("db7");
sqlserver2.AddDatabase("db8");

var mysql1 = builder.AddMySql("mysql1")
.WithAdminer();
mysql1.AddDatabase("db9");
mysql1.AddDatabase("db10");

var mysql2 = builder.AddMySql("mysql2")
.WithAdminer();
mysql2.AddDatabase("db11");
mysql2.AddDatabase("db12");

builder.Build().Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">
<Sdk Name="Aspire.AppHost.Sdk" Version="$(AspireAppHostSdkVersion)" />

<PropertyGroup>
<OutputType>Exe</OutputType>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsAspireHost>true</IsAspireHost>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting.AppHost" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\..\src\CommunityToolkit.Aspire.Hosting.MySql.Extensions\CommunityToolkit.Aspire.Hosting.MySql.Extensions.csproj" IsAspireProjectResource="false" />
</ItemGroup>


</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
var builder = DistributedApplication.CreateBuilder(args);

var mysql1 = builder.AddMySql("mysql1")
.WithAdminer(c => c.WithHostPort(8989));
mysql1.AddDatabase("db1");
mysql1.AddDatabase("db2");

var mysql2 = builder.AddMySql("mysql2")
.WithAdminer(c => c.WithHostPort(8989));
mysql2.AddDatabase("db3");
mysql2.AddDatabase("db4");

builder.Build().Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"profiles": {
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "https://localhost:17113;http://localhost:15271",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "https://localhost:21203",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "https://localhost:22089"
}
},
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"applicationUrl": "http://localhost:15271",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development",
"DOTNET_ENVIRONMENT": "Development",
"DOTNET_DASHBOARD_OTLP_ENDPOINT_URL": "http://localhost:19015",
"DOTNET_RESOURCE_SERVICE_ENDPOINT_URL": "http://localhost:20243"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning",
"Aspire.Hosting.Dcp": "Warning"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AdditionalPackageTags>hosting mysql dbgate</AdditionalPackageTags>
<Description>A .NET Aspire integration for extending mysql hosting.</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting" />
<PackageReference Include="Aspire.Hosting.MySql" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\CommunityToolkit.Aspire.Hosting.Adminer\CommunityToolkit.Aspire.Hosting.Adminer.csproj" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(SharedDir)\Adminer\AdminerLoginServer.cs" Link="AdminerLoginServer.cs" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using Aspire.Hosting.ApplicationModel;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Aspire.Hosting;

/// <summary>
/// Provides extension methods for adding MySql resources to an <see cref="IDistributedApplicationBuilder"/>.
/// </summary>
public static class MySqlBuilderExtensions
{
/// <summary>
/// Adds an administration and development platform for MySql to the application model using Adminer.
/// </summary>
/// <remarks>
/// This version of the package defaults to the <inheritdoc cref="AdminerContainerImageTags.Tag"/> tag of the <inheritdoc cref="AdminerContainerImageTags.Image"/> container image.
/// <param name="builder">The MySql server resource builder.</param>
/// <param name="configureContainer">Configuration callback for Adminer container resource.</param>
/// <param name="containerName">The name of the container (Optional).</param>
/// <example>
/// Use in application host with a MySql resource
/// <code lang="csharp">
/// var builder = DistributedApplication.CreateBuilder(args);
///
/// var mysql = builder.AddMySql("mysql")
/// .WithAdminer();
/// var db = mysql.AddDatabase("db");
///
/// var api = builder.AddProject&lt;Projects.Api&gt;("api")
/// .WithReference(db);
///
/// builder.Build().Run();
/// </code>
/// </example>
/// </remarks>
/// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
public static IResourceBuilder<MySqlServerResource> WithAdminer(this IResourceBuilder<MySqlServerResource> builder, Action<IResourceBuilder<AdminerContainerResource>>? configureContainer = null, string? containerName = null)
{
ArgumentNullException.ThrowIfNull(builder);

containerName ??= $"{builder.Resource.Name}-adminer";
var adminerBuilder = AdminerBuilderExtensions.AddAdminer(builder.ApplicationBuilder, containerName);

adminerBuilder
.WithEnvironment(context => ConfigureAdminerContainer(context, builder.ApplicationBuilder));

configureContainer?.Invoke(adminerBuilder);

return builder;
}

internal static void ConfigureAdminerContainer(EnvironmentCallbackContext context, IDistributedApplicationBuilder applicationBuilder)
{
var mysqlInstances = applicationBuilder.Resources.OfType<MySqlServerResource>();

string ADMINER_SERVERS = context.EnvironmentVariables.GetValueOrDefault("ADMINER_SERVERS")?.ToString() ?? string.Empty;

var new_servers = mysqlInstances.ToDictionary(
mysqlServer => mysqlServer.Name,
mysqlServer =>
{
return new AdminerLoginServer
{
Server = mysqlServer.Name,
UserName = "root",
Password = mysqlServer.PasswordParameter.Value,
Driver = "server" // driver for MySQL is called 'server'
};
});

if (string.IsNullOrEmpty(ADMINER_SERVERS))
{
string servers_json = JsonSerializer.Serialize(new_servers);
context.EnvironmentVariables["ADMINER_SERVERS"] = servers_json;
}
else
{
var servers = JsonSerializer.Deserialize<Dictionary<string, AdminerLoginServer>>(ADMINER_SERVERS) ?? throw new InvalidOperationException("The servers should not be null. This should never happen.");
foreach (var server in new_servers)
{
if (!servers.ContainsKey(server.Key))
{
servers!.Add(server.Key, server.Value);
}
}
string servers_json = JsonSerializer.Serialize(servers);
context.EnvironmentVariables["ADMINER_SERVERS"] = servers_json;
}

}
}
32 changes: 32 additions & 0 deletions src/CommunityToolkit.Aspire.Hosting.MySql.Extensions/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# CommunityToolkit.Aspire.Hosting.MySql.Extensions library

This integration contains extensions for the [MySql hosting package](https://nuget.org/packages/Aspire.Hosting.MySql) for .NET Aspire.

The integration provides support for running [Adminer](https://github.com/vrana/adminer) to interact with the MySql database.

## Getting Started

### Install the package

In your AppHost project, install the package using the following command:

```dotnetcli
dotnet add package CommunityToolkit.Aspire.Hosting.MySql.Extensions
```

### Example usage

Then, in the _Program.cs_ file of `AppHost`, define an MySql resource, then call `AddMySql`:

```csharp
var mysql = builder.AddMySql("mysql")
.WithAdminer();
```

## Additional Information

https://learn.microsoft.com/dotnet/aspire/community-toolkit/hosting-mysql-extensions

## Feedback & contributing

https://github.com/CommunityToolkit/Aspire
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,17 @@ public async Task WithAdminerShouldAddAnnotationsForMultipleDatabaseTypes()

var sqlserverResource2 = sqlserverResourceBuilder2.Resource;


var mysqlResourceBuilder1 = builder.AddMySql("mysql1")
.WithAdminer();

var mysqlResource1 = mysqlResourceBuilder1.Resource;

var mysqlResourceBuilder2 = builder.AddMySql("mysql2")
.WithAdminer();

var mysqlResource2 = mysqlResourceBuilder2.Resource;

using var app = builder.Build();

var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
Expand Down Expand Up @@ -211,6 +222,26 @@ public async Task WithAdminerShouldAddAnnotationsForMultipleDatabaseTypes()
Password = sqlserverResource2.PasswordParameter.Value,
UserName = "sa"
}
},
{
"mysql1",
new AdminerLoginServer
{
Driver = "server",
Server = mysqlResource1.Name,
Password = mysqlResource1.PasswordParameter.Value,
UserName = "root"
}
},
{
"mysql2",
new AdminerLoginServer
{
Driver = "server",
Server = mysqlResource2.Name,
Password = mysqlResource2.PasswordParameter.Value,
UserName = "root"
}
}
};

Expand All @@ -237,6 +268,13 @@ public void WithAdminerShouldShouldAddOneAdminerResourceForMultipleDatabaseTypes
builder.AddSqlServer("sqlserver2")
.WithAdminer();

builder.AddMySql("mysql1")
.WithAdminer();

builder.AddMySql("mysql2")
.WithAdminer();


using var app = builder.Build();

var appModel = app.Services.GetRequiredService<DistributedApplicationModel>();
Expand Down
Loading
Loading