Add asynchronous overload for WithCreationScript to support deferred value resolution for MySQL, PostgreSQL, and SQL Server
- 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
Assessment
This issue has not been assessed yet.