From 31f082e51670b3e9284ca32a27a992dbfcd55277 Mon Sep 17 00:00:00 2001 From: ta264 Date: Mon, 20 Jun 2022 21:53:25 +0100 Subject: [PATCH] Support legacy postgres options --- .../Checks/LegacyPostgresCheckFixture.cs | 51 +++++++++++++++++++ .../Datastore/ConnectionStringFactory.cs | 23 ++++++--- .../HealthCheck/Checks/LegacyPostgresCheck.cs | 47 +++++++++++++++++ src/NzbDrone.Core/Localization/Core/en.json | 1 + 4 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 src/NzbDrone.Core.Test/HealthCheck/Checks/LegacyPostgresCheckFixture.cs create mode 100644 src/NzbDrone.Core/HealthCheck/Checks/LegacyPostgresCheck.cs diff --git a/src/NzbDrone.Core.Test/HealthCheck/Checks/LegacyPostgresCheckFixture.cs b/src/NzbDrone.Core.Test/HealthCheck/Checks/LegacyPostgresCheckFixture.cs new file mode 100644 index 000000000..eeb5f16eb --- /dev/null +++ b/src/NzbDrone.Core.Test/HealthCheck/Checks/LegacyPostgresCheckFixture.cs @@ -0,0 +1,51 @@ +using System; +using Moq; +using NUnit.Framework; +using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Core.HealthCheck.Checks; +using NzbDrone.Core.Localization; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.HealthCheck.Checks +{ + [TestFixture] + public class LegacyPostgresCheckFixture : CoreTest + { + [SetUp] + public void Setup() + { + Mocker.GetMock() + .Setup(s => s.GetLocalizedString(It.IsAny())) + .Returns("Warning {0} -> {1}"); + } + + [TearDown] + public void Teardown() + { + foreach (var name in new[] { "__Postgres__Host", "__Postgres__Port", ":Postgres:Host", ":Postgres:Port" }) + { + Environment.SetEnvironmentVariable(BuildInfo.AppName + name, null); + } + } + + [Test] + public void should_return_ok_normally() + { + Subject.Check().ShouldBeOk(); + } + + [TestCase("__")] + [TestCase(":")] + public void should_return_error_if_vars_defined(string separator) + { + Environment.SetEnvironmentVariable(BuildInfo.AppName + separator + "Postgres" + separator + "Host", "localhost"); + Environment.SetEnvironmentVariable(BuildInfo.AppName + separator + "Postgres" + separator + "Port", "localhost"); + + var result = Subject.Check(); + result.ShouldBeError("Warning " + BuildInfo.AppName + separator + "Postgres" + separator + "Host, " + + BuildInfo.AppName + separator + "Postgres" + separator + "Port -> " + + BuildInfo.AppName + separator + "PostgresHost, " + + BuildInfo.AppName + separator + "PostgresPort"); + } + } +} diff --git a/src/NzbDrone.Core/Datastore/ConnectionStringFactory.cs b/src/NzbDrone.Core/Datastore/ConnectionStringFactory.cs index e1b509a0b..3d9a397d4 100644 --- a/src/NzbDrone.Core/Datastore/ConnectionStringFactory.cs +++ b/src/NzbDrone.Core/Datastore/ConnectionStringFactory.cs @@ -19,14 +19,23 @@ namespace NzbDrone.Core.Datastore { private readonly IOptionsMonitor _configFileOptions; - public ConnectionStringFactory(IAppFolderInfo appFolderInfo, IOptionsMonitor configFileOptions) + // Catch legacy config, to be removed soon + private readonly PostgresOptions _postgresOptions; + + public ConnectionStringFactory(IAppFolderInfo appFolderInfo, + IOptionsMonitor configFileOptions) { _configFileOptions = configFileOptions; + _postgresOptions = PostgresOptions.GetOptions(); - MainDbConnectionString = _configFileOptions.CurrentValue.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileOptions.CurrentValue.PostgresMainDb) : + var isPostgres = _configFileOptions.CurrentValue.PostgresHost.IsNotNullOrWhiteSpace() || _postgresOptions.Host.IsNotNullOrWhiteSpace(); + var mainDb = _configFileOptions.CurrentValue.PostgresMainDb ?? _postgresOptions.MainDb; + var logDb = _configFileOptions.CurrentValue.PostgresLogDb ?? _postgresOptions.LogDb; + + MainDbConnectionString = isPostgres ? GetPostgresConnectionString(mainDb) : GetConnectionString(appFolderInfo.GetDatabase()); - LogDbConnectionString = _configFileOptions.CurrentValue.PostgresHost.IsNotNullOrWhiteSpace() ? GetPostgresConnectionString(_configFileOptions.CurrentValue.PostgresLogDb) : + LogDbConnectionString = isPostgres ? GetPostgresConnectionString(logDb) : GetConnectionString(appFolderInfo.GetLogDatabase()); } @@ -64,10 +73,10 @@ namespace NzbDrone.Core.Datastore var connectionBuilder = new NpgsqlConnectionStringBuilder(); connectionBuilder.Database = dbName; - connectionBuilder.Host = _configFileOptions.CurrentValue.PostgresHost; - connectionBuilder.Username = _configFileOptions.CurrentValue.PostgresUser; - connectionBuilder.Password = _configFileOptions.CurrentValue.PostgresPassword; - connectionBuilder.Port = _configFileOptions.CurrentValue.PostgresPort; + connectionBuilder.Host = _configFileOptions.CurrentValue.PostgresHost ?? _postgresOptions.Host; + connectionBuilder.Username = _configFileOptions.CurrentValue.PostgresUser ?? _postgresOptions.User; + connectionBuilder.Password = _configFileOptions.CurrentValue.PostgresPassword ?? _postgresOptions.Password; + connectionBuilder.Port = _configFileOptions.CurrentValue.PostgresPort > 0 ? _configFileOptions.CurrentValue.PostgresPort : _postgresOptions.Port; connectionBuilder.Enlist = false; return connectionBuilder.ConnectionString; diff --git a/src/NzbDrone.Core/HealthCheck/Checks/LegacyPostgresCheck.cs b/src/NzbDrone.Core/HealthCheck/Checks/LegacyPostgresCheck.cs new file mode 100644 index 000000000..d079fe46d --- /dev/null +++ b/src/NzbDrone.Core/HealthCheck/Checks/LegacyPostgresCheck.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections; +using System.Linq; +using NLog; +using NzbDrone.Common.EnvironmentInfo; +using NzbDrone.Common.Extensions; +using NzbDrone.Core.Localization; + +namespace NzbDrone.Core.HealthCheck.Checks +{ + public class LegacyPostgresCheck : HealthCheckBase + { + private readonly Logger _logger; + + public LegacyPostgresCheck(ILocalizationService localizationService, Logger logger) + : base(localizationService) + { + _logger = logger; + } + + public override HealthCheck Check() + { + var legacyVars = Environment + .GetEnvironmentVariables() + .Cast() + .Select(x => x.Key.ToString()) + .Where(k => k.StartsWith(BuildInfo.AppName + "__Postgres__") || k.StartsWith(BuildInfo.AppName + ":Postgres:")) + .ToList(); + + if (legacyVars.Count == 0) + { + return new HealthCheck(GetType()); + } + + var legacyString = legacyVars.OrderBy(x => x).ConcatToString(); + var newString = legacyString + .Replace(BuildInfo.AppName + "__Postgres__", BuildInfo.AppName + "__Postgres") + .Replace(BuildInfo.AppName + ":Postgres:", BuildInfo.AppName + ":Postgres"); + + return new HealthCheck(GetType(), + HealthCheckResult.Error, + string.Format(_localizationService.GetLocalizedString("PostgresLegacyEnvironmentVariables"), legacyString, newString)); + } + + public override bool CheckOnSchedule => false; + } +} diff --git a/src/NzbDrone.Core/Localization/Core/en.json b/src/NzbDrone.Core/Localization/Core/en.json index 5139bc847..8028aca61 100644 --- a/src/NzbDrone.Core/Localization/Core/en.json +++ b/src/NzbDrone.Core/Localization/Core/en.json @@ -696,6 +696,7 @@ "PosterOptions": "Poster Options", "Posters": "Posters", "PosterSize": "Poster Size", + "PostgresLegacyEnvironmentVariables": "You have defined the following legacy PostgreSQL environment variables: {0}. Please update them to: {1}", "PreferAndUpgrade": "Prefer and Upgrade", "PreferIndexerFlags": "Prefer Indexer Flags", "PreferIndexerFlagsHelpText": "Prioritize releases with special flags",