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:
parent
cdfa50e1ec
commit
117d6f5162
@ -8,20 +8,20 @@ public class Configuration
|
|||||||
{
|
{
|
||||||
public const string ConfigDir = "config";
|
public const string ConfigDir = "config";
|
||||||
public const string FileName = "configuration.json";
|
public const string FileName = "configuration.json";
|
||||||
|
|
||||||
public string? PathToDumpMysql { get; set; }
|
public string? PathToDumpMysql { get; set; }
|
||||||
|
|
||||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||||
public Boolean? WaitOnEndCmdOpen { get; set; }
|
public Boolean? WaitOnEndCmdOpen { get; set; }
|
||||||
|
|
||||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||||
public Boolean? PrintConfigurationEnabled { get; set; }
|
public Boolean? PrintConfigurationEnabled { get; set; }
|
||||||
|
|
||||||
public List<DbConnection>? DbConnectionStrings { get; set; }
|
public List<DbConnection>? DbConnectionStrings { get; set; }
|
||||||
|
|
||||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||||
public List<Update>? UpdateString { get; set; }
|
public List<Update>? UpdateString { get; set; }
|
||||||
|
|
||||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||||
public List<Backup>? BackupDB { get; set; }
|
public List<Backup>? BackupDB { get; set; }
|
||||||
|
|
||||||
@ -30,7 +30,7 @@ public class Configuration
|
|||||||
{
|
{
|
||||||
return JsonSerializer.Deserialize<Configuration>(jsonString);
|
return JsonSerializer.Deserialize<Configuration>(jsonString);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serialize method for saving
|
// Serialize method for saving
|
||||||
public string SerializeToJson()
|
public string SerializeToJson()
|
||||||
{
|
{
|
||||||
@ -65,7 +65,6 @@ public class Configuration
|
|||||||
Console.WriteLine(backup.ToString());
|
Console.WriteLine(backup.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<BackupCombination> GetBackupCombinations()
|
public List<BackupCombination> GetBackupCombinations()
|
||||||
@ -80,7 +79,7 @@ public class Configuration
|
|||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
combinations.AddRange(GetBackupCombinations(BackupDB, dbConnection));
|
combinations.AddRange(GetBackupCombinations(BackupDB, dbConnection));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -96,7 +95,7 @@ public class Configuration
|
|||||||
{
|
{
|
||||||
foreach (Backup backup in backups)
|
foreach (Backup backup in backups)
|
||||||
{
|
{
|
||||||
if (!(backup.Enabled??true) || backup.DbConnectionStringsName != dbConnection.Name )
|
if (!(backup.Enabled ?? true) || backup.DbConnectionStringsName != dbConnection.Name)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
@ -117,10 +116,62 @@ public class Configuration
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return combinations;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@ -16,8 +16,11 @@ if (configuration == null)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool storeConfigPotentialChanges = configuration.HasPotentialModification();
|
||||||
|
|
||||||
//update part
|
//update part
|
||||||
if (configuration.UpdateString != null){
|
if (configuration.UpdateString != null)
|
||||||
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
UpdateDb(configuration);
|
UpdateDb(configuration);
|
||||||
@ -47,7 +50,10 @@ foreach (BackupCombination combination in backupCombinations)
|
|||||||
//Update one time config
|
//Update one time config
|
||||||
|
|
||||||
//store configuration
|
//store configuration
|
||||||
SaveConfiguration(configuration);
|
if (storeConfigPotentialChanges)
|
||||||
|
{
|
||||||
|
SaveConfiguration(configuration);
|
||||||
|
}
|
||||||
|
|
||||||
//final info
|
//final info
|
||||||
Console.WriteLine("Program finished correctly!!");
|
Console.WriteLine("Program finished correctly!!");
|
||||||
@ -64,8 +70,6 @@ return 0;
|
|||||||
|
|
||||||
Configuration? GetConfiguration()
|
Configuration? GetConfiguration()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Console.WriteLine($"---- program started path: {Environment.CurrentDirectory}");
|
Console.WriteLine($"---- program started path: {Environment.CurrentDirectory}");
|
||||||
@ -100,7 +104,7 @@ void SaveConfiguration(Configuration configuration)
|
|||||||
{
|
{
|
||||||
// Serialize the configuration back to JSON
|
// Serialize the configuration back to JSON
|
||||||
string jsonString = configuration.SerializeToJson();
|
string jsonString = configuration.SerializeToJson();
|
||||||
|
|
||||||
string fileNamePath = ConfigurationFileNamePath();
|
string fileNamePath = ConfigurationFileNamePath();
|
||||||
Console.WriteLine($"- full config file path: {fileNamePath}");
|
Console.WriteLine($"- full config file path: {fileNamePath}");
|
||||||
|
|
||||||
@ -200,10 +204,10 @@ void BackupDatabase(BackupCombination backupCombination)
|
|||||||
}
|
}
|
||||||
|
|
||||||
process.Close();
|
process.Close();
|
||||||
|
|
||||||
//Deactivate if only one time possible run
|
//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;
|
backupCombination.Path.Enabled = false;
|
||||||
}
|
}
|
||||||
@ -241,13 +245,14 @@ void UpdateDb(Configuration dbConfiguration)
|
|||||||
int affectedItems = command.ExecuteNonQuery();
|
int affectedItems = command.ExecuteNonQuery();
|
||||||
|
|
||||||
Console.WriteLine($"Affected items: {affectedItems}");
|
Console.WriteLine($"Affected items: {affectedItems}");
|
||||||
|
|
||||||
connection.Close();
|
connection.Close();
|
||||||
|
|
||||||
if (update.IsOneTimeOpaque ?? false)
|
if (update.IsOneTimeOpaque ?? false)
|
||||||
{
|
{
|
||||||
update.Enabled = false;
|
update.Enabled = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine($"------ MySql Data upgrade name: {update.Name} finished! -----");
|
Console.WriteLine($"------ MySql Data upgrade name: {update.Name} finished! -----");
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
@ -263,9 +268,9 @@ string ConfigurationFileNamePath()
|
|||||||
{
|
{
|
||||||
string s;
|
string s;
|
||||||
#if DEBUG
|
#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;
|
string projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.FullName;
|
||||||
|
|
||||||
s = Path.Combine(projectPath, Configuration.ConfigDir, Configuration.FileName);
|
s = Path.Combine(projectPath, Configuration.ConfigDir, Configuration.FileName);
|
||||||
#else
|
#else
|
||||||
// For release mode, use an absolute path or the application base directory
|
// For release mode, use an absolute path or the application base directory
|
||||||
|
|||||||
@ -6,7 +6,7 @@ public class Backup : StandardBehaviour
|
|||||||
{
|
{
|
||||||
|
|
||||||
public string DbConnectionStringsName { get; set; }
|
public string DbConnectionStringsName { get; set; }
|
||||||
public List<PathWrapper> StoreDirPaths { get; set; }
|
public List<PathWrapper>? StoreDirPaths { get; set; }
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
|
|||||||
@ -8,4 +8,9 @@ public class StandardBehaviour
|
|||||||
public Boolean? Enabled { get; set; }
|
public Boolean? Enabled { get; set; }
|
||||||
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
||||||
public Boolean? IsOneTimeOpaque { get; set; }
|
public Boolean? IsOneTimeOpaque { get; set; }
|
||||||
|
|
||||||
|
public bool isOneTimeEnabled()
|
||||||
|
{
|
||||||
|
return ((Enabled ?? true) && (IsOneTimeOpaque ?? false));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue
Block a user