Skip to content

Commit

Permalink
Wait for dependent resources before depolying the .dacpac
Browse files Browse the repository at this point in the history
  • Loading branch information
ErikEJ committed Jan 11, 2025
1 parent bc4c32d commit e1b25ee
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
var builder = DistributedApplication.CreateBuilder(args);

var server = builder.AddSqlServer("sql")
.AddDatabase("TargetDatabase");
var server = builder.AddSqlServer("sql");

builder.AddSqlProject<Projects.SdkProject>("sdk-project")
.WithReference(server);
var database = server.AddDatabase("TargetDatabase");

var otherDatabase = server.AddDatabase("OtherTargetDatabase");

var sdkProject = builder.AddSqlProject<Projects.SdkProject>("sdk-project")
.WithReference(database);

var otherProject = builder.AddSqlProject<Projects.SdkProject>("other-sdk-project")
.WithReference(otherDatabase)
.WaitForCompletion(sdkProject);

builder.AddSqlPackage<Packages.ErikEJ_Dacpac_Chinook>("chinook")
.WithReference(server);
.WithReference(database);

builder.Build().Run();
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,12 @@ internal static IResourceBuilder<TResource> InternalWithReference<TResource>(thi
builder.ApplicationBuilder.Services.TryAddSingleton<IDacpacDeployer, DacpacDeployer>();
builder.ApplicationBuilder.Services.TryAddSingleton<SqlProjectPublishService>();

builder.ApplicationBuilder.Eventing.Subscribe<ResourceReadyEvent>(target.Resource, (resourceReady, ct) =>
builder.ApplicationBuilder.Eventing.Subscribe<ResourceReadyEvent>(target.Resource, async (resourceReady, ct) =>
{
var notificationService = resourceReady.Services.GetRequiredService<ResourceNotificationService>();
await notificationService.WaitForDependenciesAsync(builder.Resource, ct);
var service = resourceReady.Services.GetRequiredService<SqlProjectPublishService>();
return service.PublishSqlProject(builder.Resource, target.Resource, ct);
await service.PublishSqlProject(builder.Resource, target.Resource, ct);
});

builder.WaitFor(target);
Expand All @@ -184,7 +186,7 @@ internal static IResourceBuilder<TResource> InternalWithReference<TResource>(thi
await service.PublishSqlProject(builder.Resource, target.Resource, context.CancellationToken);
return new ExecuteCommandResult { Success = true };
}, updateState: (context) => context.ResourceSnapshot?.State?.Text == KnownResourceStates.Finished ? ResourceCommandState.Enabled : ResourceCommandState.Disabled,
displayDescription: "Redeploys the SQL Server Database to the target database.",
displayDescription: "Redeploys the SQL Database Project to the target database.",
iconName: "ArrowReset",
iconVariant: IconVariant.Filled,
isHighlighted: true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ namespace CommunityToolkit.Aspire.Hosting.SqlDatabaseProjects.Tests;
public class AppHostTests(AspireIntegrationTestFixture<Projects.CommunityToolkit_Aspire_Hosting_SqlDatabaseProjects_AppHost> fixture) : IClassFixture<AspireIntegrationTestFixture<Projects.CommunityToolkit_Aspire_Hosting_SqlDatabaseProjects_AppHost>>
{
[Theory]
[InlineData("sdk-project", "SdkProject")]
[InlineData("chinook", "InvoiceLine")]
public async Task ProjectBasedResourceStartsAndRespondsOk(string resourceName, string tableName)
[InlineData("sdk-project", "SdkProject", "TargetDatabase")]
[InlineData("other-sdk-project", "SdkProject", "OtherTargetDatabase")]
[InlineData("chinook", "InvoiceLine", "TargetDatabase")]
public async Task ProjectBasedResourceStartsAndRespondsOk(string resourceName, string tableName, string database)
{
await fixture.ResourceNotificationService.WaitForResourceAsync(resourceName, KnownResourceStates.Finished).WaitAsync(TimeSpan.FromMinutes(5));

string? connectionString = await fixture.GetConnectionString("TargetDatabase");
string? connectionString = await fixture.GetConnectionString(database);
Assert.NotNull(connectionString);

using var connection = new SqlConnection(connectionString);
Expand Down

0 comments on commit e1b25ee

Please sign in to comment.