-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ 226 Run Migrations and Seed Data via Hosted Service (#386)
* WIP * Added MigrationService.csproj * Removed Database.csproj and refactored integration tests initialization logic * Downgrade todo to note * Update tools/MigrationService/Program.cs Co-authored-by: Matthew Parker [SSW] <[email protected]> --------- Co-authored-by: Matthew Parker [SSW] <[email protected]>
- Loading branch information
1 parent
63e90fa
commit 986c62e
Showing
19 changed files
with
197 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
tools/MigrationService/Initializers/DbContextInitializerBase.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.EntityFrameworkCore.Infrastructure; | ||
using Microsoft.EntityFrameworkCore.Storage; | ||
|
||
namespace MigrationService.Initializers; | ||
|
||
public abstract class DbContextInitializerBase<T> where T : DbContext | ||
{ | ||
protected readonly T DbContext; | ||
|
||
// public constructor needed for DI | ||
internal DbContextInitializerBase(T dbContext) | ||
{ | ||
DbContext = dbContext; | ||
} | ||
|
||
public async Task EnsureDatabaseAsync(CancellationToken cancellationToken) | ||
{ | ||
var dbCreator = DbContext.GetService<IRelationalDatabaseCreator>(); | ||
|
||
var strategy = DbContext.Database.CreateExecutionStrategy(); | ||
await strategy.ExecuteAsync(async () => | ||
{ | ||
// Create the database if it does not exist. | ||
// Do this first so there is then a database to start a transaction against. | ||
if (!await dbCreator.ExistsAsync(cancellationToken)) | ||
{ | ||
await dbCreator.CreateAsync(cancellationToken); | ||
} | ||
}); | ||
} | ||
|
||
public async Task RunMigrationAsync(CancellationToken cancellationToken) | ||
{ | ||
var strategy = DbContext.Database.CreateExecutionStrategy(); | ||
await strategy.ExecuteAsync(async () => | ||
{ | ||
// Run migration in a transaction to avoid partial migration if it fails. | ||
// await using var transaction = await DbContext.Database.BeginTransactionAsync(cancellationToken); | ||
await DbContext.Database.MigrateAsync(cancellationToken); | ||
// await transaction.CommitAsync(cancellationToken); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Worker"> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Aspire.Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0-rc.1.24511.1" /> | ||
<PackageReference Include="Bogus" Version="35.6.1" /> | ||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="9.0.0-rc.2.24473.5" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\..\src\Application\Application.csproj" /> | ||
<ProjectReference Include="..\..\src\Infrastructure\Infrastructure.csproj" /> | ||
</ItemGroup> | ||
</Project> |
Oops, something went wrong.