Skip to content

Instantly share code, notes, and snippets.

@ircnelson
Created October 1, 2020 04:22
Show Gist options
  • Save ircnelson/7468b46d8b7cf3e00c347b2de9e246b0 to your computer and use it in GitHub Desktop.
Save ircnelson/7468b46d8b7cf3e00c347b2de9e246b0 to your computer and use it in GitHub Desktop.
public class Program
{
private static void Main(string[] args)
{
var host = Host.CreateDefaultBuilder(args).Build();
using var scope = host.Services.CreateScope();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<Program>>();
var configuration = scope.ServiceProvider.GetService<IConfiguration>();
if (configuration.GetValue("InK8s", false))
{
/*
* Your migration routine
*
* E.g: yourDbContext.Database.Migrate()
*/
}
else
{
const int retries = 10;
var retry = Policy.Handle<SqlException>()
.WaitAndRetry(
retryCount: retries,
sleepDurationProvider: retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, retryAttempt)),
onRetry: (exception, timeSpan, attemptRetry, ctx) =>
{
logger.LogWarning(exception, "{message} :: [attempt {retry} of {retries}]",
exception.Message,
attemptRetry,
retries);
});
retry.Execute(() =>
{
/*
* Your migration routine
*
* E.g: yourDbContext.Database.Migrate()
*/
});
}
logger.LogInformation("Database migrations ended without errors.");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment