Skip to content

Commit

Permalink
Merge pull request #227 from DFE-Digital/FHB-732
Browse files Browse the repository at this point in the history
[FHB-732] Disable EF Core runtime migrations
  • Loading branch information
Top-Cat authored Nov 12, 2024
2 parents 274bb14 + 85c29af commit cfa0773
Show file tree
Hide file tree
Showing 28 changed files with 4,683 additions and 340 deletions.
4 changes: 2 additions & 2 deletions src/service/idam-api/src/FamilyHubs.Idam.Api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public static async Task Main(string[] args)

builder.Services.RegisterApplicationComponents(builder.Configuration);

builder.Services.ConfigureServices(builder.Configuration, builder.Environment.IsProduction());
builder.Services.ConfigureServices(builder.Configuration);

var webApplication = builder.Build();

await webApplication.ConfigureWebApplication();
webApplication.ConfigureWebApplication();

await webApplication.RunAsync();
}
Expand Down
27 changes: 2 additions & 25 deletions src/service/idam-api/src/FamilyHubs.Idam.Api/StartupExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private static void RegisterMediator(this IServiceCollection services)
services.AddTransient<ExceptionHandlingMiddleware>();
}

public static void ConfigureServices(this IServiceCollection services, IConfiguration configuration, bool isProduction)
public static void ConfigureServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<ITelemetryInitializer, IdamsTelemetryPiiRedactor>();
services.AddApplicationInsightsTelemetry();
Expand All @@ -132,7 +132,7 @@ public static void ConfigureServices(this IServiceCollection services, IConfigur
services.AddFamilyHubsHealthChecks(configuration);
}

public static async Task ConfigureWebApplication(this WebApplication webApplication)
public static void ConfigureWebApplication(this WebApplication webApplication)
{
webApplication.UseSerilogRequestLogging();

Expand All @@ -148,28 +148,5 @@ public static async Task ConfigureWebApplication(this WebApplication webApplicat
webApplication.MapControllers();

webApplication.MapFamilyHubsHealthChecks(typeof(StartupExtensions).Assembly);

await webApplication.InitialiseDatabase();
}

private static async Task InitialiseDatabase(this WebApplication webApplication)
{
using var scope = webApplication.Services.CreateScope();

// Seed Database
var dbContext = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
var shouldRestDatabaseOnRestart = webApplication.Configuration.GetValue<bool>("ShouldRestDatabaseOnRestart");

if (!webApplication.Environment.IsProduction())
{

if (shouldRestDatabaseOnRestart)
await dbContext.Database.EnsureDeletedAsync();

if(dbContext.Database.IsSqlServer())
await dbContext.Database.MigrateAsync();
else
await dbContext.Database.EnsureCreatedAsync();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
"aeg-sas-key": "",
"ServiceDirectoryApiBaseUrl": "https://localhost:7022/",
"UseSqlite": false,
"ShouldRestDatabaseOnRestart": false,
"LogLevel": "Verbose",
"AllowedHosts": "*",
"APPINSIGHTS_INSTRUMENTATIONKEY": "",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void SetupTestData(bool many = false)
{
var context = scopedServices.GetRequiredService<ApplicationDbContext>();

context.Database.EnsureCreated();
context.Accounts.Add(TestDataProvider.GetTestAccount(many));

context.SaveChanges();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public static async Task Main(string[] args)

builder.Services.RegisterApplicationComponents(builder.Configuration);

builder.Services.ConfigureServices(builder.Configuration, builder.Environment.IsProduction());
builder.Services.ConfigureServices(builder.Configuration);

var webApplication = builder.Build();

await webApplication.ConfigureWebApplication();
webApplication.ConfigureWebApplication();

await webApplication.RunAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ private static void RegisterAutoMapper(this IServiceCollection services)
private static void RegisterAppDbContext(this IServiceCollection services, IConfiguration configuration)
{
services.AddTransient<AuditableEntitySaveChangesInterceptor>();
services.AddTransient<ApplicationDbContextInitialiser>();

var connectionString = configuration.GetConnectionString("NotificationConnection");
ArgumentException.ThrowIfNullOrEmpty(connectionString);
Expand Down Expand Up @@ -151,7 +150,7 @@ public static void RegisterMediator(this IServiceCollection services)
services.AddTransient<ExceptionHandlingMiddleware>();
}

public static void ConfigureServices(this IServiceCollection services, IConfiguration configuration, bool isProduction)
public static void ConfigureServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddApplicationInsightsTelemetry();

Expand All @@ -167,7 +166,7 @@ public static void ConfigureServices(this IServiceCollection services, IConfigur
});
}

public static async Task ConfigureWebApplication(this WebApplication webApplication)
public static void ConfigureWebApplication(this WebApplication webApplication)
{
webApplication.UseSerilogRequestLogging();

Expand All @@ -184,29 +183,14 @@ public static async Task ConfigureWebApplication(this WebApplication webApplicat

webApplication.MapFamilyHubsHealthChecks(typeof(StartupExtensions).Assembly);

await RegisterEndPoints(webApplication);
RegisterEndPoints(webApplication);
}

private static async Task RegisterEndPoints(this WebApplication app)
private static void RegisterEndPoints(this WebApplication app)
{
using var scope = app.Services.CreateScope();

var notifyApi = scope.ServiceProvider.GetService<MinimalNotifyEndPoints>();
notifyApi?.RegisterMinimalNotifyEndPoints(app);

try
{
if (!app.Environment.IsProduction())
{
// Seed Database
var initialiser = scope.ServiceProvider.GetRequiredService<ApplicationDbContextInitialiser>();
var shouldRestDatabaseOnRestart = app.Configuration.GetValue<bool>("ShouldRestDatabaseOnRestart");
await initialiser.InitialiseAsync(app.Environment.IsProduction(), shouldRestDatabaseOnRestart);
}
}
catch (Exception ex)
{
Log.Error(ex, "An error occurred seeding the DB. {exceptionMessage}", ex.Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"NotificationConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=FamilyHubs.Notify.Database;Integrated Security=True;MultipleActiveResultSets=True;Pooling=False;Connect Timeout=30;"
},
"UseSqlite": false,
"ShouldRestDatabaseOnRestart": false,
"Crypto": {
"DbEncryptionKey": "",
"DbEncryptionIVKey": ""
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,8 @@ public void SetupTestDatabaseAndSeedData()

try
{
#pragma warning disable S1481
var context = scopedServices.GetRequiredService<ApplicationDbContext>();
#pragma warning restore S1481



context.Database.EnsureCreated();
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ public static async Task Main(string[] args)

builder.Services.RegisterApplicationComponents(builder.Configuration);

builder.Services.ConfigureServices(builder.Configuration, builder.Environment.IsProduction());
builder.Services.ConfigureServices(builder.Configuration);

var webApplication = builder.Build();

await webApplication.ConfigureWebApplication();
webApplication.ConfigureWebApplication();

await webApplication.RunAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ private static void RegisterAutoMapper(this IServiceCollection services)
private static void RegisterAppDbContext(this IServiceCollection services, IConfiguration configuration)
{
services.AddTransient<AuditableEntitySaveChangesInterceptor>();
services.AddTransient<ApplicationDbContextInitialiser>();

var connectionString = configuration.GetConnectionString("ReferralConnection");
ArgumentException.ThrowIfNullOrEmpty(connectionString);
Expand Down Expand Up @@ -170,7 +169,7 @@ public static void RegisterMediator(this IServiceCollection services)
services.AddTransient<ExceptionHandlingMiddleware>();
}

public static void ConfigureServices(this IServiceCollection services, IConfiguration configuration, bool isProduction)
public static void ConfigureServices(this IServiceCollection services, IConfiguration configuration)
{
services.AddSingleton<ITelemetryInitializer, ConnectTelemetryPiiRedactor>();
services.AddApplicationInsightsTelemetry();
Expand All @@ -187,7 +186,7 @@ public static void ConfigureServices(this IServiceCollection services, IConfigur
});
}

public static async Task ConfigureWebApplication(this WebApplication webApplication)
public static void ConfigureWebApplication(this WebApplication webApplication)
{
webApplication.UseSerilogRequestLogging();

Expand All @@ -204,10 +203,10 @@ public static async Task ConfigureWebApplication(this WebApplication webApplicat

webApplication.MapFamilyHubsHealthChecks(typeof(StartupExtensions).Assembly);

await RegisterEndPoints(webApplication);
RegisterEndPoints(webApplication);
}

private static async Task RegisterEndPoints(this WebApplication app)
private static void RegisterEndPoints(this WebApplication app)
{
using var scope = app.Services.CreateScope();

Expand All @@ -224,21 +223,5 @@ private static async Task RegisterEndPoints(this WebApplication app)
throw new InvalidOperationException("MinimalUserAccountEndPoints is not registered");
}
userAccountsApi.RegisterUserAccountEndPoints(app);

try
{
if (!app.Environment.IsProduction())
{
// Seed Database
// Seed Database
var initialiser = scope.ServiceProvider.GetRequiredService<ApplicationDbContextInitialiser>();
var shouldRestDatabaseOnRestart = app.Configuration.GetValue<bool>("ShouldRestDatabaseOnRestart");
await initialiser.InitialiseAsync(app.Environment.IsProduction(), shouldRestDatabaseOnRestart);
}
}
catch (Exception ex)
{
Log.Error(ex, "An error occurred seeding the DB. {ExceptionMessage}", ex.Message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"ReferralConnection": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=FamilyHubs.Referral.Database;Integrated Security=True;MultipleActiveResultSets=True;Pooling=False;Connect Timeout=30;"
},
"UseSqlite": false,
"ShouldRestDatabaseOnRestart": false,
"GovUkOidcConfiguration": {
"BearerTokenSigningKey": ""
},
Expand Down
Loading

0 comments on commit cfa0773

Please sign in to comment.