This commit is contained in:
solidDoWant 2025-11-27 19:32:24 -05:00 committed by GitHub
commit 0c659d2d46
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 86 additions and 6 deletions

View file

@ -9,6 +9,7 @@
using Npgsql;
using NUnit.Framework;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Options;
using NzbDrone.Core.Configuration;
using NzbDrone.Core.Datastore;
using NzbDrone.Core.Datastore.Migration.Framework;
@ -179,6 +180,7 @@ protected void SetupContainer()
// Set up remaining container services
Mocker.SetConstant(Options.Create(postgresOptions));
Mocker.GetMock<IOptions<LogOptions>>().Setup(v => v.Value).Returns(new LogOptions());
Mocker.SetConstant<IConfigFileProvider>(Mocker.Resolve<ConfigFileProvider>());
Mocker.SetConstant<IConnectionStringFactory>(Mocker.Resolve<ConnectionStringFactory>());
Mocker.SetConstant<IMigrationController>(Mocker.Resolve<MigrationController>());

View file

@ -65,6 +65,8 @@ public interface IConfigFileProvider : IHandleAsync<ApplicationStartedEvent>,
string PostgresPassword { get; }
string PostgresMainDb { get; }
string PostgresLogDb { get; }
string PostgresMainDbConnectionString { get; }
string PostgresLogDbConnectionString { get; }
string Theme { get; }
bool TrustCgnatIpAddresses { get; }
}
@ -251,6 +253,8 @@ public AuthenticationType AuthenticationMethod
public string PostgresMainDb => _postgresOptions?.MainDb ?? GetValue("PostgresMainDb", "radarr-main", persist: false);
public string PostgresLogDb => _postgresOptions?.LogDb ?? GetValue("PostgresLogDb", "radarr-log", persist: false);
public int PostgresPort => (_postgresOptions?.Port ?? 0) != 0 ? _postgresOptions.Port : GetValueInt("PostgresPort", 5432, persist: false);
public string PostgresMainDbConnectionString => _postgresOptions?.MainDbConnectionString ?? GetValue("PostgresMainDbConnectionString", string.Empty, persist: false);
public string PostgresLogDbConnectionString => _postgresOptions?.LogDbConnectionString ?? GetValue("PostgresLogDbConnectionString", string.Empty, persist: false);
public bool LogDbEnabled => _logOptions.DbEnabled ?? GetValueBoolean("LogDbEnabled", true, persist: false);
public bool LogSql => _logOptions.Sql ?? GetValueBoolean("LogSql", false, persist: false);
public int LogRotate => _logOptions.Rotate ?? GetValueInt("LogRotate", 50, persist: false);

View file

@ -17,16 +17,32 @@ public interface IConnectionStringFactory
public class ConnectionStringFactory : IConnectionStringFactory
{
private readonly IConfigFileProvider _configFileProvider;
private bool _usePostgres;
private bool _usePostgresConnectionStrings;
public ConnectionStringFactory(IAppFolderInfo appFolderInfo, IConfigFileProvider configFileProvider)
{
_configFileProvider = configFileProvider;
MainDbConnection = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresMainDb) :
GetConnectionString(appFolderInfo.GetDatabase());
ValidatePostgresOptions();
LogDbConnection = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresLogDb) :
GetConnectionString(appFolderInfo.GetLogDatabase());
if (_usePostgres)
{
if (_usePostgresConnectionStrings)
{
MainDbConnection = GetPostgresConnectionInfoFromConnectionString(_configFileProvider.PostgresMainDbConnectionString);
LogDbConnection = GetPostgresConnectionInfoFromConnectionString(_configFileProvider.PostgresLogDbConnectionString);
return;
}
MainDbConnection = GetPostgresConnectionInfoFromIndividualValues(_configFileProvider.PostgresMainDb);
LogDbConnection = GetPostgresConnectionInfoFromIndividualValues(_configFileProvider.PostgresLogDb);
return;
}
// Default to sqlite
MainDbConnection = GetConnectionString(appFolderInfo.GetDatabase());
LogDbConnection = GetConnectionString(appFolderInfo.GetLogDatabase());
}
public DatabaseConnectionInfo MainDbConnection { get; private set; }
@ -60,9 +76,9 @@ private static DatabaseConnectionInfo GetConnectionString(string dbPath)
return new DatabaseConnectionInfo(DatabaseType.SQLite, connectionBuilder.ConnectionString);
}
private DatabaseConnectionInfo GetPostgresConnectionString(string dbName)
private DatabaseConnectionInfo GetPostgresConnectionInfoFromIndividualValues(string dbName)
{
var connectionBuilder = new NpgsqlConnectionStringBuilder
var connectionBuilder = new NpgsqlConnectionStringBuilder()
{
Database = dbName,
Host = _configFileProvider.PostgresHost,
@ -74,5 +90,61 @@ private DatabaseConnectionInfo GetPostgresConnectionString(string dbName)
return new DatabaseConnectionInfo(DatabaseType.PostgreSQL, connectionBuilder.ConnectionString);
}
private DatabaseConnectionInfo GetPostgresConnectionInfoFromConnectionString(string connectionString)
{
var connectionBuilder = new NpgsqlConnectionStringBuilder(connectionString)
{
Enlist = false
};
return new DatabaseConnectionInfo(DatabaseType.PostgreSQL, connectionBuilder.ConnectionString);
}
/// <summary>
/// Validates that either Postgres connection strings are both set or neither are set, and that either connection strings or
/// other Postgres settings are set, but not both.
/// </summary>
/// <exception cref="ArgumentException">Thrown when configuration is invalid.</exception>
private void ValidatePostgresOptions()
{
var isMainDBConnectionStringSet = !string.IsNullOrWhiteSpace(_configFileProvider.PostgresMainDbConnectionString);
var isLogDBConnectionStringSet = !string.IsNullOrWhiteSpace(_configFileProvider.PostgresLogDbConnectionString);
var isHostSet = !string.IsNullOrWhiteSpace(_configFileProvider.PostgresHost);
if (!isHostSet && !isMainDBConnectionStringSet && !isLogDBConnectionStringSet)
{
// No Postgres settings are set, so nothing to validate
return;
}
_usePostgres = true;
if (_configFileProvider.LogDbEnabled)
{
if (!isMainDBConnectionStringSet && isLogDBConnectionStringSet)
{
throw new ArgumentException("Postgres MainDbConnectionString is set but LogDbConnectionString is not. Both must be set or neither.");
}
if (isLogDBConnectionStringSet && !isMainDBConnectionStringSet)
{
throw new ArgumentException("Postgres LogDbConnectionString is set but MainDbConnectionString is not. Both must be set or neither.");
}
}
// At this point either all required connection strings are set or neither, so only one needs to be checked
var areConnectionStringConfigsSet = isMainDBConnectionStringSet;
// This one _must_ be set if connection strings are not being used, so it is used as a test to see if the user attempted configuration via individual settings
var areOtherPostgresConfigsSet = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace();
if (areConnectionStringConfigsSet && areOtherPostgresConfigsSet)
{
throw new ArgumentException($"Either both Postgres connection strings must be set, or the other Postgres settings must be set, but not both.");
}
_usePostgresConnectionStrings = areConnectionStringConfigsSet;
}
}
}

View file

@ -10,6 +10,8 @@ public class PostgresOptions
public string Password { get; set; }
public string MainDb { get; set; }
public string LogDb { get; set; }
public string MainDbConnectionString { get; set; }
public string LogDbConnectionString { get; set; }
public static PostgresOptions GetOptions()
{