-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
357 lines (310 loc) · 9.54 KB
/
Program.cs
File metadata and controls
357 lines (310 loc) · 9.54 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using Migratonator;
using System.CommandLine;
// ------------------------------------------------------------
// global options
var optionConnectionString = new Option<string>(
"--connection",
description:
"SQL Server connection string. Can also be provided using the MIGRATONATOR_CONNECTION environment variable.\n" +
"E.g. \"Server=(local);Database=ApplicationDB;User=sa;Password=<redacted>;...etc...;\".",
isDefault: true,
parseArgument: result =>
{
var connectionString = string.Empty;
if (result.Tokens.Any())
connectionString = result.Tokens.Single().Value;
if (string.IsNullOrWhiteSpace(connectionString))
connectionString = Environment.GetEnvironmentVariable("MIGRATONATOR_CONNECTION");
if (string.IsNullOrWhiteSpace(connectionString))
result.ErrorMessage = $"A connection string is required.";
return connectionString!;
}
) { IsRequired = true };
// if null, will use the underlying provider default
var optionExecutionTimeout = new Option<TimeSpan?>(
new[] { "--timeout" },
"Script execution timeout."
);
// base path to work off
// all relative paths will be in respect of this path
var optionScriptsPath = new Option<string>(
new[] { "--path" },
Directory.GetCurrentDirectory,
"Base path for scripts."
) { IsRequired = true };
// ensure --path directory exists
optionScriptsPath.AddValidator(result =>
{
var path = result.GetValueForOption(optionScriptsPath);
if (!Directory.Exists(path))
result.ErrorMessage = $"The base path '{path}' does not exist.";
});
var optionAssemblyBinariesPath = new Option<string>(
new[] { "--binaries" },
Directory.GetCurrentDirectory,
"Path for SQL CLR assembly binaries."
) { IsRequired = true };
// ensure --binaries directory exists
optionAssemblyBinariesPath.AddValidator(result =>
{
var path = result.GetValueForOption(optionAssemblyBinariesPath);
if (!Directory.Exists(path))
result.ErrorMessage = $"The path '{path}' for SQL CLR assembly binaries does not exist.";
});
var optionVariablesFile = new Option<string>(
new[] { "--variables" },
() => "Variables.json",
"Variables file."
) { IsRequired = true };
// ensure --variables file exists
optionVariablesFile.AddValidator(result =>
{
// ensure variables file exists
var fileName = result.GetValueForOption(optionVariablesFile);
if (!File.Exists(fileName))
result.ErrorMessage = $"The variables file '{fileName}' does not exist.";
});
var optionMacrosFile = new Option<string>(
new[] { "--macros" },
() => "Macros.json",
"Macros file."
) { IsRequired = true };
// ensure --macros file exists
optionMacrosFile.AddValidator(result =>
{
// ensure variables file exists
var fileName = result.GetValueForOption(optionMacrosFile);
if (!File.Exists(fileName))
result.ErrorMessage = $"The macros file '{fileName}' does not exist.";
});
var optionSchemaFile = new Option<string>(
new[] { "--schema-file" },
() => "Schema.sql",
"Filename of the database schema file."
) { IsRequired = true };
// the schema file may not exist yet, so validate
// the directory instead if it's a rooted path
optionSchemaFile.AddValidator(result =>
{
// ensure variables file exists
var fileName = result.GetValueForOption(optionSchemaFile);
if (string.IsNullOrWhiteSpace(fileName))
{
result.ErrorMessage = $"The schema file is required.";
return;
}
if (!Path.IsPathRooted(fileName)) return;
var path = Path.GetDirectoryName(fileName);
if (!Directory.Exists(path))
result.ErrorMessage = $"The schema file's path '{path}' does not exist.";
});
var optionConfirm = new Option<bool>(
new[] { "--confirm" },
"Hide confirmation prompt. Typically used for CI/CD pipelines or in non-interactive scenarios."
);
var optionVerbose = new Option<bool>(
new[] { "--verbose", "-v" },
"Enable verbose logging."
);
var optionDebug = new Option<bool>(
new[] { "--debug" },
"Output final scripts before execution."
);
// use binding to manage global options
var optionsBinding = new OptionsBinder(
optionConnectionString,
optionExecutionTimeout,
optionScriptsPath,
optionVariablesFile,
optionMacrosFile,
optionSchemaFile,
optionAssemblyBinariesPath,
optionConfirm,
optionVerbose,
optionDebug
);
// ------------------------------------------------------------
// db commands
//
// db create command
var commandCreateDatabase = new Command(
"create",
"Create the database."
);
commandCreateDatabase.SetHandler(
(config) => { new CreateDatabaseCommand(config).Perform(); },
optionsBinding
);
// db drop command
var commandDropDatabase = new Command(
"drop",
"Drop the database."
);
commandDropDatabase.SetHandler(
(config) => { new DropDatabaseCommand(config).Perform(); },
optionsBinding
);
var commandDatabase = new Command(
"db",
"Database management commands."
);
commandDatabase.AddCommand(commandCreateDatabase);
commandDatabase.AddCommand(commandDropDatabase);
// ------------------------------------------------------------
// migration commands
//
var optionSteps = new Option<int>(
new[] { "--steps", "-n" },
() => 1,
"Number of migrations to execute."
);
var optionDryRun = new Option<bool>(
new[] { "--dry-run" },
"Run the migrations without actually applying them."
);
var argumentName = new Argument<string>
{
Name = "name",
Description = "Name of the migration. E.g. Add Measure to Table X"
};
// create command
var commandMigrateCreate = new Command(
"create",
"Create a new database migration."
);
commandMigrateCreate.AddArgument(argumentName);
commandMigrateCreate.SetHandler(
(config, name) => { new MigrateCreateCommand(config, name).Perform(); },
optionsBinding,
argumentName
);
// up command
var commandMigrateUp = new Command(
"up",
"Apply the database migrations."
);
commandMigrateUp.AddOption(optionSchemaFile);
commandMigrateUp.AddOption(optionDryRun);
commandMigrateUp.AddOption(optionConfirm);
commandMigrateUp.SetHandler(
(config, dryRun) => { new MigrateUpCommand(config, dryRun).Perform(); },
optionsBinding,
optionDryRun
);
// scripts command
var commandMigrateScripts = new Command(
"scripts",
"Apply the database scripts."
);
commandMigrateScripts.AddOption(optionSchemaFile);
commandMigrateScripts.AddOption(optionDryRun);
commandMigrateScripts.AddOption(optionConfirm);
commandMigrateScripts.SetHandler(
(config, dryRun) => { new MigrateScriptCommand(config, dryRun).Perform(); },
optionsBinding,
optionDryRun
);
// down command
var commandMigrateDown = new Command(
"down",
"Rollback the database migrations. NOT YET IMPLEMENTED"
)
{
IsHidden = true // not yet implemented
};
commandMigrateDown.AddOption(optionSteps);
commandMigrateDown.AddOption(optionDryRun);
commandMigrateDown.AddOption(optionConfirm);
commandMigrateDown.SetHandler(
(config, steps) => { new MigrateDownCommand(config, steps).Perform(); },
optionsBinding,
optionSteps
);
var commandMigrate = new Command(
"migrate",
"Database migration commands."
);
commandMigrate.AddCommand(commandMigrateCreate);
commandMigrate.AddCommand(commandMigrateUp);
commandMigrate.AddCommand(commandMigrateScripts);
commandMigrate.AddCommand(commandMigrateDown);
// ------------------------------------------------------------
// schema commands
//
// dump command
var commandSchemaDump = new Command(
"dump",
"Dumps the database schema to file."
);
commandSchemaDump.SetHandler(
config => { new SchemaDumpCommand(config).Perform(); },
optionsBinding
);
// load command
var commandSchemaLoad = new Command(
"load",
"Loads the database schema from file into an empty database."
);
commandSchemaLoad.AddOption(optionConfirm);
commandSchemaLoad.SetHandler(
config => { new SchemaLoadCommand(config).Perform(); },
optionsBinding
);
var commandSchema = new Command(
"schema",
"Database schema commands."
);
commandSchema.AddGlobalOption(optionSchemaFile);
commandSchema.AddCommand(commandSchemaDump);
commandSchema.AddCommand(commandSchemaLoad);
// ------------------------------------------------------------
// seeds commands
//
var commandSeeds = new Command(
"seeds",
"Apply the database data seed scripts."
);
commandSeeds.AddOption(optionDryRun);
commandSeeds.AddOption(optionConfirm);
commandSeeds.SetHandler(
(config, dryRun) => { new SeedsCommand(config, dryRun).Perform(); },
optionsBinding,
optionDryRun
);
// ------------------------------------------------------------
// init commands
//
var commandInit = new Command(
"init",
"Initialise a project directory."
);
commandSeeds.AddOption(optionConfirm);
commandSeeds.SetHandler(
(config) => { new InitialiseCommand(config).Perform(); },
optionsBinding
);
//
// root command
//
var rootCommand = new RootCommand($"{Utilities.ToolName} - Database Migration Tool");
rootCommand.AddGlobalOption(optionConnectionString);
rootCommand.AddGlobalOption(optionScriptsPath);
rootCommand.AddGlobalOption(optionVariablesFile);
rootCommand.AddGlobalOption(optionMacrosFile);
rootCommand.AddGlobalOption(optionAssemblyBinariesPath);
rootCommand.AddGlobalOption(optionVerbose);
rootCommand.AddGlobalOption(optionDebug);
rootCommand.AddCommand(commandDatabase);
rootCommand.AddCommand(commandMigrate);
rootCommand.AddCommand(commandSchema);
rootCommand.AddCommand(commandSeeds);
rootCommand.AddCommand(commandInit);
try
{
return rootCommand.Invoke(args);
}
catch (Exception e)
{
BaseCommand.ReportError(e);
return -1;
}