Skip to content

Commit b626345

Browse files
author
Hamid Mayeli
committed
Add DbUp NuGet and first run logic
1 parent d404939 commit b626345

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

SimpleDbUp/EmptyClass.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using DbUp;
2+
using DbUp.Builder;
3+
using DbUp.Engine;
4+
using MySqlX.XDevAPI.Common;
5+
6+
namespace SimpleDbUp;
7+
8+
public class EmptyClass
9+
{
10+
public static void Run(string connectionString, string scriptsPath)
11+
{
12+
try
13+
{
14+
EnsureDatabase.For.MySqlDatabase(connectionString);
15+
}
16+
catch (Exception exception)
17+
{
18+
ExitWithError(exception);
19+
}
20+
21+
var upgrade = DeployChanges.To
22+
.MySqlDatabase(connectionString)
23+
.WithScriptsFromFileSystem(
24+
scriptsPath,
25+
(filename) => filename.EndsWith(".sql", StringComparison.CurrentCultureIgnoreCase),
26+
new SqlScriptOptions
27+
{
28+
ScriptType = DbUp.Support.ScriptType.RunOnce
29+
})
30+
.LogToConsole()
31+
.LogScriptOutput()
32+
.Build();
33+
34+
var result = upgrade.PerformUpgrade();
35+
36+
if (!result.Successful)
37+
{
38+
ExitWithError(result.Error);
39+
}
40+
41+
Console.ForegroundColor = ConsoleColor.Green;
42+
Console.WriteLine("Success!");
43+
Console.ResetColor();
44+
Environment.Exit(0);
45+
}
46+
47+
private static void ExitWithError(Exception exception)
48+
{
49+
Console.ForegroundColor = ConsoleColor.Red;
50+
Console.WriteLine(exception);
51+
Console.ResetColor();
52+
#if DEBUG
53+
Console.ReadLine();
54+
#endif
55+
Environment.Exit(1);
56+
}
57+
}

SimpleDbUp/Program.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.CommandLine;
22
using System.CommandLine.Builder;
33
using System.CommandLine.Parsing;
4+
using SimpleDbUp;
45

56
var command = new RootCommand();
67

@@ -12,11 +13,11 @@
1213
command.AddOption(connectionString);
1314
command.AddOption(scriptDirectory);
1415

15-
command.SetHandler((conStr, scripts) => Console.WriteLine((conStr + "-" + scripts) ?? "Null"), connectionString, scriptDirectory);
16+
command.SetHandler(EmptyClass.Run, connectionString, scriptDirectory);
1617

1718
var parser = new CommandLineBuilder(command)
1819
.UseHelp()
1920
.UseDefaults()
2021
.Build();
2122

22-
await parser.InvokeAsync(args);
23+
parser.Invoke(args);

SimpleDbUp/SimpleDbUp.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99

1010
<ItemGroup>
1111
<None Remove="System.CommandLine" />
12+
<None Remove="dbup-mysql" />
1213
</ItemGroup>
1314
<ItemGroup>
1415
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
16+
<PackageReference Include="dbup-mysql" Version="5.0.10" />
1517
</ItemGroup>
1618
</Project>

0 commit comments

Comments
 (0)