-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add database migrator hosted service (#55)
* feat: add hosted service to apply migrations on startup * refactor: remove indentation in disposable blocks * refactor: add migrator hosted service to ef core infrastructure * refactor: use ilogger instead of serilog to output logs * feat: use use migrator service to migrate encoder database
- Loading branch information
1 parent
98f392e
commit 8fc7de6
Showing
17 changed files
with
187 additions
and
91 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
46 changes: 46 additions & 0 deletions
46
src/Infrastructure/src/EntityFrameworkCore/MigratorHostedService.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,46 @@ | ||
using System.Diagnostics; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Hosting; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace Giantnodes.Infrastructure.EntityFrameworkCore; | ||
|
||
public sealed class MigratorHostedService<TDbContext> : IHostedService | ||
where TDbContext : DbContext | ||
{ | ||
private readonly IServiceScopeFactory _factory; | ||
private readonly ILogger<MigratorHostedService<TDbContext>> _logger; | ||
|
||
public MigratorHostedService(IServiceScopeFactory factory, ILogger<MigratorHostedService<TDbContext>> logger) | ||
{ | ||
_factory = factory; | ||
_logger = logger; | ||
} | ||
|
||
public async Task StartAsync(CancellationToken cancellationToken) | ||
{ | ||
var stopwatch = new Stopwatch(); | ||
using var scope = _factory.CreateScope(); | ||
|
||
var database = scope.ServiceProvider.GetRequiredService<TDbContext>(); | ||
var pending = await database.Database.GetPendingMigrationsAsync(cancellationToken); | ||
var total = pending.Count(); | ||
|
||
if (total <= 0) | ||
{ | ||
_logger.LogInformation("no pending migrations for database context {0}.", typeof(TDbContext).Name); | ||
return; | ||
} | ||
|
||
_logger.LogInformation("applying {0} pending migrations for database context {1}...", total, typeof(TDbContext).Name); | ||
stopwatch.Start(); | ||
await database.Database.MigrateAsync(cancellationToken); | ||
stopwatch.Stop(); | ||
_logger.LogInformation("successfully applied {0} migrations for database context {1} in {2} ms.", total, typeof(TDbContext).Name, stopwatch.ElapsedMilliseconds); | ||
} | ||
|
||
public Task StopAsync(CancellationToken cancellationToken) | ||
{ | ||
return Task.CompletedTask; | ||
} | ||
} |
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
41 changes: 27 additions & 14 deletions
41
...rations/20240413010042_v0.0.1.Designer.cs → ...rations/20240528095559_v0.0.1.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Oops, something went wrong.