Files
Anubis/src/Anubis.Server/DatabaseCommand.cs
T
Alby96andClaude Opus 5.5 1bc7e067fd
ci / test (postgres) (push) Successful in 4m50s
ci / test (sqlite) (push) Failing after 4m21s
ci / image (push) Skipped
Fase 10 — Nuova rotta: solo Docker (#184)
ADR 0021-0026 for the new course of 2 December 2026, roadmap of phases 10-24. The desktop application is gone: Docker is the only form, and outside a container the server runs only for development and tests with --data-dir, on this computer only. Lighter image (829 MB to 410 MB), rotating logs, backtest and learning as one-shot commands.

refs #113 #114 #115 #78 #80 #81

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Alberto Balbo <alberto.balbo.96@gmail.com>
2026-12-03 19:59:00 +01:00

72 lines
3.3 KiB
C#

using System.Collections;
using Anubis.Core;
using Anubis.Db;
using Anubis.Platform;
namespace Anubis.Server;
/// <summary>
/// Command line export and import (SPEC 5.4), the way to change engine: with the server stopped, export from the old
/// database, point the configuration to the new one, import. The import goes only into an empty database and renews
/// the installation fingerprint, so the old copy is no longer accepted (SPEC 15.1).
/// </summary>
internal static class DatabaseCommand
{
public static async Task<int> RunAsync(string[] args, TextWriter? output = null, IReadOnlyDictionary<string, string?>? environment = null)
{
output ??= Console.Out;
if (args.Length < 2 || args[0] is not ("export" or "import"))
{
await output.WriteLineAsync("Uso: anubis db export <cartella> | anubis db import <cartella> [--data-dir <cartella>, obbligatoria fuori dal container]").ConfigureAwait(false);
return 2;
}
environment ??= Environment.GetEnvironmentVariables().Cast<DictionaryEntry>().ToDictionary(e => (string)e.Key, e => e.Value as string, StringComparer.Ordinal);
var options = ServerOptions.Parse(args[2..], environment);
var config = options.LoadConfig(environment);
if (!config.IsComplete)
{
await output.WriteLineAsync("La configurazione d'avvio non è completa:").ConfigureAwait(false);
foreach (var problem in config.Problems)
{
await output.WriteLineAsync($"- {problem.Setting}: {problem.What} {problem.Remedy}").ConfigureAwait(false);
}
return 1;
}
Redactor.Register(config.Db!.Password);
var clock = new SystemClock();
var folder = Path.GetFullPath(args[1]);
try
{
if (args[0] == "import")
{
// Read before the database is opened: a wrong folder must not leave a new empty SQLite file behind.
await Archive.ReadIndexAsync(folder).ConfigureAwait(false);
config.Data.Ensure();
}
// Moving to SQLite, the file does not exist yet: the user asked for it by name, unlike a start (trap 20).
await using var db = Database.Open(config.Db!, allowCreate: args[0] == "import");
if (args[0] == "export")
{
var index = await Archive.ExportAsync(db, folder, clock).ConfigureAwait(false);
await output.WriteLineAsync($"Esportate {index.Tables.Sum(t => t.Count)} righe di {index.Tables.Count} tabelle in {folder}.").ConfigureAwait(false);
return 0;
}
var imported = await Archive.ImportAsync(db, folder, clock).ConfigureAwait(false);
await Installation.RenewAsync(db, config.Data, clock, "Archivio importato").ConfigureAwait(false);
await output.WriteLineAsync($"Importate {imported.Tables.Sum(t => t.Count)} righe di {imported.Tables.Count} tabelle: conteggi e totali coincidono. " +
"Al prossimo avvio il bot riparte protetto.").ConfigureAwait(false);
return 0;
}
catch (Exception ex) when (Forms.IsArchiveProblem(ex))
{
await output.WriteLineAsync("Non riuscito: " + Redactor.Redact(ex.Message)).ConfigureAwait(false);
return 1;
}
}
}