-
Notifications
You must be signed in to change notification settings - Fork 649
/
MigratorConsole.cs
502 lines (473 loc) · 22.2 KB
/
MigratorConsole.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
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#region License
// Copyright (c) 2007-2024, Fluent Migrator Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using FluentMigrator.Exceptions;
using FluentMigrator.Runner;
using FluentMigrator.Runner.Conventions;
using FluentMigrator.Runner.Exceptions;
using FluentMigrator.Runner.Initialization;
using FluentMigrator.Runner.Initialization.NetFramework;
using FluentMigrator.Runner.Logging;
using FluentMigrator.Runner.Processors;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Mono.Options;
using static FluentMigrator.Runner.ConsoleUtilities;
namespace FluentMigrator.Console
{
public class MigratorConsole
{
public string Connection;
public string ConnectionStringConfigPath;
public string Namespace;
public bool NestedNamespaces;
public bool Output;
public string OutputFilename;
public bool OutputSemicolonDelimiter = false;
public bool PreviewOnly;
public string ProcessorType;
public string Profile;
public bool ShowHelp;
public int Steps;
public List<string> Tags = new List<string>();
public bool IncludeUntaggedMaintenances;
public bool IncludeUntaggedMigrations = true;
public string TargetAssembly;
public string Task;
public int? Timeout;
public bool Verbose;
public bool StopOnError;
public long Version;
public long StartVersion;
public bool NoConnection;
public string WorkingDirectory;
public bool TransactionPerSession;
public bool AllowBreakingChange;
public string ProviderSwitches;
public bool StripComments = true;
public string DefaultSchemaName { get; set; }
public int Run(params string[] args)
{
var dbChoicesList = new List<string>();
string dbChoices;
var services = CreateCoreServices()
.AddScoped<IConnectionStringReader>(_ => new PassThroughConnectionStringReader("No connection"));
using (var sp = services.BuildServiceProvider(validateScopes: false))
{
var processors = sp.GetRequiredService<IEnumerable<IMigrationProcessor>>().ToList();
dbChoicesList.AddRange(processors.Select(p => p.DatabaseType));
}
dbChoices = string.Join(
", ",
dbChoicesList
.Distinct(StringComparer.OrdinalIgnoreCase)
.OrderBy(x => x, StringComparer.OrdinalIgnoreCase));
System.Console.Out.WriteHeader();
try
{
var optionSet = new OptionSet
{
{
"assembly=|a=|target=",
"REQUIRED. The assembly containing the migrations you want to execute.",
v => { TargetAssembly = v; }
},
{
"provider=|dbType=|db=",
$"REQUIRED. The kind of database you are migrating against. Available choices are: {dbChoices}.",
v => { ProcessorType = v; }
},
{
"connectionString=|connection=|conn=|c=",
"The name of the connection string (falls back to machine name) or the connection string itself to the server and database you want to execute your migrations against.",
v => { Connection = v; }
},
{
"connectionStringConfigPath=|configPath=",
string.Format(
"The path of the machine.config where the connection string named by connectionString" +
" is found. If not specified, it defaults to the machine.config used by the currently running CLR version"),
v => { ConnectionStringConfigPath = v; }
},
{
"namespace=|ns=",
"The namespace contains the migrations you want to run. Default is all migrations found within the Target Assembly will be run.",
v => { Namespace = v; }
},
{
"nested",
"Whether migrations in nested namespaces should be included. Used in conjunction with the namespace option.",
v => { NestedNamespaces = v != null; }
},
{
"output|out|o",
"Output generated SQL to a file. Default is no output. Use outputFilename to control the filename, otherwise [assemblyname].sql is the default.",
v => { Output = v != null; }
},
{
"outputSemicolonDelimiter|outsemdel|osd",
"Whether each command should be delimited with a semicolon.",
v => { OutputSemicolonDelimiter = v != null; }
},
{
"outputFilename=|outfile=|of=",
"The name of the file to output the generated SQL to. The output option must be included for output to be saved to the file.",
v => { OutputFilename = v; }
},
{
"preview|p",
"Only output the migration steps - do not execute them. Add the `--verbose` switch to also see the SQL statements. Default is false.",
v => { PreviewOnly = v != null; }
},
{
"steps=",
"The number of versions to rollback if the task is 'rollback'. Default is 1.",
v => { Steps = int.Parse(v); }
},
{
"task=|t=",
"The task you want FluentMigrator to perform. Available choices are: migrate:up, migrate (same as migrate:up), migrate:down, rollback, rollback:toversion, rollback:all, validateversionorder, listmigrations. Default is 'migrate'.",
v => { Task = v; }
},
{
"version=",
"The specific version to migrate. Default is 0, which will run all migrations.",
v => { Version = long.Parse(v); }
},
{
"startVersion=",
"The specific version to start migrating from. Only used when NoConnection is true. Default is 0",
v => { StartVersion = long.Parse(v); }
},
{
"noConnection",
"Indicates that migrations will be generated without consulting a target database. Should only be used when generating an output file.",
v => { NoConnection = v != null; }
},
{
"verbose=",
"Show the SQL statements generated and execution time in the console. Default is false.",
v => { Verbose = v != null; }
},
{
"stopOnError=",
"Pauses migration execution until the user input if any error occurred. Default is false.",
v => { StopOnError = v != null; }
},
{
"workingdirectory=|wd=",
"The directory to load SQL scripts specified by migrations from.",
v => { WorkingDirectory = v; }
},
{
"profile=",
"The profile to run after executing migrations.",
v => { Profile = v; }
},
{
"timeout=",
"Overrides the default SqlCommand timeout of 30 seconds.",
v => { Timeout = int.Parse(v); }
},
{
"tag=",
"Filters the migrations to be run by tag.",
v => { Tags.Add(v); }
},
{
"include-untagged:",
"Include untagged migrations and/or maintenance objects.",
v =>
{
if (string.IsNullOrEmpty(v))
{
IncludeUntaggedMigrations = IncludeUntaggedMaintenances = true;
}
else
{
var items = v.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.ToLowerInvariant().Trim())
.Where(x => !string.IsNullOrEmpty(x))
.Select(
x =>
{
var hasOption = x.EndsWith("+") || x.EndsWith("-");
var enable = !x.EndsWith("-");
var name = hasOption ? x.Substring(0, x.Length - 1) : x;
return new
{
FullName = x,
ShortName = name.Substring(Math.Min(2, name.Length)),
Enable = enable,
};
});
foreach (var item in items)
{
switch (item.ShortName)
{
case "ma":
IncludeUntaggedMaintenances = item.Enable;
break;
case "mi":
IncludeUntaggedMigrations = item.Enable;
break;
default:
throw new ArgumentOutOfRangeException(
$"The argument {item.FullName} is not supported. "
+ "Valid values are: ma, maintenance, mi, migrations with an optional '+' or '-' at the end to enable or disable the option. "
+ "Multiple values may be given when separated by a comma.");
}
}
}
}
},
{
"providerswitches=",
"Provider specific switches",
v => { ProviderSwitches = v; }
},
{
"strip|strip-comments",
"Strip comments from the SQL scripts. Default is true. To disable, use --strip- or --strip-comments-",
v => { StripComments = v != null; }
},
{
"help|h|?",
"Displays this help menu.",
v => { ShowHelp = true; }
},
{
"transaction-per-session|tps",
"Overrides the transaction behavior of migrations, so that all migrations to be executed will run in one transaction.",
v => { TransactionPerSession = v != null; }
},
{
"allow-breaking-changes|abc",
"Allows execution of migrations marked as breaking changes.",
v => { AllowBreakingChange = v != null; }
},
{
"default-schema-name=",
"Set default schema name for the VersionInfo table and the migrations.",
v => { DefaultSchemaName = v; }
},
};
try
{
optionSet.Parse(args);
}
catch (OptionException e)
{
AsError(() => System.Console.Error.WriteException(e));
System.Console.WriteLine(@"Try 'migrate --help' for more information.");
return 2;
}
if (string.IsNullOrEmpty(Task))
{
Task = "migrate";
}
if (!ValidateArguments(optionSet))
{
return 1;
}
if (ShowHelp)
{
DisplayHelp(optionSet);
return 0;
}
return ExecuteMigrations();
}
catch (MissingMigrationsException ex)
{
AsError(() => System.Console.Error.WriteException(ex));
return 6;
}
catch (RunnerException ex)
{
AsError(() => System.Console.Error.WriteException(ex));
return 5;
}
catch (FluentMigratorException ex)
{
AsError(() => System.Console.Error.WriteException(ex));
return 4;
}
catch (Exception ex)
{
AsError(() => System.Console.Error.WriteException(ex));
return 3;
}
}
private bool ValidateArguments(OptionSet optionSet)
{
if (string.IsNullOrEmpty(TargetAssembly))
{
DisplayHelp(optionSet, "Please enter the path of the assembly containing migrations you want to execute.");
return false;
}
if (string.IsNullOrEmpty(ProcessorType))
{
DisplayHelp(optionSet, "Please enter the kind of database you are migrating against.");
return false;
}
return true;
}
private void DisplayHelp(OptionSet optionSet, string validationErrorMessage)
{
System.Console.ForegroundColor = ConsoleColor.Yellow;
System.Console.WriteLine(validationErrorMessage);
System.Console.ResetColor();
DisplayHelp(optionSet);
}
private void DisplayHelp(OptionSet p)
{
System.Console.WriteLine(@"Usage:");
System.Console.WriteLine(@" migrate [OPTIONS]");
System.Console.WriteLine(@"Example:");
System.Console.WriteLine(@" migrate -a bin\debug\MyMigrations.dll -db SqlServer2008 -conn ""SEE_BELOW"" -profile ""Debug""");
System.Console.WriteLine(@" ");
System.Console.WriteLine(@"Boolean options/flags (those without '=' or ':' in the option format string)");
System.Console.WriteLine(@"are explicitly enabled if they are followed with '+', and explicitly");
System.Console.WriteLine(@"disabled if they are followed with '-'.");
System.Console.Out.WriteHorizontalRuler();
System.Console.WriteLine(@"Example Connection Strings:");
System.Console.WriteLine(@" MySql: Data Source=172.0.0.1;Database=Foo;User Id=USERNAME;Password=BLAH");
System.Console.WriteLine(@" Oracle: Server=172.0.0.1;Database=Foo;Uid=USERNAME;Pwd=BLAH");
System.Console.WriteLine(@" SqlLite: Data Source=:memory:");
System.Console.WriteLine(@" SqlServer: server=127.0.0.1;database=Foo;user id=USERNAME;password=BLAH");
System.Console.WriteLine(@" server=.\SQLExpress;database=Foo;trusted_connection=true");
System.Console.WriteLine(@" ");
System.Console.WriteLine(@"OR use a named connection string from the machine.config:");
System.Console.WriteLine(@" migrate -a bin\debug\MyMigrations.dll -db SqlServer2008 -conn ""namedConnection"" -profile ""Debug""");
System.Console.Out.WriteHorizontalRuler();
System.Console.WriteLine(@"Options:");
p.WriteOptionDescriptions(System.Console.Out);
}
private bool ExecutingAgainstMsSql => ProcessorType.StartsWith("SqlServer", StringComparison.InvariantCultureIgnoreCase);
private int ExecuteMigrations()
{
var conventionSet = new DefaultConventionSet(DefaultSchemaName, WorkingDirectory);
var services = CreateCoreServices()
.Configure<FluentMigratorLoggerOptions>(
opt =>
{
opt.ShowElapsedTime = Verbose;
opt.ShowSql = Verbose;
})
.AddSingleton<IConventionSet>(conventionSet)
.Configure<SelectingProcessorAccessorOptions>(opt => opt.ProcessorId = ProcessorType)
.Configure<AssemblySourceOptions>(opt => opt.AssemblyNames = new[] { TargetAssembly })
#pragma warning disable 612
.Configure<AppConfigConnectionStringAccessorOptions>(
opt => opt.ConnectionStringConfigPath = ConnectionStringConfigPath)
#pragma warning restore 612
.Configure<TypeFilterOptions>(
opt =>
{
opt.Namespace = Namespace;
opt.NestedNamespaces = NestedNamespaces;
})
.Configure<RunnerOptions>(
opt =>
{
opt.Task = Task;
opt.Version = Version;
opt.StartVersion = StartVersion;
opt.NoConnection = NoConnection;
opt.Steps = Steps;
opt.Profile = Profile;
opt.Tags = Tags.ToArray();
opt.TransactionPerSession = TransactionPerSession;
opt.AllowBreakingChange = AllowBreakingChange;
opt.IncludeUntaggedMaintenances = IncludeUntaggedMaintenances;
opt.IncludeUntaggedMigrations = IncludeUntaggedMigrations;
})
.Configure<ProcessorOptions>(
opt =>
{
opt.ConnectionString = Connection;
opt.PreviewOnly = PreviewOnly;
opt.ProviderSwitches = ProviderSwitches;
opt.StripComments = StripComments;
opt.Timeout = Timeout == null ? null : (TimeSpan?) TimeSpan.FromSeconds(Timeout.Value);
});
if (StopOnError)
{
services
.AddSingleton<ILoggerProvider, StopOnErrorLoggerProvider>();
}
else
{
services
.AddSingleton<ILoggerProvider, FluentMigratorConsoleLoggerProvider>();
}
if (Output)
{
services
.Configure<LogFileFluentMigratorLoggerOptions>(
opt =>
{
opt.ShowSql = true;
opt.OutputFileName = OutputFilename;
opt.OutputGoBetweenStatements = ExecutingAgainstMsSql;
opt.OutputSemicolonDelimiter = OutputSemicolonDelimiter;
})
.AddSingleton<ILoggerProvider, LogFileFluentMigratorLoggerProvider>();
}
using (var serviceProvider = services.BuildServiceProvider(validateScopes: false))
{
var executor = serviceProvider.GetRequiredService<TaskExecutor>();
executor.Execute();
}
return 0;
}
private static IServiceCollection CreateCoreServices()
{
var services = new ServiceCollection()
.AddFluentMigratorCore()
.ConfigureRunner(
builder => builder
.AddDb2()
.AddDb2ISeries()
.AddDotConnectOracle()
.AddDotConnectOracle12C()
.AddFirebird()
.AddHana()
.AddMySql4()
.AddMySql5()
.AddOracle()
.AddOracle12C()
.AddOracleManaged()
.AddOracle12CManaged()
.AddPostgres()
.AddPostgres92()
.AddRedshift()
.AddSnowflake()
.AddSQLite()
.AddSqlServer()
.AddSqlServer2000()
.AddSqlServer2005()
.AddSqlServer2008()
.AddSqlServer2012()
.AddSqlServer2014()
.AddSqlServer2016()
);
return services;
}
}
}