[feat:] added json schema migration (ai slop, since this would be boring to implement) (untested)

This commit is contained in:
2026-07-25 19:40:40 +02:00
parent 22130bcb41
commit 05caa559b4
2 changed files with 235 additions and 3 deletions
+54 -3
View File
@@ -2,12 +2,19 @@ using System;
using System.Collections.Generic;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Logof_Client;
public class Settings
{
public static Settings _instance = new();
/// <summary>
/// Schema version - increment this when you make breaking changes to the Settings structure
/// </summary>
public int schemaVersion { get; set; } = SchemaMigration.CURRENT_SCHEMA_VERSION;
public AddressSets addressSets = new();
public Customers customers = new();
public PdfExportSettings pdfExport { get; set; } = new();
@@ -27,7 +34,9 @@ public class Settings
Directory.CreateDirectory(Global._instance.config_path);
// if (!string.IsNullOrEmpty(Global._instance.config_path)) _instance.settingsPath = Global._instance.config_path;
var json = JsonConvert.SerializeObject(_instance);
// Always save with current schema version
_instance.schemaVersion = SchemaMigration.CURRENT_SCHEMA_VERSION;
var json = JsonConvert.SerializeObject(_instance, Formatting.Indented);
File.WriteAllText(Path.Combine(Global._instance.config_path,"config.json"), json);
}
@@ -38,7 +47,24 @@ public class Settings
try
{
var contents = File.ReadAllText(Path.Combine(Global._instance.config_path, "config.json"));
_instance = JsonConvert.DeserializeObject<Settings>(contents);
// First, parse as JObject to check schema version
var jsonObject = JObject.Parse(contents);
int? loadedVersion = jsonObject["schemaVersion"]?.Value<int>();
// Apply migrations if needed
jsonObject = SchemaMigration.MigrateIfNeeded(jsonObject, loadedVersion);
// Now deserialize the migrated JSON
_instance = jsonObject.ToObject<Settings>();
// Save after migration to persist the upgraded schema
if (loadedVersion != SchemaMigration.CURRENT_SCHEMA_VERSION)
{
Logger.Log("Settings file was migrated to new schema version and saved.", Logger.LogType.Info);
Save();
}
MainWindow._instance.RefreshCustomerItems();
}
catch (Exception ex)
@@ -88,6 +114,11 @@ public class Global
{
public static Global _instance;
/// <summary>
/// Schema version - increment this when you make breaking changes to the Global structure
/// </summary>
public int schemaVersion { get; set; } = SchemaMigration.CURRENT_SCHEMA_VERSION;
public Global()
{
_instance = this;
@@ -116,6 +147,9 @@ public class Global
"logofclient")))
Directory.CreateDirectory(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
"logofclient"));
// Always save with current schema version
_instance.schemaVersion = SchemaMigration.CURRENT_SCHEMA_VERSION;
var json = JsonConvert.SerializeObject(_instance, Formatting.Indented);
File.WriteAllText(
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "logofclient",
@@ -135,7 +169,24 @@ public class Global
var contents = File.ReadAllText(Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "logofclient",
"global.config"));
_instance = JsonConvert.DeserializeObject<Global>(contents) ?? new Global();
// First, parse as JObject to check schema version
var jsonObject = JObject.Parse(contents);
int? loadedVersion = jsonObject["schemaVersion"]?.Value<int>();
// Apply migrations if needed
jsonObject = SchemaMigration.MigrateIfNeeded(jsonObject, loadedVersion);
// Now deserialize the migrated JSON
_instance = jsonObject.ToObject<Global>() ?? new Global();
// Save after migration to persist the upgraded schema
if (loadedVersion != SchemaMigration.CURRENT_SCHEMA_VERSION)
{
Logger.Log("Global settings file was migrated to new schema version and saved.", Logger.LogType.Info);
Save();
}
_instance.NormalizePaths();
}
catch (Exception ex)