feat(db): add Postgres support to configuration

Extends database configuration to support Postgres in addition to existing SQLite and MySQL options.

Includes new settings for Postgres host, database name, username, and password.

Implements a connection builder for Postgres, ensuring proper error handling when connecting.

Updates dependency to include Npgsql for Postgres connectivity.
This commit is contained in:
Sakura Akeno Isayeki 2025-04-28 15:51:16 +02:00
parent 084411f847
commit 69b98980f1
No known key found for this signature in database
GPG key ID: BAB781B71FD2E7E6
3 changed files with 45 additions and 2 deletions

View file

@ -529,7 +529,7 @@ namespace TShockAPI.Configuration
#region MySQL Settings
/// <summary>The type of database to use when storing data (either "sqlite" or "mysql").</summary>
[Description("The type of database to use when storing data (either \"sqlite\" or \"mysql\").")]
[Description("The type of database to use when storing data (either \"sqlite\", \"mysql\" or \"postgres\").")]
public string StorageType = "sqlite";
/// <summary>The path of sqlite db.</summary>
@ -552,6 +552,22 @@ namespace TShockAPI.Configuration
[Description("The password used when connecting to a MySQL database.")]
public string MySqlPassword = "";
///<summary>The Postgres hostname and port to direct connections to.</summary>
[Description("The Postgres hostname and port to direct connections to.")]
public string PostgresHost = "";
/// <summary>The database name to connect to when using Postgres as the database type.</summary>
[Description("The database name to connect to when using Postgres as the database type.")]
public string PostgresDbName = "";
/// <summary>The username used when connecting to a Postgres database.</summary>
[Description("The username used when connecting to a Postgres database.")]
public string PostgresUsername = "";
/// <summary>The password used when connecting to a Postgres database.</summary>
[Description("The password used when connecting to a Postgres database.")]
public string PostgresPassword = "";
/// <summary>Whether or not to save logs to the SQL database instead of a text file.</summary>
[Description("Whether or not to save logs to the SQL database instead of a text file.\nDefault = false.")]
public bool UseSqlLogs = false;

View file

@ -3,6 +3,7 @@ using System.Diagnostics;
using System.IO;
using Microsoft.Data.Sqlite;
using MySql.Data.MySqlClient;
using Npgsql;
using TerrariaApi.Server;
using TShockAPI.Configuration;
@ -45,6 +46,7 @@ public sealed class DbBuilder
{
"sqlite" => BuildSqliteConnection(),
"mysql" => BuildMySqlConnection(),
"postgres" => BuildPostgresConnection(),
_ => throw new("Invalid storage type")
};
}
@ -86,4 +88,28 @@ public sealed class DbBuilder
throw new("MySql not setup correctly", e);
}
}
private NpgsqlConnection BuildPostgresConnection()
{
try
{
string[] hostport = _config.Settings.PostgresHost.Split(':');
NpgsqlConnectionStringBuilder connStrBuilder = new()
{
Host = hostport[0],
Port = hostport.Length > 1 ? int.Parse(hostport[1]) : 5432,
Database = _config.Settings.PostgresDbName,
Username = _config.Settings.PostgresUsername,
Password = _config.Settings.PostgresPassword
};
return new(connStrBuilder.ToString());
}
catch (NpgsqlException e)
{
ServerApi.LogWriter.PluginWriteLine(_caller, e.ToString(), TraceLevel.Error);
throw new("Postgres not setup correctly", e);
}
}
}

View file

@ -33,7 +33,8 @@
<PackageReference Include="MySql.Data" Version="8.4.0" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="6.0.11" />
<PackageReference Include="ModFramework" Version="1.1.7" GeneratePathProperty="true" /> <!-- only used to extract out to ./bin. -->
<PackageReference Include="GetText.NET" Version="1.7.14" /> <!-- only used to extract out to ./bin. -->
<PackageReference Include="GetText.NET" Version="1.7.14" />
<PackageReference Include="Npgsql" Version="6.0.13" /> <!-- only used to extract out to ./bin. -->
<!-- the launcher doesnt need the direct OTAPI reference, but since PackageReference[ExcludeFromSingleFile] doesnt work, exclude the assets and copy manually -->
<PackageReference Include="OTAPI.Upcoming" Version="3.1.20" ExcludeAssets="all" GeneratePathProperty="true" />