Created
October 1, 2020 04:22
-
-
Save ircnelson/7468b46d8b7cf3e00c347b2de9e246b0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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