merge
This commit is contained in:
commit
2891044c9c
@ -24,9 +24,7 @@ namespace Microsoft.AspNetCore.Hosting
|
||||
using (var scope = webHost.Services.CreateScope())
|
||||
{
|
||||
var services = scope.ServiceProvider;
|
||||
|
||||
var logger = services.GetRequiredService<ILogger<TContext>>();
|
||||
|
||||
var context = services.GetService<TContext>();
|
||||
|
||||
try
|
||||
@ -60,7 +58,6 @@ namespace Microsoft.AspNetCore.Hosting
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message.ToString() + "An error occurred while migrating the database used on context {DbContextName}" + typeof(TContext).Name);
|
||||
logger.LogError(ex, "An error occurred while migrating the database used on context {DbContextName}", typeof(TContext).Name);
|
||||
if (underK8s)
|
||||
{
|
||||
@ -72,72 +69,11 @@ namespace Microsoft.AspNetCore.Hosting
|
||||
return webHost;
|
||||
}
|
||||
|
||||
|
||||
public static IWebHost RemoveDbContext<TContext>(this IWebHost webHost) where TContext : DbContext
|
||||
{
|
||||
var underK8s = webHost.IsInKubernetes();
|
||||
|
||||
using (var scope = webHost.Services.CreateScope())
|
||||
{
|
||||
var services = scope.ServiceProvider;
|
||||
var logger = services.GetRequiredService<ILogger<TContext>>();
|
||||
var context = services.GetService<TContext>();
|
||||
|
||||
try
|
||||
{
|
||||
logger.LogInformation("Deleting the database associated with context {DbContextName}" + typeof(TContext).Name);
|
||||
|
||||
if (underK8s)
|
||||
{
|
||||
InvokeRemover(context);
|
||||
}
|
||||
else
|
||||
{
|
||||
var retries = 10;
|
||||
var retry = Policy.Handle<SqlException>()
|
||||
.WaitAndRetry(
|
||||
retryCount: retries,
|
||||
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
|
||||
onRetry: (exception, timeSpan, retry, ctx) =>
|
||||
{
|
||||
Console.WriteLine(" --RETRYING Exception " + exception.Message.ToString());
|
||||
logger.LogWarning(exception, "[{prefix}] Exception {ExceptionType} with message {Message} detected on attempt {retry} of {retries}", nameof(TContext), exception.GetType().Name, exception.Message, retry, retries);
|
||||
});
|
||||
|
||||
//if the sql server container is not created on run docker compose this
|
||||
//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(() => InvokeRemover(context));
|
||||
}
|
||||
|
||||
Console.WriteLine("Deleted database associated with context {DbContextName}", typeof(TContext).Name);
|
||||
logger.LogInformation("Deleted database associated with context {DbContextName}", typeof(TContext).Name);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine(ex.Message.ToString() + "An error occurred while deleting the database used on context {DbContextName}" + typeof(TContext).Name);
|
||||
logger.LogError(ex, "An error occurred while deleting the database used on context {DbContextName}", typeof(TContext).Name);
|
||||
if (underK8s)
|
||||
{
|
||||
throw; // Rethrow under k8s because we rely on k8s to re-run the pod
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return webHost;
|
||||
}
|
||||
|
||||
private static void InvokeSeeder<TContext>(Action<TContext, IServiceProvider> seeder, TContext context, IServiceProvider services)
|
||||
where TContext : DbContext
|
||||
{
|
||||
context.Database.Migrate();
|
||||
seeder(context, services);
|
||||
}
|
||||
private static void InvokeRemover<TContext>(TContext context)
|
||||
where TContext : DbContext
|
||||
{
|
||||
context.Database.EnsureDeleted();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -35,13 +35,10 @@
|
||||
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
|
||||
.AddEnvironmentVariables()
|
||||
.Build();
|
||||
var connectionString = configuration["ConnectionString"];
|
||||
Console.WriteLine(" -- Connection string");
|
||||
Console.WriteLine(connectionString);
|
||||
|
||||
var connectionString = configuration["ConnectionString"];
|
||||
var optionsBuilder = new DbContextOptionsBuilder<MarketingContext>()
|
||||
.UseSqlServer(connectionString);
|
||||
// .UseSqlServer("Server=.;Initial Catalog=Microsoft.eShopOnContainers.Services.MarketingDb;Integrated Security=true");
|
||||
return new MarketingContext(optionsBuilder.Options);
|
||||
}
|
||||
}
|
||||
|
@ -32,18 +32,9 @@ namespace Marketing.FunctionalTests
|
||||
})
|
||||
.CaptureStartupErrors(true)
|
||||
.UseStartup<MarketingTestsStartup>();
|
||||
|
||||
|
||||
var testServer = new TestServer(hostBuilder);
|
||||
|
||||
using (var scope = testServer.Services.CreateScope())
|
||||
{
|
||||
var services = scope.ServiceProvider;
|
||||
|
||||
var logger = services.GetRequiredService<ILogger<MarketingScenarioBase>>();
|
||||
var settings = services.GetRequiredService<IOptions<MarketingSettings>>();
|
||||
logger.LogError("connectionString " + settings.Value.ConnectionString);
|
||||
}
|
||||
|
||||
testServer.Host
|
||||
.MigrateDbContext<MarketingContext>((context, services) =>
|
||||
{
|
||||
|
Loading…
x
Reference in New Issue
Block a user