Skip to content

Commit

Permalink
Fix for broken first run on fresh installs
Browse files Browse the repository at this point in the history
* Fix for broken first run on fresh installs
  • Loading branch information
michael-j-green authored Feb 10, 2024
1 parent 07c973c commit 111c501
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 35 deletions.
115 changes: 80 additions & 35 deletions gaseous-server/Classes/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,18 +205,25 @@ public static void InitSettings()

try
{
switch ((int)dataRow["ValueType"])
if (Database.schema_version >= 1016)
{
case 0:
default:
// value is a string
AppSettings.Add(SettingName, dataRow["Value"]);
break;

case 1:
// value is a datetime
AppSettings.Add(SettingName, dataRow["ValueDate"]);
break;
switch ((int)dataRow["ValueType"])
{
case 0:
default:
// value is a string
AppSettings.Add(SettingName, dataRow["Value"]);
break;

case 1:
// value is a datetime
AppSettings.Add(SettingName, dataRow["ValueDate"]);
break;
}
}
else
{
AppSettings.Add(SettingName, dataRow["Value"]);
}
}
catch (InvalidCastException castEx)
Expand Down Expand Up @@ -249,34 +256,58 @@ public static T ReadSetting<T>(string SettingName, T DefaultValue)
}
else
{

string sql = "SELECT Value, ValueDate FROM Settings WHERE Setting = @SettingName";
string sql;
Dictionary<string, object> dbDict = new Dictionary<string, object>
{
{ "SettingName", SettingName }
};
DataTable dbResponse;

try
{
Logging.Log(Logging.LogType.Debug, "Database", "Reading setting '" + SettingName + "'");
DataTable dbResponse = db.ExecuteCMD(sql, dbDict);
Type type = typeof(T);
if (dbResponse.Rows.Count == 0)

if (Database.schema_version >= 1016)
{
// no value with that name stored - respond with the default value
SetSetting<T>(SettingName, DefaultValue);
return DefaultValue;
sql = "SELECT Value, ValueDate FROM Settings WHERE Setting = @SettingName";

dbResponse = db.ExecuteCMD(sql, dbDict);
Type type = typeof(T);
if (dbResponse.Rows.Count == 0)
{
// no value with that name stored - respond with the default value
SetSetting<T>(SettingName, DefaultValue);
return DefaultValue;
}
else
{
if (type.ToString() == "System.DateTime")
{
AppSettings.Add(SettingName, (T)dbResponse.Rows[0]["ValueDate"]);
return (T)dbResponse.Rows[0]["ValueDate"];
}
else
{
AppSettings.Add(SettingName, (T)dbResponse.Rows[0]["Value"]);
return (T)dbResponse.Rows[0]["Value"];
}
}
}
else
{
if (type.ToString() == "System.DateTime")
sql = "SELECT Value FROM Settings WHERE Setting = @SettingName";

dbResponse = db.ExecuteCMD(sql, dbDict);
Type type = typeof(T);
if (dbResponse.Rows.Count == 0)
{
AppSettings.Add(SettingName, dbResponse.Rows[0]["ValueDate"]);
return (T)dbResponse.Rows[0]["ValueDate"];
// no value with that name stored - respond with the default value
SetSetting<T>(SettingName, DefaultValue);
return DefaultValue;
}
else
{
AppSettings.Add(SettingName, dbResponse.Rows[0]["Value"]);
AppSettings.Add(SettingName, (T)dbResponse.Rows[0]["Value"]);
return (T)dbResponse.Rows[0]["Value"];
}
}
Expand Down Expand Up @@ -317,27 +348,41 @@ public static T ReadSetting<T>(string SettingName, T DefaultValue)
public static void SetSetting<T>(string SettingName, T Value)
{
Database db = new Database(Database.databaseType.MySql, Config.DatabaseConfiguration.ConnectionString);
string sql = "REPLACE INTO Settings (Setting, ValueType, Value, ValueDate) VALUES (@SettingName, @ValueType, @Value, @ValueDate)";
string sql;
Dictionary<string, object> dbDict;
Type type = typeof(T);
if (type.ToString() == "System.DateTime")
if (Database.schema_version >= 1016)
{
dbDict = new Dictionary<string, object>
sql = "REPLACE INTO Settings (Setting, ValueType, Value, ValueDate) VALUES (@SettingName, @ValueType, @Value, @ValueDate)";
Type type = typeof(T);
if (type.ToString() == "System.DateTime")
{
{ "SettingName", SettingName },
{ "ValueType", 1 },
{ "Value", null },
{ "ValueDate", Value }
};
dbDict = new Dictionary<string, object>
{
{ "SettingName", SettingName },
{ "ValueType", 1 },
{ "Value", null },
{ "ValueDate", Value }
};
}
else
{
dbDict = new Dictionary<string, object>
{
{ "SettingName", SettingName },
{ "ValueType", 0 },
{ "Value", Value },
{ "ValueDate", null }
};
}
}
else
{
sql = "REPLACE INTO Settings (Setting, Value) VALUES (@SettingName, @Value)";
dbDict = new Dictionary<string, object>
{
{ "SettingName", SettingName },
{ "ValueType", 0 },
{ "Value", Value },
{ "ValueDate", null }
{ "Value", Value }
};
}

Expand Down
5 changes: 5 additions & 0 deletions gaseous-server/Classes/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace gaseous_server.Classes
{
public class Database
{
public static int schema_version = 0;

public Database()
{

Expand Down Expand Up @@ -123,6 +125,9 @@ public void InitDB()

// run post-upgrade code
DatabaseMigration.PostUpgradeScript(i, _ConnectorType);

// update schema version variable
Database.schema_version = i;
}
catch (Exception ex)
{
Expand Down

0 comments on commit 111c501

Please sign in to comment.