Something went wrong. Try again.
A database migration tool for .NET
Something went wrong. Try again.
2.2 kB · 69 lines
C#
at develop
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970using System;using System.CommandLine;using Microsoft.Extensions.Logging;using Migrato.Cli;using Migrato.Core;using Migrato.Sqlite;using Spectre.Console;
namespace Migrato.Cli.Commands;
/// <summary>/// Shows the applied/pending status of every migration/// </summary>internal static class StatusCommand{ public static Command Build() { var command = new Command("status", "Shows migration status") { MigrationOptions.Dir, MigrationOptions.Db, MigrationOptions.Verbose };
command.SetAction(parseResult => { var dir = parseResult.GetValue(MigrationOptions.Dir)!; var db = parseResult.GetValue(MigrationOptions.Db)!; var verbose = parseResult.GetValue(MigrationOptions.Verbose);
try { var migrations = MigrationLoader.LoadFromDirectory(dir);
IMigrationProvider provider = new SqliteMigrationProvider(); using var connection = provider.CreateConnection(db);
using var loggerFactory = Logging.CreateFactory(verbose); var runner = new MigrationRunner(provider, loggerFactory.CreateLogger<MigrationRunner>()); var status = runner.GetStatus(connection, migrations);
var table = new Table(); table.AddColumn("Version"); table.AddColumn("Name"); table.AddColumn("Status"); table.AddColumn("Applied At");
foreach (var entry in status) { table.AddRow( entry.Version.ToString(), entry.Name.EscapeMarkup(), entry.IsApplied ? "[green]Applied[/]" : "[yellow]Pending[/]", entry.AppliedAt?.ToString("u") ?? "-"); }
AnsiConsole.Write(table); return 0; } catch (Exception ex) { AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}"); return 1; } });
return command; }}