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

@ -65,7 +65,6 @@ public class Configuration
Console.WriteLine(backup.ToString());
}
}
}
public List<BackupCombination> GetBackupCombinations()
@ -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
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}");
@ -248,6 +252,7 @@ void UpdateDb(Configuration dbConfiguration)
{
update.Enabled = false;
}
Console.WriteLine($"------ MySql Data upgrade name: {update.Name} finished! -----");
}
catch (Exception e)

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));
}
}