From 558fe7bc6786fae892c2dde20defb19a1c617b56 Mon Sep 17 00:00:00 2001 From: Dominic Villemure Date: Sun, 18 Aug 2024 11:06:32 -0400 Subject: [PATCH] Added initializer for dev environment for the new dbContexts --- .../Data/ContentDbContextInitializer.cs | 32 +++++++++++++++++++ .../Features/Contents/DependencyInjection.cs | 1 + .../Data/MessagingDbContextInitializer.cs | 32 +++++++++++++++++++ .../Features/Messages/DependencyInjection.cs | 1 + src/Web/Program.cs | 2 ++ 5 files changed, 68 insertions(+) create mode 100644 src/Web/Features/Contents/Data/ContentDbContextInitializer.cs create mode 100644 src/Web/Features/Messages/Data/MessagingDbContextInitializer.cs diff --git a/src/Web/Features/Contents/Data/ContentDbContextInitializer.cs b/src/Web/Features/Contents/Data/ContentDbContextInitializer.cs new file mode 100644 index 0000000..571e8f5 --- /dev/null +++ b/src/Web/Features/Contents/Data/ContentDbContextInitializer.cs @@ -0,0 +1,32 @@ +namespace Hutopy.Web.Features.Contents.Data; + +public static class InitializerExtensions +{ + public static async Task InitialiseContentDbContextAsync(this WebApplication app) + { + using var scope = app.Services.CreateScope(); + + var initializer = scope.ServiceProvider.GetRequiredService(); + + await initializer.InitialiseAsync(); + } +} + +public class ContentDbContextInitializer( + ILogger logger, + ContentDbContext context + ) +{ + public async Task InitialiseAsync() + { + try + { + await context.Database.MigrateAsync(); + } + catch (Exception ex) + { + logger.LogError(ex, "An error occurred while initialising the content database."); + throw; + } + } +} diff --git a/src/Web/Features/Contents/DependencyInjection.cs b/src/Web/Features/Contents/DependencyInjection.cs index 77c7c40..cbb16d3 100644 --- a/src/Web/Features/Contents/DependencyInjection.cs +++ b/src/Web/Features/Contents/DependencyInjection.cs @@ -9,6 +9,7 @@ public static class DependencyInjection Action? configureAction = null) { services.AddDbContext(configureAction); + services.AddScoped(); return services; } diff --git a/src/Web/Features/Messages/Data/MessagingDbContextInitializer.cs b/src/Web/Features/Messages/Data/MessagingDbContextInitializer.cs new file mode 100644 index 0000000..cd40485 --- /dev/null +++ b/src/Web/Features/Messages/Data/MessagingDbContextInitializer.cs @@ -0,0 +1,32 @@ +namespace Hutopy.Web.Features.Messages.Data; + +public static class InitializerExtensions +{ + public static async Task InitialiseMessagingDbContextAsync(this WebApplication app) + { + using var scope = app.Services.CreateScope(); + + var initializer = scope.ServiceProvider.GetRequiredService(); + + await initializer.InitialiseAsync(); + } +} + +public class MessagingDbContextInitializer( + ILogger logger, + MessagingDbContext context + ) +{ + public async Task InitialiseAsync() + { + try + { + await context.Database.MigrateAsync(); + } + catch (Exception ex) + { + logger.LogError(ex, "An error occurred while initialising the messaging database."); + throw; + } + } +} diff --git a/src/Web/Features/Messages/DependencyInjection.cs b/src/Web/Features/Messages/DependencyInjection.cs index 6f4bf3c..67ff59b 100644 --- a/src/Web/Features/Messages/DependencyInjection.cs +++ b/src/Web/Features/Messages/DependencyInjection.cs @@ -9,6 +9,7 @@ public static class DependencyInjection Action? configureAction = null) { services.AddDbContext(configureAction); + services.AddScoped(); return services; } diff --git a/src/Web/Program.cs b/src/Web/Program.cs index 575d391..132c462 100644 --- a/src/Web/Program.cs +++ b/src/Web/Program.cs @@ -109,6 +109,8 @@ app.UseAuthorization(); // Initialize and seed the db. await app.InitialiseApplicationDatabaseAsync(); +await app.InitialiseContentDbContextAsync(); +await app.InitialiseMessagingDbContextAsync(); await app.SeedDatabaseWithTestDataOnlyIfNoDataIsPresentAsync(); // Configure the HTTP request pipeline.