Can I use ListMigrations to show a list of Migrations on an MVC Page? #1439
-
Hello, Can I use ListMigrations to show a list of available migrations to the user on an ASP.NET Core MVC Website (using .NET Core 5.0)?
I believe I would need some way to fetch the output of ListMigrations and show pass it to the view. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
@asifshakir MigrationRunner has a public constructor that takes an ILogger instance: I could envision a "logger" component that has a public property which is the data structure for your ASP.NET MVC view. However, a cleaner solution would probably be to modularize MigrationRunner. I would start with just what you need modularized, e.g., fluentmigrator/src/FluentMigrator.Runner/MigrationRunner.cs Lines 912 to 937 in 7033cdf Something like the following should be a self-contained Service: // Assume MigrationWithStatus is a DTO.
// Therefore, MigrationWithStatus does not contain any methods or transitive references to objects that contain methods.
public ICollection<MigrationWithStatus> GetMigrationsWithStatus()
{
var currentVersionInfo = VersionLoader.VersionInfo;
var currentVersion = currentVersionInfo.Latest();
var migrationsWithStatus = MigrationLoader
.LoadMigrations()
.Select(migration => {
var migrationName = migration.Value.GetName();
var status = GetStatus(migration , currentVersion);
var statusStrings = GetStatusStrings(status);
var statusString = string.Join(",", statusStrings);
return
new MigrationWithStatus() {
Name = migrationName,
Status = status,
StatusDetails = GetStatusStrings(status),
IsCurrent = (status & MigrationStatus.AppliedMask) == MigrationStatus.Current,
IsBreaking = (status & MigrationStatus.Breaking) == MigrationStatus.Breaking,
Message = $"{migrationName}{(string.IsNullOrEmpty(statusString) ? string.Empty : $" ({statusString})")}",
};
})
.ToList();
return migrationsWithStatus;
} You would need to basically take my proof-of-concept above and,
|
Beta Was this translation helpful? Give feedback.
@asifshakir MigrationRunner has a public constructor that takes an ILogger instance:
fluentmigrator/src/FluentMigrator.Runner/MigrationRunner.cs
Line 202 in 7033cdf
I could envision a "logger" component that has a public property which is the data structure for your ASP.NET MVC view.
However, a cleaner solution would probably be to modularize MigrationRunner. I would start with just what you need modularized, e.g.,
fluentmigrator/src/FluentMigrator.Runner/MigrationRunner.cs
Lines 912 to 937 in 7033cdf