New: Postgres Connection String option
(cherry picked from commit d4f14246f16821aa60fc78fa079cd9e96899c9ef)
This commit is contained in:
@@ -9,6 +9,7 @@ using Microsoft.Extensions.Options;
|
|||||||
using Npgsql;
|
using Npgsql;
|
||||||
using NUnit.Framework;
|
using NUnit.Framework;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
|
using NzbDrone.Common.Options;
|
||||||
using NzbDrone.Core.Configuration;
|
using NzbDrone.Core.Configuration;
|
||||||
using NzbDrone.Core.Datastore;
|
using NzbDrone.Core.Datastore;
|
||||||
using NzbDrone.Core.Datastore.Migration.Framework;
|
using NzbDrone.Core.Datastore.Migration.Framework;
|
||||||
@@ -179,6 +180,7 @@ namespace NzbDrone.Core.Test.Framework
|
|||||||
|
|
||||||
// Set up remaining container services
|
// Set up remaining container services
|
||||||
Mocker.SetConstant(Options.Create(postgresOptions));
|
Mocker.SetConstant(Options.Create(postgresOptions));
|
||||||
|
Mocker.GetMock<IOptions<LogOptions>>().Setup(v => v.Value).Returns(new LogOptions());
|
||||||
Mocker.SetConstant<IConfigFileProvider>(Mocker.Resolve<ConfigFileProvider>());
|
Mocker.SetConstant<IConfigFileProvider>(Mocker.Resolve<ConfigFileProvider>());
|
||||||
Mocker.SetConstant<IConnectionStringFactory>(Mocker.Resolve<ConnectionStringFactory>());
|
Mocker.SetConstant<IConnectionStringFactory>(Mocker.Resolve<ConnectionStringFactory>());
|
||||||
Mocker.SetConstant<IMigrationController>(Mocker.Resolve<MigrationController>());
|
Mocker.SetConstant<IMigrationController>(Mocker.Resolve<MigrationController>());
|
||||||
|
|||||||
@@ -65,6 +65,8 @@ namespace NzbDrone.Core.Configuration
|
|||||||
string PostgresPassword { get; }
|
string PostgresPassword { get; }
|
||||||
string PostgresMainDb { get; }
|
string PostgresMainDb { get; }
|
||||||
string PostgresLogDb { get; }
|
string PostgresLogDb { get; }
|
||||||
|
string PostgresMainDbConnectionString { get; }
|
||||||
|
string PostgresLogDbConnectionString { get; }
|
||||||
bool TrustCgnatIpAddresses { get; }
|
bool TrustCgnatIpAddresses { get; }
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,6 +252,8 @@ namespace NzbDrone.Core.Configuration
|
|||||||
public string PostgresMainDb => _postgresOptions?.MainDb ?? GetValue("PostgresMainDb", "lidarr-main", persist: false);
|
public string PostgresMainDb => _postgresOptions?.MainDb ?? GetValue("PostgresMainDb", "lidarr-main", persist: false);
|
||||||
public string PostgresLogDb => _postgresOptions?.LogDb ?? GetValue("PostgresLogDb", "lidarr-log", persist: false);
|
public string PostgresLogDb => _postgresOptions?.LogDb ?? GetValue("PostgresLogDb", "lidarr-log", persist: false);
|
||||||
public int PostgresPort => (_postgresOptions?.Port ?? 0) != 0 ? _postgresOptions.Port : GetValueInt("PostgresPort", 5432, 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 LogDbEnabled => _logOptions.DbEnabled ?? GetValueBoolean("LogDbEnabled", true, persist: false);
|
||||||
public bool LogSql => _logOptions.Sql ?? GetValueBoolean("LogSql", false, persist: false);
|
public bool LogSql => _logOptions.Sql ?? GetValueBoolean("LogSql", false, persist: false);
|
||||||
public int LogRotate => _logOptions.Rotate ?? GetValueInt("LogRotate", 50, persist: false);
|
public int LogRotate => _logOptions.Rotate ?? GetValueInt("LogRotate", 50, persist: false);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
using System.Data.SQLite;
|
using System.Data.SQLite;
|
||||||
using Npgsql;
|
using Npgsql;
|
||||||
using NzbDrone.Common.EnvironmentInfo;
|
using NzbDrone.Common.EnvironmentInfo;
|
||||||
|
using NzbDrone.Common.Exceptions;
|
||||||
using NzbDrone.Common.Extensions;
|
using NzbDrone.Common.Extensions;
|
||||||
using NzbDrone.Core.Configuration;
|
using NzbDrone.Core.Configuration;
|
||||||
|
|
||||||
@@ -22,11 +23,25 @@ namespace NzbDrone.Core.Datastore
|
|||||||
{
|
{
|
||||||
_configFileProvider = configFileProvider;
|
_configFileProvider = configFileProvider;
|
||||||
|
|
||||||
MainDbConnection = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresMainDb) :
|
var connectionStringType = GetConnectionStringType();
|
||||||
GetConnectionString(appFolderInfo.GetDatabase());
|
|
||||||
|
|
||||||
LogDbConnection = _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileProvider.PostgresLogDb) :
|
switch (connectionStringType)
|
||||||
GetConnectionString(appFolderInfo.GetLogDatabase());
|
{
|
||||||
|
case ConnectionStringType.PostgreSqlVars:
|
||||||
|
MainDbConnection = GetPostgresConnectionString(_configFileProvider.PostgresMainDb);
|
||||||
|
LogDbConnection = GetPostgresConnectionString(_configFileProvider.PostgresLogDb);
|
||||||
|
break;
|
||||||
|
case ConnectionStringType.PostgreSqlConnectionString:
|
||||||
|
MainDbConnection = GetPostgresConnectionInfoFromConnectionString(_configFileProvider.PostgresMainDbConnectionString);
|
||||||
|
LogDbConnection = GetPostgresConnectionInfoFromConnectionString(_configFileProvider.PostgresLogDbConnectionString);
|
||||||
|
break;
|
||||||
|
case ConnectionStringType.Sqlite:
|
||||||
|
MainDbConnection = GetConnectionString(appFolderInfo.GetDatabase());
|
||||||
|
LogDbConnection = GetConnectionString(appFolderInfo.GetLogDatabase());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
throw new LidarrStartupException("Unable to determine database connection string for type {0}.", connectionStringType.ToString());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public DatabaseConnectionInfo MainDbConnection { get; private set; }
|
public DatabaseConnectionInfo MainDbConnection { get; private set; }
|
||||||
@@ -74,5 +89,55 @@ namespace NzbDrone.Core.Datastore
|
|||||||
|
|
||||||
return new DatabaseConnectionInfo(DatabaseType.PostgreSQL, connectionBuilder.ConnectionString);
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
private ConnectionStringType GetConnectionStringType()
|
||||||
|
{
|
||||||
|
var isMainDBConnectionStringSet = !_configFileProvider.PostgresMainDbConnectionString.IsNullOrWhiteSpace();
|
||||||
|
var isLogDBConnectionStringSet = !_configFileProvider.PostgresLogDbConnectionString.IsNullOrWhiteSpace();
|
||||||
|
var isHostSet = !_configFileProvider.PostgresHost.IsNullOrWhiteSpace();
|
||||||
|
|
||||||
|
if (!isHostSet && !isMainDBConnectionStringSet && !isLogDBConnectionStringSet)
|
||||||
|
{
|
||||||
|
// No Postgres settings are set, so nothing to validate
|
||||||
|
return ConnectionStringType.Sqlite;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (_configFileProvider.LogDbEnabled)
|
||||||
|
{
|
||||||
|
if (!isMainDBConnectionStringSet && isLogDBConnectionStringSet)
|
||||||
|
{
|
||||||
|
throw new LidarrStartupException("Postgres MainDbConnectionString is set but LogDbConnectionString is not. Both must be set or neither.");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isLogDBConnectionStringSet && !isMainDBConnectionStringSet)
|
||||||
|
{
|
||||||
|
throw new LidarrStartupException("Postgres LogDbConnectionString is set but MainDbConnectionString is not. Both must be set or neither.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isMainDBConnectionStringSet && _configFileProvider.PostgresHost.IsNotNullOrWhiteSpace())
|
||||||
|
{
|
||||||
|
throw new LidarrStartupException($"Either both Postgres connection strings must be set, or the other Postgres settings must be set, but not both.");
|
||||||
|
}
|
||||||
|
|
||||||
|
return isMainDBConnectionStringSet ? ConnectionStringType.PostgreSqlConnectionString : ConnectionStringType.PostgreSqlVars;
|
||||||
|
}
|
||||||
|
|
||||||
|
private enum ConnectionStringType
|
||||||
|
{
|
||||||
|
Sqlite,
|
||||||
|
PostgreSqlVars,
|
||||||
|
PostgreSqlConnectionString
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,8 @@ namespace NzbDrone.Core.Datastore
|
|||||||
public string Password { get; set; }
|
public string Password { get; set; }
|
||||||
public string MainDb { get; set; }
|
public string MainDb { get; set; }
|
||||||
public string LogDb { get; set; }
|
public string LogDb { get; set; }
|
||||||
|
public string MainDbConnectionString { get; set; }
|
||||||
|
public string LogDbConnectionString { get; set; }
|
||||||
|
|
||||||
public static PostgresOptions GetOptions()
|
public static PostgresOptions GetOptions()
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user