microsoft / microsoft/aspire

Add asynchronous overload for WithCreationScript to support deferred value resolution for MySQL, PostgreSQL, and SQL Server

Open
#10,876 4 comments 0 reactions 0 assignees View on GitHub
area-integrations
Dominant language
C#
Stars
6.3k
Forks
991
Avg merge
2d 15h
Merged PRs (30d)
196

Description

## Background and Motivation

Currently, the `WithCreationScript` API only supports synchronous value resolution for SQL scripts. There are scenarios where values such as parameters or endpoints need to be resolved asynchronously at creation time (e.g., fetching secrets, endpoints, or other dynamic resources). Supporting an asynchronous overload would enhance flexibility and enable deferred value resolution when creating databases.

## Proposed API

```diff
namespace Aspire.Hosting.MySql;
namespace Aspire.Hosting.Postgres;
namespace Aspire.Hosting.SqlServer;

public static class MySqlBuilderExtensions
{
+ public static IResourceBuilder WithCreationScriptAsync(
+ this IResourceBuilder builder,
+ Func> scriptProvider)
}

public static class PostgresBuilderExtensions
{
+ public static IResourceBuilder WithCreationScriptAsync(
+ this IResourceBuilder builder,
+ Func> scriptProvider)
}

public static class SqlServerBuilderExtensions
{
+ public static IResourceBuilder WithCreationScriptAsync(
+ this IResourceBuilder builder,
+ Func> scriptProvider)
}
```

## Usage Examples

```csharp
var pwd = builder.AddParameter("pwd", secret: true);

var mydb =
mySqlServer
.AddDatabase("mydb")
.WithCreationScriptAsync(async sp =>
{
var migratorPassword = await pwd.GetValueAsync(default);
return script.Replace("", migratorPassword);
});
```

## Alternative Designs
- Only support async for one provider, or extend to all database providers for consistency.
- Accept a `Task` directly, or keep the `Func>` for maximum flexibility.

## Risks
- Must ensure proper handling in resource creation logic (awaiting script provider).
- May introduce complexity in error handling for asynchronous script resolution.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.