Implement conditional config saving based on potential changes

Added a check for potential modifications to determine whether the configuration should be saved. Introduced a new method `HasPotentialModification` in the `Configuration` class to identify one-time-enabled items and update the saving logic accordingly. Cleaned up and refactored related code for better readability and maintainability.
This commit is contained in:
Michal Buzik 2024-10-18 08:18:38 +02:00
parent cdfa50e1ec
commit 117d6f5162
4 changed files with 84 additions and 23 deletions

View File

@ -8,20 +8,20 @@ public class Configuration
{
public const string ConfigDir = "config";
public const string FileName = "configuration.json";
public string? PathToDumpMysql { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Boolean? WaitOnEndCmdOpen { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Boolean? PrintConfigurationEnabled { get; set; }
public List<DbConnection>? DbConnectionStrings { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public List<Update>? UpdateString { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public List<Backup>? BackupDB { get; set; }
@ -30,7 +30,7 @@ public class Configuration
{
return JsonSerializer.Deserialize<Configuration>(jsonString);
}
// Serialize method for saving
public string SerializeToJson()
{
@ -65,7 +65,6 @@ public class Configuration
Console.WriteLine(backup.ToString());
}
}
}
public List<BackupCombination> GetBackupCombinations()
@ -80,7 +79,7 @@ public class Configuration
{
continue;
}
combinations.AddRange(GetBackupCombinations(BackupDB, dbConnection));
}
}
@ -96,7 +95,7 @@ public class Configuration
{
foreach (Backup backup in backups)
{
if (!(backup.Enabled??true) || backup.DbConnectionStringsName != dbConnection.Name )
if (!(backup.Enabled ?? true) || backup.DbConnectionStringsName != dbConnection.Name)
{
continue;
}
@ -117,10 +116,62 @@ public class Configuration
});
}
}
}
}
return combinations;
}
public bool HasPotentialModification()
{
bool returnPotentialModification = false;
if (DbConnectionStrings != null)
{
foreach (DbConnection dbConnection in DbConnectionStrings)
{
if (dbConnection.isOneTimeEnabled())
{
return true;
}
}
}
if (UpdateString != null)
{
foreach (Update update in UpdateString)
{
if (update.isOneTimeEnabled())
{
return true;
}
}
}
if (BackupDB != null)
{
foreach (Backup backup in BackupDB)
{
if (backup.isOneTimeEnabled())
{
return true;
}
if (backup.StoreDirPaths != null)
{
foreach (PathWrapper path in backup.StoreDirPaths)
{
if (path.isOneTimeEnabled())
{
return true;
}
}
}
}
}
return returnPotentialModification;
}
}

View File

@ -16,8 +16,11 @@ if (configuration == null)
return -1;
}
bool storeConfigPotentialChanges = configuration.HasPotentialModification();
//update part
if (configuration.UpdateString != null){
if (configuration.UpdateString != null)
{
try
{
UpdateDb(configuration);
@ -47,7 +50,10 @@ foreach (BackupCombination combination in backupCombinations)
//Update one time config
//store configuration
SaveConfiguration(configuration);
if (storeConfigPotentialChanges)
{
SaveConfiguration(configuration);
}
//final info
Console.WriteLine("Program finished correctly!!");
@ -64,8 +70,6 @@ return 0;
Configuration? GetConfiguration()
{
try
{
Console.WriteLine($"---- program started path: {Environment.CurrentDirectory}");
@ -100,7 +104,7 @@ void SaveConfiguration(Configuration configuration)
{
// Serialize the configuration back to JSON
string jsonString = configuration.SerializeToJson();
string fileNamePath = ConfigurationFileNamePath();
Console.WriteLine($"- full config file path: {fileNamePath}");
@ -200,10 +204,10 @@ void BackupDatabase(BackupCombination backupCombination)
}
process.Close();
//Deactivate if only one time possible run
if ((backupCombination.Path.IsOneTimeOpaque ?? false) && (backupCombination.Path.Enabled??true))
if ((backupCombination.Path.IsOneTimeOpaque ?? false) && (backupCombination.Path.Enabled ?? true))
{
backupCombination.Path.Enabled = false;
}
@ -241,13 +245,14 @@ void UpdateDb(Configuration dbConfiguration)
int affectedItems = command.ExecuteNonQuery();
Console.WriteLine($"Affected items: {affectedItems}");
connection.Close();
if (update.IsOneTimeOpaque ?? false)
{
update.Enabled = false;
}
Console.WriteLine($"------ MySql Data upgrade name: {update.Name} finished! -----");
}
catch (Exception e)
@ -263,9 +268,9 @@ string ConfigurationFileNamePath()
{
string s;
#if DEBUG
Console.WriteLine("-- DEV version of THE PROJECT -- ");
Console.WriteLine("-- DEV version of THE PROJECT -- ");
string projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;
s = Path.Combine(projectPath, Configuration.ConfigDir, Configuration.FileName);
#else
// For release mode, use an absolute path or the application base directory

View File

@ -6,7 +6,7 @@ public class Backup : StandardBehaviour
{
public string DbConnectionStringsName { get; set; }
public List<PathWrapper> StoreDirPaths { get; set; }
public List<PathWrapper>? StoreDirPaths { get; set; }
public override string ToString()
{

View File

@ -8,4 +8,9 @@ public class StandardBehaviour
public Boolean? Enabled { get; set; }
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
public Boolean? IsOneTimeOpaque { get; set; }
public bool isOneTimeEnabled()
{
return ((Enabled ?? true) && (IsOneTimeOpaque ?? false));
}
}