Skip to content

Commit f6db2e1

Browse files
authored
✨ Added command to drop database on demand (#411)
1 parent 0e404b3 commit f6db2e1

4 files changed

Lines changed: 42 additions & 2 deletions

File tree

tools/AppHost/AppHost.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
<ItemGroup>
1212
<PackageReference Include="Aspire.Hosting.AppHost" Version="9.0.0-rc.1.24511.1"/>
1313
<PackageReference Include="Aspire.Hosting.SqlServer" Version="9.0.0-rc.1.24511.1"/>
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0-rc.2.24474.1" />
1415
</ItemGroup>
1516

1617
<ItemGroup>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Microsoft.EntityFrameworkCore;
2+
using Microsoft.Extensions.Diagnostics.HealthChecks;
3+
4+
namespace AppHost.Commands;
5+
6+
public static class SqlServerCommandExt
7+
{
8+
public static IResourceBuilder<SqlServerServerResource> WithDropDatabaseCommand(
9+
this IResourceBuilder<SqlServerServerResource> builder)
10+
{
11+
builder.WithCommand(
12+
"drop-database",
13+
"Drop Database",
14+
async context =>
15+
{
16+
var connectionString = await builder.Resource.GetConnectionStringAsync();
17+
ArgumentException.ThrowIfNullOrWhiteSpace(connectionString);
18+
19+
var optionsBuilder = new DbContextOptionsBuilder<DbContext>();
20+
optionsBuilder.UseSqlServer(connectionString);
21+
var db = new DbContext(optionsBuilder.Options);
22+
await db.Database.EnsureDeletedAsync();
23+
24+
return CommandResults.Success();
25+
},
26+
context =>
27+
{
28+
if (context.ResourceSnapshot.HealthStatus is HealthStatus.Healthy)
29+
{
30+
return ResourceCommandState.Enabled;
31+
}
32+
33+
return ResourceCommandState.Disabled;
34+
});
35+
return builder;
36+
}
37+
}

tools/AppHost/Program.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
using AppHost.Commands;
12
using Projects;
23

34
var builder = DistributedApplication.CreateBuilder(args);
45

56
// Ensure the port doesn't conflict with other docker containers (or remove it altogether)
67
var sqlServer = builder
78
.AddSqlServer("sql", port: 1800)
9+
.WithDropDatabaseCommand()
810
.WithLifetime(ContainerLifetime.Persistent);
911

1012
var db = sqlServer

tools/MigrationService/Initializers/DbContextInitializerBase.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ public async Task EnsureDatabaseAsync(CancellationToken cancellationToken)
2121
var strategy = DbContext.Database.CreateExecutionStrategy();
2222
await strategy.ExecuteAsync(async () =>
2323
{
24+
// Create the database if it does not exist.
25+
// Do this first so there is then a database to start a transaction against.
2426
if (!await dbCreator.ExistsAsync(cancellationToken))
2527
{
26-
// Create the database if it does not exist.
27-
// Do this first so there is then a database to start a transaction against.
2828
await dbCreator.CreateAsync(cancellationToken);
2929
}
3030
});

0 commit comments

Comments
 (0)