LANCommander/LANCommander.Server/Migrations/EncapsulateUserData.cs
Aaron Powell e2b2654a61 Adding SQLite database migration step
Created a new abstraction for migrations that have to run against the file system, so that we don't need to do the check in each migration.

Adding the default DB name to the settings so it's got a const value.

Updated the Settings migrator to check a few more places
2025-12-17 17:09:36 +11:00

64 lines
No EOL
2 KiB
C#

using LANCommander.SDK;
using LANCommander.SDK.Helpers;
using LANCommander.SDK.Migrations;
using Semver;
namespace LANCommander.Server.Migrations;
public class EncapsulateUserData(
ILogger<EncapsulateUserData> logger) : FileSystemMigration(logger)
{
public override SemVersion Version => new(2, 0, 0);
private string _oldConfigDirectory = string.Empty;
public override async Task ExecutePostMoveAsync()
{
var baseDirectory = Directory.GetCurrentDirectory();
if (DirectoryHelper.IsDirectoryWritable(baseDirectory))
_oldConfigDirectory = baseDirectory;
else
_oldConfigDirectory = AppPaths.GetAppDataPath();
MoveOldPath("Backups");
MoveOldPath("Media");
MoveOldPath("Saves");
MoveOldPath("Snippets");
MoveOldPath("Upload");
MoveOldPath("Uploads");
MoveOldPath("Servers");
MoveOldPath("Updates");
MoveOldPath("Launcher");
MoveOldPath("Logs");
}
private void MoveOldPath(string path)
{
try
{
var source = Path.Combine(_oldConfigDirectory, path);
var destination = AppPaths.GetConfigPath(path);
if (source == destination)
return;
logger.LogInformation($"Moving old config directory/file \"{path}\"");
if (Directory.Exists(source))
DirectoryHelper.MoveContents(source, destination);
else if (File.Exists(source))
{
if (!Directory.Exists(Path.GetDirectoryName(destination)))
Directory.CreateDirectory(Path.GetDirectoryName(destination));
File.Move(source, destination);
}
else
logger.LogInformation($"Skipping old directory/file \"{path}\" because it doesn't exist.");
}
catch (Exception ex)
{
logger.LogError(ex, "Error while moving old config directory/file");
}
}
}