adds updateDb command
All checks were successful
PiwigoDirectorySync/pipeline/head This commit looks good

This commit is contained in:
Philipp Häfelfinger 2023-09-10 22:57:42 +02:00
parent 551ef82c01
commit d1290f73c1
3 changed files with 52 additions and 1 deletions

View File

@ -0,0 +1,44 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using PiwigoDirectorySync.Infrastructure;
using PiwigoDirectorySync.Persistence;
using Spectre.Console;
using Spectre.Console.Cli;
namespace PiwigoDirectorySync.Commands;
internal class UpdateDatabaseCommand : CancellableAsyncCommand<UpdateDatabaseCommand.UpdateDatabaseSettings>
{
private readonly ILogger<UpdateDatabaseCommand> _logger;
private readonly PersistenceContext _persistenceContext;
public UpdateDatabaseCommand(ILogger<UpdateDatabaseCommand> logger, PersistenceContext persistenceContext)
{
_logger = logger;
_persistenceContext = persistenceContext;
}
protected override async Task<int> ExecuteAsync(CommandContext context, UpdateDatabaseSettings settings, CancellationToken cancellation)
{
try
{
await AnsiConsole.Status()
.Spinner(Spinner.Known.Star)
.SpinnerStyle(Style.Parse("green bold"))
.StartAsync("[orange3]Updating database[/]", async _ => { await _persistenceContext.Database.MigrateAsync(cancellation); });
AnsiConsole.MarkupLine("[green]done[/]");
return 0;
}
catch (Exception ex)
{
_logger.LogCritical(ex, "Could not update database due to an error {ExMessage}", ex.Message);
AnsiConsole.WriteException(ex);
}
return -1;
}
internal class UpdateDatabaseSettings : CommandSettings
{
}
}

View File

@ -59,6 +59,7 @@ app.Configure(config =>
c.AddCommand<PiwigoUpdateCommand>("update"); c.AddCommand<PiwigoUpdateCommand>("update");
c.AddCommand<PiwigoRemoveCommand>("remove"); c.AddCommand<PiwigoRemoveCommand>("remove");
}); });
config.AddCommand<UpdateDatabaseCommand>("updateDb");
}); });
return await app.RunAsync(args); return await app.RunAsync(args);

View File

@ -42,6 +42,12 @@
"commandLineArgs": "piwigo list", "commandLineArgs": "piwigo list",
"environmentVariables": { "environmentVariables": {
} }
},
"UpdateDb": {
"commandName": "Project",
"commandLineArgs": "updateDb",
"environmentVariables": {
}
} }
} }
} }