forked from bittercoder/Migrator.NET
-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathMigrator.cs
More file actions
350 lines (311 loc) · 15.7 KB
/
Copy pathMigrator.cs
File metadata and controls
350 lines (311 loc) · 15.7 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
#region License
//The contents of this file are subject to the Mozilla Public License
//Version 1.1 (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.mozilla.org/MPL/
//Software distributed under the License is distributed on an "AS IS"
//basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//License for the specific language governing rights and limitations
//under the License.
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using DotNetProjects.Migrator.Framework;
using DotNetProjects.Migrator.Framework.Loggers;
using DotNetProjects.Migrator.Providers;
namespace DotNetProjects.Migrator;
/// <summary>
/// Migrations mediator.
/// </summary>
public class Migrator
{
public RunnerOptions Options { get; init; } = new();
private readonly MigrationLoader _migrationLoader;
private readonly ITransformationProvider _provider;
private string[] _args;
protected bool _dryrun;
private ILogger _logger = new Logger(false);
public Migrator(ProviderTypes provider, string connectionString, string defaultSchema, Assembly migrationAssembly)
: this(provider, connectionString, defaultSchema, migrationAssembly, false)
{
}
public Migrator(ProviderTypes provider, string connectionString, string defaultSchema, params Type[] migrationTypes)
: this(provider, connectionString, defaultSchema, false, migrationTypes)
{
}
public Migrator(ProviderTypes provider, string connectionString, string defaultSchema, Assembly migrationAssembly, bool trace)
: this(ProviderFactory.Create(provider, connectionString, defaultSchema), migrationAssembly, trace)
{
}
public Migrator(ProviderTypes provider, string connectionString, string defaultSchema, bool trace, params Type[] migrationTypes)
: this(ProviderFactory.Create(provider, connectionString, defaultSchema), trace, migrationTypes)
{
}
public Migrator(ProviderTypes provider, string connectionString, string defaultSchema, Assembly migrationAssembly, bool trace, ILogger logger)
: this(ProviderFactory.Create(provider, connectionString, defaultSchema), migrationAssembly, trace, logger)
{
}
public Migrator(ProviderTypes provider, string connectionString, string defaultSchema, bool trace, ILogger logger, params Type[] migrationTypes)
: this(ProviderFactory.Create(provider, connectionString, defaultSchema), trace, logger, migrationTypes)
{
}
public Migrator(ITransformationProvider provider, Assembly migrationAssembly, bool trace)
: this(provider, migrationAssembly, trace, new Logger(trace, new ConsoleWriter()))
{
}
public Migrator(ITransformationProvider provider, bool trace, params Type[] migrationTypes)
: this(provider, trace, new Logger(trace, new ConsoleWriter()), migrationTypes)
{
}
public Migrator(ITransformationProvider provider, Assembly migrationAssembly, bool trace, ILogger logger)
{
_provider = provider;
Logger = logger;
_migrationLoader = new MigrationLoader(provider, migrationAssembly, trace);
_migrationLoader.CheckForDuplicatedVersion();
}
public Migrator(ITransformationProvider provider, bool trace, ILogger logger, params Type[] migrationTypes)
{
_provider = provider;
Logger = logger;
_migrationLoader = new MigrationLoader(provider, trace, migrationTypes);
_migrationLoader.CheckForDuplicatedVersion();
}
public Migrator(ITransformationProvider provider, ILogger logger, MigrationLoader migrationLoader)
{
_provider = provider;
Logger = logger;
_migrationLoader = migrationLoader;
_migrationLoader.CheckForDuplicatedVersion();
}
public string[] args
{
get { return _args; }
set { _args = value; }
}
/// <summary>
/// Returns registered migration <see cref="System.Type">types</see>.
/// </summary>
public List<Type> MigrationsTypes
{
get { return _migrationLoader.MigrationsTypes; }
}
/// <summary>
/// Set or get the Schema Info table name, where the migration applied are saved
/// Default is: SchemaInfo
/// </summary>
public string SchemaInfoTableName
{
get
{
return _provider.SchemaInfoTable;
}
set
{
_provider.SchemaInfoTable = value;
}
}
/// <summary>
/// Returns the current migrations applied to the database.
/// </summary>
public List<long> AppliedMigrations
{
get { return _provider.AppliedMigrations; }
}
/// <summary>
/// Get or set the event logger.
/// </summary>
public ILogger Logger
{
get { return _logger; }
set
{
_logger = value;
_provider.Logger = value;
}
}
public virtual bool DryRun
{
get { return _dryrun; }
set { _dryrun = value; }
}
public long AssemblyLastMigrationVersion
{
get { return _migrationLoader.LastVersion; }
}
public long? LastAppliedMigrationVersion
{
get
{
if (AppliedMigrations.Count() == 0)
{
return null;
}
return AppliedMigrations.Max();
}
}
/// <summary>
/// Run all migrations up to the latest. Make no changes to database if
/// dryrun is true.
/// </summary>
public void MigrateToLastVersion()
{
var versions = SelectedMigrationTypes.Select(MigrationLoader.GetMigrationVersion).ToArray();
if (versions.Length == 0 && Options.Profiles.Count == 0 &&
!_migrationLoader.AuxiliaryTypes.Any(t => t.GetCustomAttribute<MaintenanceAttribute>() is { } a && _migrationLoader.InScope(a.Scope)))
{
Logger.Warn("No migrations found for the effective scope.");
return;
}
MigrateTo(versions.DefaultIfEmpty(0).Max(), false, versions.Length == 0);
}
/// <summary>
/// Migrate the database to a specific version.
/// Runs all migration between the actual version and the
/// specified version.
/// If <c>version</c> is greater then the current version,
/// the <c>Up()</c> method will be invoked.
/// If <c>version</c> lower then the current version,
/// the <c>Down()</c> method of previous migration will be invoked.
/// If <c>dryrun</c> is set, don't write any changes to the database.
/// </summary>
/// <param name="version">The version that must became the current one</param>
private IEnumerable<Type> SelectedMigrationTypes => _migrationLoader.SelectedTypes.Where(t =>
{
if (Options.Tags.Count == 0) return true;
var tags = t.GetCustomAttribute<TagsAttribute>()?.Tags ?? Array.Empty<string>();
return Options.TagMatch == TagMatchMode.All ? Options.Tags.All(tags.Contains) : Options.Tags.Any(tags.Contains);
});
private IReadOnlyList<MigrationStep> CreatePlan(IEnumerable<long> applied, long version)
{
_migrationLoader.CheckForDuplicatedVersion();
var selected = SelectedMigrationTypes.Select(MigrationLoader.GetMigrationVersion).ToHashSet();
var known = _migrationLoader.GetAvailableMigrations().ToHashSet();
// Filtered migrations stay applied; unknown history must still fail a downgrade.
return MigrationPlanner.Create(selected, applied.Where(v => selected.Contains(v) || !known.Contains(v)), version);
}
public IReadOnlyList<MigrationStep> Plan(long version)
{
if (_provider is not IMigrationHistory history)
throw new NotSupportedException("Read-only planning requires IMigrationHistory on custom providers.");
return CreatePlan(history.ReadAppliedMigrations(), version);
}
public string PreviewSql(long version, ProviderTypes provider, bool allowLegacyBodies = false)
{
_migrationLoader.Activator = Options.Activator;
var plan = Plan(version);
var migrations = new List<(IMigration, bool)>();
void AddMaintenance(MaintenanceStage stage)
{
foreach (var type in _migrationLoader.AuxiliaryTypes.Where(t => t.GetCustomAttribute<MaintenanceAttribute>() is { } a && a.Stage == stage && _migrationLoader.InScope(a.Scope))
.OrderBy(t => t.GetCustomAttribute<MaintenanceAttribute>().Order).ThenBy(t => t.FullName, StringComparer.Ordinal))
migrations.Add((_migrationLoader.CreateInstance(type), true));
}
AddMaintenance(MaintenanceStage.BeforeRun);
foreach (var step in plan)
{
AddMaintenance(MaintenanceStage.BeforeMigration);
migrations.Add((_migrationLoader.GetMigration(step.Version), step.IsUp));
AddMaintenance(MaintenanceStage.AfterMigration);
}
foreach (var name in Options.Profiles)
if (!_migrationLoader.AuxiliaryTypes.Any(t => t.GetCustomAttribute<ProfileAttribute>() is { } a && a.Name == name && _migrationLoader.InScope(a.Scope)))
throw new MigrationException("Unknown profile: " + name);
foreach (var type in _migrationLoader.AuxiliaryTypes.Where(t => t.GetCustomAttribute<ProfileAttribute>() is { } a && Options.Profiles.Contains(a.Name) && _migrationLoader.InScope(a.Scope))
.OrderBy(t => t.GetCustomAttribute<ProfileAttribute>().Order).ThenBy(t => t.FullName, StringComparer.Ordinal))
migrations.Add((_migrationLoader.CreateInstance(type), true));
AddMaintenance(MaintenanceStage.AfterRun);
return MigrationSqlPreview.Generate(provider, migrations, allowLegacyBodies,
table => _provider.TableExists(table) ? _provider.GetColumns(table) : throw new MigrationException("Preview table does not exist: " + table));
}
public void MigrateTo(long version) => MigrateTo(version, false);
/// <summary>Run only downward steps; validate the target after acquiring the configured lock.</summary>
public void RollbackTo(long version) => MigrateTo(version, true);
private void MigrateTo(long version, bool downOnly, bool preserveVersion = false)
{
if (DryRun)
{
var preview = preserveVersion ? Array.Empty<MigrationStep>() : Plan(version);
if (downOnly && preview.Any(step => step.IsUp)) throw new MigrationException("Rollback cannot apply upward migrations.");
foreach (var step in preview)
if (step.IsUp) Logger.MigrateUp(step.Version, "Preview"); else Logger.MigrateDown(step.Version, "Preview");
return;
}
if (Options.LockTimeout < TimeSpan.Zero) throw new ArgumentOutOfRangeException(nameof(Options.LockTimeout));
var session = Options.TransactionMode == MigrationTransactionMode.WholeSession;
if (session && _provider.Dialect is not (Providers.Impl.SQLite.SQLiteDialect or Providers.Impl.PostgreSQL.PostgreSQLDialect or Providers.Impl.SqlServer.SqlServerDialect))
throw new UnsupportedMigrationFeatureException("Whole-session transactions require a verified transactional DDL provider (SQLite, PostgreSQL or SQL Server).");
_migrationLoader.Activator = Options.Activator;
IDisposable AcquireLock()
{
try { return Options.Lock?.Acquire(_provider, (_provider as IMigrationHistory)?.Scope, Options.LockTimeout); }
catch (TimeoutException ex) { throw new MigrationLockTimeoutException(ex); }
}
var lease = AcquireLock();
Exception failure = null;
try
{
(_provider as IMigrationHistory)?.InvalidateHistory();
var history = new List<long>(_provider.AppliedMigrations);
var initialHistory = new List<long>(history);
if (preserveVersion) version = history.DefaultIfEmpty(0).Max();
var plan = CreatePlan(history, version);
if (downOnly && (version >= history.DefaultIfEmpty(0).Max() || plan.Any(step => step.IsUp)))
throw new MigrationException("Rollback requires a lower target and cannot apply upward migrations.");
var profiles = _migrationLoader.AuxiliaryTypes.Where(t => t.GetCustomAttribute<ProfileAttribute>() is { } p && Options.Profiles.Contains(p.Name) && _migrationLoader.InScope(p.Scope))
.OrderBy(t => t.GetCustomAttribute<ProfileAttribute>().Order).ThenBy(t => t.FullName, StringComparer.Ordinal).ToArray();
foreach (var name in Options.Profiles)
if (!profiles.Any(t => t.GetCustomAttribute<ProfileAttribute>().Name == name)) throw new MigrationException("Unknown profile: " + name);
var afterCommit = new List<Action>();
var firstRun = true;
void Execute(IMigration migration, MigrationStep step, bool record)
{
migration.Database = _provider;
if (firstRun) { migration.InitializeOnce(_args); firstRun = false; }
MigrationExecution.Execute(_provider, migration, step, Logger,
Options.TransactionMode == MigrationTransactionMode.PerMigration, session, record, !session);
if (session) afterCommit.Add(() => MigrationExecution.After(_provider, migration, step.IsUp));
}
void Maintenance(MaintenanceStage stage)
{
foreach (var type in _migrationLoader.AuxiliaryTypes.Where(t => t.GetCustomAttribute<MaintenanceAttribute>() is { } a && a.Stage == stage && _migrationLoader.InScope(a.Scope))
.OrderBy(t => t.GetCustomAttribute<MaintenanceAttribute>().Order).ThenBy(t => t.FullName, StringComparer.Ordinal))
Execute(_migrationLoader.CreateInstance(type), new MigrationStep(0, true), false);
}
void Run()
{
Maintenance(MaintenanceStage.BeforeRun);
foreach (var step in plan)
{
// A consolidated migration can record (or revert) other versions in its body.
// The plan is a snapshot; consult the provider's current, scope-specific history.
if (_provider.AppliedMigrations.Contains(step.Version) == step.IsUp) continue;
Maintenance(MaintenanceStage.BeforeMigration);
if (_provider.AppliedMigrations.Contains(step.Version) == step.IsUp) continue;
Execute(_migrationLoader.GetMigration(step.Version), step, true);
if (step.IsUp) history.Add(step.Version); else history.Remove(step.Version);
Maintenance(MaintenanceStage.AfterMigration);
}
foreach (var type in profiles) Execute(_migrationLoader.CreateInstance(type), new MigrationStep(0, true), false);
Maintenance(MaintenanceStage.AfterRun);
}
Logger.Started(new List<long>(initialHistory), version);
if (session) MigrationExecution.InTransaction(_provider, true, Run); else Run();
foreach (var callback in afterCommit) callback();
history.Sort();
Logger.Finished(new List<long>(initialHistory), version);
}
catch (Exception ex) { failure = ex; throw; }
finally
{
try { lease?.Dispose(); }
catch (Exception release)
{
if (failure == null) throw;
failure.Data["LockReleaseException"] = release;
}
}
}
}