-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseCommand.cs
More file actions
176 lines (143 loc) Β· 5.56 KB
/
Copy pathBaseCommand.cs
File metadata and controls
176 lines (143 loc) Β· 5.56 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
using DbUp;
using DbUp.Builder;
using DbUp.Engine;
using DbUp.Helpers;
using DbUp.SqlServer;
namespace Migratonator;
internal abstract class BaseCommand : ICommand
{
// the path to migration scripts, relative to the base script path
protected const string MigrationPath = "Migrations";
// the name of the table used to track migrations (journal)
protected const string SchemaVersionsTable = "SchemaVersions";
protected BaseCommand(Options options)
{
Options = options;
}
protected Options Options { get; }
public abstract void Perform();
protected UpgradeEngineBuilder CreateJournaledMigrator()
{
// creates a journaled migrator which will be used
// to modify the database schema and track which scripts
// have been applied, so they only get applied once.
//
// manages the instance of the SQL journal here, so that
// the table name is owned by the migrator and safely used.
// the database schema is given as null since that the underlying
// provider will use the default schema, which is `dbo` for MSSQL
// or is provided via the connection string
return CreateMigrator()
.JournalTo(
(connectionManager, upgradeLog) =>
new SqlTableJournal(connectionManager, upgradeLog, null!, SchemaVersionsTable)
);
}
protected UpgradeEngineBuilder CreateUnJournaledMigrator()
{
// creates an un-journaled migrator which will be used
// to run scripts over and over again, such as for views
// stored procedures, functions, etc.
return CreateMigrator()
.JournalTo(new NullJournal());
}
private UpgradeEngineBuilder CreateMigrator()
{
var variables = Variable.LoadVariables(Options.VariablesFile);
// add built-in variables
variables.Add("DatabaseName", Options.DatabaseName);
variables.Add("SchemaName", Options.SchemaName);
var builder = DeployChanges.To
.SqlDatabase(Options.ConnectionString)
// special handling for macros
.WithPreprocessor(new MacroPreprocessor(Options.MacrosFile))
// include defined variables
.WithVariables(variables)
// special handling for SQL CLR assemblies
.WithPreprocessor(new SqlClrAssemblyPreprocessor(Options.ScriptsPath))
// configure execution timeout; null clears underlying providers' default
.WithExecutionTimeout(Options.ExecutionTimeout)
// display script outputs, so that issues can be spotted
.LogScriptOutput();
if (Options.Debug)
builder = builder
.WithPreprocessor(new ScriptDebugPreprocessor());
return builder;
}
protected void Apply(UpgradeEngine migrator, bool dryRun = true, bool confirm = false, bool dumpSchema = true)
{
if (!migrator.IsUpgradeRequired())
{
Console.WriteLine("π No scripts to run. Exiting.");
return;
}
var scripts = migrator.GetScriptsToExecute();
if (dryRun || !confirm)
{
if (Options.Verbose)
{
Console.WriteLine("π The following scripts will be executed:");
Console.WriteLine();
foreach (var script in scripts)
Console.WriteLine($" β {script.Name}");
}
else
{
Console.WriteLine($"π {scripts.Count} scripts will be executed.");
}
if (dryRun) return;
if (!PromptUserToContinue($"Apply changes to '{Options.DatabaseName}' database?")) return;
}
Console.WriteLine($"Applying {scripts.Count} changes...");
var result = migrator.PerformUpgrade();
if (!result.Successful)
{
ReportFailure(result.Error.Message);
return;
}
// dump the database schema
if (dumpSchema)
new SchemaDumpCommand(Options).Perform();
ReportSuccess("Applied changes successfully!");
}
public static bool PromptUserToContinue(string message)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine();
Console.Write($"{message} (Y/N): ");
Console.ResetColor();
var answer = Console.ReadLine();
if (string.Equals(answer, "Y", StringComparison.InvariantCultureIgnoreCase)) return true;
Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("π Operation cancelled, exiting.");
Console.ResetColor();
return false;
}
// ReSharper disable once MemberCanBePrivate.Global
public static void ReportSuccess(string message)
{
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine(message);
Console.ResetColor();
}
// ReSharper disable once MemberCanBePrivate.Global
public static void ReportWarning(string message)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine(message);
Console.ResetColor();
}
// ReSharper disable once MemberCanBePrivate.Global
public static void ReportFailure(string message)
{
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(message);
Console.ResetColor();
}
// ReSharper disable once MemberCanBePrivate.Global
public static void ReportError(Exception exception)
{
ReportFailure(exception.Message);
Console.WriteLine(exception.StackTrace);
}
}