Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4e7e0de

Browse files
authored
WithPgAdmin - Respect UserName Parameter (#5805)
* WithPgAdmin - Respect UserName Parameter * tests
1 parent 230dd3b commit 4e7e0de

File tree

2 files changed

+6
-5
lines changed

2 files changed

+6
-5
lines changed

src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
using System.Text.Json;
54
using System.Text;
5+
using System.Text.Json;
66
using Aspire.Hosting.ApplicationModel;
77
using Aspire.Hosting.Postgres;
88
using Aspire.Hosting.Utils;
@@ -69,7 +69,7 @@ public static IResourceBuilder<PostgresServerResource> AddPostgres(this IDistrib
6969
//
7070
// https://github.com/npgsql/npgsql/blob/c3b31c393de66a4b03fba0d45708d46a2acb06d2/src/Npgsql/NpgsqlConnection.cs#L445
7171
//
72-
connection.ConnectionString = connection.ConnectionString + ";Database=postgres;";
72+
connection.ConnectionString += ";Database=postgres;";
7373
});
7474

7575
return builder.AddResource(postgresServer)
@@ -183,7 +183,7 @@ public static IResourceBuilder<T> WithPgAdmin<T>(this IResourceBuilder<T> builde
183183
// This will need to be refactored once updated service discovery APIs are available
184184
writer.WriteString("Host", endpoint.Resource.Name);
185185
writer.WriteNumber("Port", (int)endpoint.TargetPort!);
186-
writer.WriteString("Username", "postgres");
186+
writer.WriteString("Username", postgresInstance.UserNameParameter?.Value ?? "postgres");
187187
writer.WriteString("SSLMode", "prefer");
188188
writer.WriteString("MaintenanceDB", "postgres");
189189
writer.WriteString("PasswordExecCommand", $"echo '{postgresInstance.PasswordParameter.Value}'"); // HACK: Generating a pass file and playing around with chmod is too painful.

tests/Aspire.Hosting.PostgreSQL.Tests/AddPostgresTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -452,8 +452,9 @@ public void WithPostgresTwiceEndsUpWithOneContainer()
452452
public async Task WithPostgresProducesValidServersJsonFile()
453453
{
454454
var builder = DistributedApplication.CreateBuilder();
455+
var username = builder.AddParameter("pg-user", "myuser");
455456
var pg1 = builder.AddPostgres("mypostgres1").WithPgAdmin(pga => pga.WithHostPort(8081));
456-
var pg2 = builder.AddPostgres("mypostgres2").WithPgAdmin(pga => pga.WithHostPort(8081));
457+
var pg2 = builder.AddPostgres("mypostgres2", username).WithPgAdmin(pga => pga.WithHostPort(8081));
457458

458459
// Add fake allocated endpoints.
459460
pg1.WithEndpoint("tcp", e => e.AllocatedEndpoint = new AllocatedEndpoint(e, "localhost", 5001));
@@ -486,7 +487,7 @@ public async Task WithPostgresProducesValidServersJsonFile()
486487
Assert.Equal("Servers", servers.GetProperty("2").GetProperty("Group").GetString());
487488
Assert.Equal("mypostgres2", servers.GetProperty("2").GetProperty("Host").GetString());
488489
Assert.Equal(5432, servers.GetProperty("2").GetProperty("Port").GetInt32());
489-
Assert.Equal("postgres", servers.GetProperty("2").GetProperty("Username").GetString());
490+
Assert.Equal("myuser", servers.GetProperty("2").GetProperty("Username").GetString());
490491
Assert.Equal("prefer", servers.GetProperty("2").GetProperty("SSLMode").GetString());
491492
Assert.Equal("postgres", servers.GetProperty("2").GetProperty("MaintenanceDB").GetString());
492493
Assert.Equal($"echo '{pg2.Resource.PasswordParameter.Value}'", servers.GetProperty("2").GetProperty("PasswordExecCommand").GetString());

0 commit comments

Comments
 (0)