Skip to content

Conversation

@sebastienros
Copy link
Member

Description

Support database creation for AddDatabase() in Postgres container resource. If the database already exists nothing is done. Custom scripts can be provided.

Updated PgAdmin container version.

Part of #7101

Checklist

  • Is this feature complete?
    • Yes. Ready to ship.
    • No. Follow-up changes expected.
  • Are you including unit tests for the changes and scenario tests if relevant?
    • Yes
    • No
  • Did you add public API?
    • Yes
      • If yes, did you have an API Review for it?
        • Yes
        • No
      • Did you add <remarks /> and <code /> elements on your triple slash comments?
        • Yes
        • No
    • No
  • Does the change make any security assumptions or guarantees?
    • Yes
      • If yes, have you done a threat model and had a security review?
        • Yes
        • No
    • No
  • Does the change require an update in our Aspire docs?

Copilot AI review requested due to automatic review settings March 15, 2025 00:02
@github-actions github-actions bot added the area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication label Mar 15, 2025
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 adds support for database creation in the Postgres container resource, including the ability to run a custom SQL creation script. Key changes include updating test cases to validate database creation scenarios, enhancing the builder extension logic for Postgres and database resources, and updating container image tags for PgAdmin.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/Aspire.Hosting.PostgreSQL.Tests/PostgresFunctionalTests.cs Updated test cases removing redundant environment settings and adding new scenarios for custom scripts, special names, resiliency, and multi-database creation.
src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs Refactored the AddPostgres and AddDatabase logic to handle connection string subscriptions and database creation through a dedicated CreateDatabaseAsync method.
src/Aspire.Hosting.PostgreSQL/PostgresDatabaseResource.cs Revised the ConnectionStringExpression to use a DbConnectionStringBuilder for constructing the connection string.
src/Aspire.Hosting.PostgreSQL/PostgresContainerImageTags.cs Updated the PgAdmin container image tag to a newer version.
playground/PostgresEndToEnd/PostgresEndToEnd.AppHost/Program.cs Adjusted the pgAdmin builder usage by removing an explicit image tag override.
Comments suppressed due to low confidence (3)

src/Aspire.Hosting.PostgreSQL/PostgresDatabaseResource.cs:36

  • Ensure that the connection string constructed via DbConnectionStringBuilder.ToString() produces the expected format with all required parameters for consumers. Validate that no essential connection properties are omitted.
return ReferenceExpression.Create($"{Parent};{connectionStringBuilder.ToString()}");

src/Aspire.Hosting.PostgreSQL/PostgresBuilderExtensions.cs:589

  • When executing a user-provided creation script, consider propagating the exception after logging instead of only logging the error, to avoid silent failures in database creation.
command.CommandText = scriptAnnotation?.Script;

playground/PostgresEndToEnd/PostgresEndToEnd.AppHost/Program.cs:16

  • [nitpick] Removing the explicit call to WithImageTag may lead to an unintended pgAdmin image version being deployed; verify that the desired image version is correctly set via configuration or defaults to ensure compatibility.
var pg6 = builder.AddPostgres("pg6").WithPgAdmin(c => c.WithHostPort(8999)).PublishAsContainer();

command.CommandText = scriptAnnotation?.Script;
await command.ExecuteNonQueryAsync(cancellationToken).ConfigureAwait(false);
}
catch (Exception e)
Copy link
Member

@davidfowl davidfowl Mar 15, 2025

Choose a reason for hiding this comment

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

Don't we want to handle catch (PostgresException p) when (p.SqlState == "42P04") here too? Or is the expectation that they are already checking for database existence.

Copy link
Member Author

Choose a reason for hiding this comment

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

Since the script could contain other things than create database I didn't want to hide errors. But now it's only supporting create db so it's doing the same thing.

Comment on lines +31 to +34
var connectionStringBuilder = new DbConnectionStringBuilder
{
["Database"] = DatabaseName
};
Copy link
Member

Choose a reason for hiding this comment

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

What's special about this that we need to use the builder? Does it escape properly?

Copy link
Member Author

Choose a reason for hiding this comment

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

It does, and there is a functional test to validate we can pass database name with special chars to ensure the connection string and the default CREATE DATABASE script are correctly escaping.

Copy link
Member Author

Choose a reason for hiding this comment

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

I did it for Sql Server too btw.

@sebastienros sebastienros merged commit 95d28f6 into main Mar 18, 2025
162 checks passed
@sebastienros sebastienros deleted the sebros/postgresadddatabase branch March 18, 2025 17:47
@github-actions github-actions bot locked and limited conversation to collaborators Apr 18, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-app-model Issues pertaining to the APIs in Aspire.Hosting, e.g. DistributedApplication

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants