Something went wrong. Try again.
A database migration tool for .NET
Something went wrong. Try again.
1.9 kB · 60 lines
C#
at develop
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061using 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>/// Reverts the most recently applied migration/// </summary>internal static class DownCommand{ public static Command Build() { var command = new Command("down", "Reverts the most recently applied migration") { 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 reverted = runner.MigrateDown(connection, migrations);
if (reverted is null) { AnsiConsole.MarkupLine("[yellow]No applied migrations to revert.[/]"); return 0; }
AnsiConsole.MarkupLineInterpolated($"[green]Reverted[/] {reverted.Version}_{reverted.Name}"); return 0; } catch (Exception ex) { AnsiConsole.MarkupLineInterpolated($"[red]Error:[/] {ex.Message}"); return 1; } });
return command; }}