freeso/TSOClient/FSO.Server/ToolInitDatabase.cs
riperiperi b57ebbd141 Cleanup: Remove most unused usings and MPL per-file licenses
MPL per-file was used in older project files, but should apply to the whole project, so having it there was redundant.
2023-11-25 03:45:29 +00:00

88 lines
3.2 KiB
C#

using FSO.Server.Database.DA;
using FSO.Server.Database.Management;
using NLog;
using System;
namespace FSO.Server
{
/// <summary>
/// This tool will install and update the SQL database.
///
/// It does this by reading the manifest.json file from the Scripts folder and compares it
/// to whats inside the fso_db_changes table.
///
/// </summary>
public class ToolInitDatabase : ITool
{
private static Logger LOG = LogManager.GetCurrentClassLogger();
private IDAFactory DAFactory;
public ToolInitDatabase(DatabaseInitOptions options, IDAFactory factory)
{
this.DAFactory = factory;
}
public int Run()
{
Console.WriteLine("Starting database init");
using (var da = (SqlDA)DAFactory.Get())
{
var changeTool = new DbChangeTool(da.Context);
var changes = changeTool.GetChanges();
foreach(var change in changes)
{
if(change.Status == DbChangeScriptStatus.MODIFIED && change.Idempotent == false)
{
Console.WriteLine(change.Status + " - " + change.ScriptFilename + " (Cant update, fix manually)");
}
else
{
Console.WriteLine(change.Status + " - " + change.ScriptFilename);
}
}
Console.WriteLine();
Console.WriteLine("Apply changes (y|n)? Make sure you have backed up your database first");
var input = Console.ReadLine().Trim();
if (input.StartsWith("y") || input.StartsWith("r"))
{
//Repair just updates fso_db_changes to latest
var repair = input.StartsWith("r");
Console.WriteLine("Applying changes");
foreach (var change in changes)
{
if(change.Status == DbChangeScriptStatus.FORCE_REINSTALL ||
change.Status == DbChangeScriptStatus.NOT_INSTALLED ||
(change.Status == DbChangeScriptStatus.MODIFIED && change.Idempotent))
{
try {
changeTool.ApplyChange(change, repair);
}catch(DbMigrateException e)
{
Console.Error.WriteLine("Error applying change: " + change.ScriptFilename);
Console.Error.WriteLine("\"" + e.Message + "\"");
Console.WriteLine("Would you like to continue? (y|n)?");
input = Console.ReadLine().Trim();
if (!input.StartsWith("y"))
{
return -1;
}
}
}
}
}
else
{
Console.WriteLine("No changes applied");
}
}
return 0;
}
}
}