-
-
Notifications
You must be signed in to change notification settings - Fork 90
feat: Refactor MigrationManager to accept migrations as a parameter #503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| global using System; | ||
| global using System.IO; | ||
| global using System.Threading.Tasks; | ||
| global using LinkDotNet.Blog.UpgradeAssistant; | ||
| global using LinkDotNet.Blog.UpgradeAssistant.Migrations; | ||
| global using Shouldly; | ||
| global using Xunit; |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,14 @@ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| namespace LinkDotNet.Blog.UpgradeAssistant; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static class MigrationDiscovery | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public static IReadOnlyList<IMigration> DiscoverAll() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return typeof(IMigration).Assembly | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .GetTypes() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| .Where(t => t is { IsClass: true, IsAbstract: false } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| && typeof(IMigration).IsAssignableFrom(t)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| && typeof(IMigration).IsAssignableFrom(t)) | |
| && typeof(IMigration).IsAssignableFrom(t)) | |
| .OrderBy(t => t.FullName) |
Copilot
AI
Mar 11, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DiscoverAll() instantiates every IMigration type via reflection without filtering for a public parameterless constructor. If an IMigration implementation is internal or requires ctor parameters, Activator.CreateInstance will throw at runtime and the tool won’t start. Consider filtering to types with an accessible parameterless constructor (or using the overload that allows non-public) and surfacing a clear error message when instantiation fails.
| return typeof(IMigration).Assembly | |
| .GetTypes() | |
| .Where(t => t is { IsClass: true, IsAbstract: false } | |
| && typeof(IMigration).IsAssignableFrom(t)) | |
| .Select(t => (IMigration)Activator.CreateInstance(t)!) | |
| .ToList(); | |
| var migrationTypes = typeof(IMigration).Assembly | |
| .GetTypes() | |
| .Where(t => t is { IsClass: true, IsAbstract: false } | |
| && typeof(IMigration).IsAssignableFrom(t) | |
| && t.GetConstructor(Type.EmptyTypes) is not null) | |
| .ToList(); | |
| var migrations = new List<IMigration>(migrationTypes.Count); | |
| foreach (var type in migrationTypes) | |
| { | |
| try | |
| { | |
| var instance = Activator.CreateInstance(type); | |
| if (instance is IMigration migration) | |
| { | |
| migrations.Add(migration); | |
| } | |
| else | |
| { | |
| throw new InvalidOperationException( | |
| $"Type '{type.FullName}' was expected to implement '{typeof(IMigration).FullName}' but did not."); | |
| } | |
| } | |
| catch (Exception ex) | |
| { | |
| throw new InvalidOperationException( | |
| $"Failed to create an instance of migration type '{type.FullName}'. " + | |
| "Ensure it has a public parameterless constructor and that its constructor does not throw.", | |
| ex); | |
| } | |
| } | |
| return migrations; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| using System.Globalization; | ||
| using System.Text.Json; | ||
| using System.Text.Json.Nodes; | ||
| using LinkDotNet.Blog.UpgradeAssistant.Migrations; | ||
|
|
||
| namespace LinkDotNet.Blog.UpgradeAssistant; | ||
|
|
||
|
|
@@ -10,13 +9,9 @@ public sealed class MigrationManager | |
| private readonly List<IMigration> _migrations; | ||
| private readonly string _currentVersion; | ||
|
|
||
| public MigrationManager() | ||
| public MigrationManager(IEnumerable<IMigration> migrations) | ||
| { | ||
| _migrations = new List<IMigration> | ||
| { | ||
| new Migration11To12() | ||
| }; | ||
|
|
||
| _migrations = migrations.ToList(); | ||
| _currentVersion = DetermineCurrentVersionFromMigrations(); | ||
|
||
| } | ||
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DiscoverAll()currently returns migrations in whatever orderAssembly.GetTypes()happens to yield. If you ever end up with multiple migrations that share the sameFromVersion(or you rely on deterministic ordering for display/logging), this can lead to nondeterministic behavior across runtimes/builds. Consider ordering the discovered migrations deterministically (e.g., by parsedFromVersionthenToVersion, or by an explicitOrderproperty).