using System; using System.CommandLine; using Microsoft.Extensions.Logging; using Migrato.Cli; using Migrato.Core; using Migrato.Sqlite; using Spectre.Console; namespace Migrato.Cli.Commands; /// /// Shows the applied/pending status of every migration /// 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()); 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; } }