|
|
@ -2,23 +2,23 @@ |
|
|
|
|
|
|
|
public static class WebHostExtensions |
|
|
|
{ |
|
|
|
public static bool IsInKubernetes(this IWebHost host) |
|
|
|
public static bool IsInKubernetes(this IServiceProvider services) |
|
|
|
{ |
|
|
|
var cfg = host.Services.GetService<IConfiguration>(); |
|
|
|
var cfg = services.GetService<IConfiguration>(); |
|
|
|
var orchestratorType = cfg.GetValue<string>("OrchestratorType"); |
|
|
|
return orchestratorType?.ToUpper() == "K8S"; |
|
|
|
} |
|
|
|
|
|
|
|
public static IWebHost MigrateDbContext<TContext>(this IWebHost host, Action<TContext, IServiceProvider> seeder) where TContext : DbContext |
|
|
|
public static IServiceProvider MigrateDbContext<TContext>(this IServiceProvider services, Action<TContext, IServiceProvider> seeder) where TContext : DbContext |
|
|
|
{ |
|
|
|
var underK8s = host.IsInKubernetes(); |
|
|
|
var underK8s = services.IsInKubernetes(); |
|
|
|
|
|
|
|
using var scope = host.Services.CreateScope(); |
|
|
|
var services = scope.ServiceProvider; |
|
|
|
using var scope = services.CreateScope(); |
|
|
|
var scopedServices = scope.ServiceProvider; |
|
|
|
|
|
|
|
var logger = services.GetRequiredService<ILogger<TContext>>(); |
|
|
|
var logger = scopedServices.GetRequiredService<ILogger<TContext>>(); |
|
|
|
|
|
|
|
var context = services.GetService<TContext>(); |
|
|
|
var context = scopedServices.GetService<TContext>(); |
|
|
|
|
|
|
|
try |
|
|
|
{ |
|
|
@ -26,7 +26,7 @@ public static class WebHostExtensions |
|
|
|
|
|
|
|
if (underK8s) |
|
|
|
{ |
|
|
|
InvokeSeeder(seeder, context, services); |
|
|
|
InvokeSeeder(seeder, context, scopedServices); |
|
|
|
} |
|
|
|
else |
|
|
|
{ |
|
|
@ -42,7 +42,7 @@ public static class WebHostExtensions |
|
|
|
//migration can't fail for network related exception. The retry options for DbContext only
|
|
|
|
//apply to transient exceptions
|
|
|
|
// Note that this is NOT applied when running some orchestrators (let the orchestrator to recreate the failing service)
|
|
|
|
retry.Execute(() => InvokeSeeder(seeder, context, services)); |
|
|
|
retry.Execute(() => InvokeSeeder(seeder, context, scopedServices)); |
|
|
|
} |
|
|
|
|
|
|
|
logger.LogInformation("Migrated database associated with context {DbContextName}", typeof(TContext).Name); |
|
|
@ -56,7 +56,7 @@ public static class WebHostExtensions |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return host; |
|
|
|
return services; |
|
|
|
} |
|
|
|
|
|
|
|
private static void InvokeSeeder<TContext>(Action<TContext, IServiceProvider> seeder, TContext context, IServiceProvider services) |
|
|
|