forked from lkurzyniec/netcore-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
68 lines (59 loc) · 2.26 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.IO;
using System.Reflection;
using System.Text.RegularExpressions;
using DbUp;
using HappyCode.NetCoreBoilerplate.Db.Options;
using Microsoft.Extensions.Configuration;
namespace HappyCode.NetCoreBoilerplate.Db
{
class Program
{
private static Regex _runPattern = new Regex(@".*");
public static int Main(string[] args)
{
var configuration = LoadAppConfiguration();
var upgradeOptions = configuration.GetSection("UpgradeOptions").Get<UpgradeOptions>();
SetRunPattern(upgradeOptions.RunPattern);
var upgrader = DeployChanges.To
.SqlDatabase(configuration.GetConnectionString("MsSqlDb"))
.WithScriptsAndCodeEmbeddedInAssembly(Assembly.GetExecutingAssembly(), (fileName) => _runPattern.IsMatch(fileName))
.WithExecutionTimeout(TimeSpan.FromSeconds(upgradeOptions.CommandExecutionTimeoutSeconds))
.WithTransaction()
.LogToConsole()
.Build();
var result = upgrader.PerformUpgrade();
if (!result.Successful)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(result.Error);
Console.ResetColor();
#if DEBUG
Console.ReadLine();
#endif
return -1;
}
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Success!");
Console.ResetColor();
return 0;
}
private static void SetRunPattern(string runPattern)
{
if (string.IsNullOrWhiteSpace(runPattern))
{
return;
}
_runPattern = new Regex(runPattern);
}
private static IConfigurationRoot LoadAppConfiguration()
{
return new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Production"}.json", optional: true)
.AddJsonFile("appsettings.local.json", optional: true)
.Build();
}
}
}