1
0
Fork 0
mirror of https://github.com/lidarr/Lidarr synced 2024-12-22 07:42:28 +00:00

Support Postgres with non-standard version string

(cherry picked from commit 40f4ef27b22113c1dae0d0cbdee8205132bed68a)

Closes #5267
This commit is contained in:
Mark McDowall 2024-11-24 20:51:33 -08:00 committed by Bogdan
parent ec93c33aa9
commit 4c603e24f6
3 changed files with 61 additions and 22 deletions

View file

@ -0,0 +1,38 @@
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Datastore;
namespace NzbDrone.Core.Test.Datastore;
[TestFixture]
public class DatabaseVersionParserFixture
{
[TestCase("3.44.2", 3, 44, 2)]
public void should_parse_sqlite_database_version(string serverVersion, int majorVersion, int minorVersion, int buildVersion)
{
var version = DatabaseVersionParser.ParseServerVersion(serverVersion);
version.Should().NotBeNull();
version.Major.Should().Be(majorVersion);
version.Minor.Should().Be(minorVersion);
version.Build.Should().Be(buildVersion);
}
[TestCase("14.8 (Debian 14.8-1.pgdg110+1)", 14, 8, null)]
[TestCase("16.3 (Debian 16.3-1.pgdg110+1)", 16, 3, null)]
[TestCase("16.3 - Percona Distribution", 16, 3, null)]
[TestCase("17.0 - Percona Server", 17, 0, null)]
public void should_parse_postgres_database_version(string serverVersion, int majorVersion, int minorVersion, int? buildVersion)
{
var version = DatabaseVersionParser.ParseServerVersion(serverVersion);
version.Should().NotBeNull();
version.Major.Should().Be(majorVersion);
version.Minor.Should().Be(minorVersion);
if (buildVersion.HasValue)
{
version.Build.Should().Be(buildVersion.Value);
}
}
}

View file

@ -1,7 +1,7 @@
using System; using System;
using System.Data; using System.Data;
using System.Data.Common;
using System.Data.SQLite; using System.Data.SQLite;
using System.Text.RegularExpressions;
using Dapper; using Dapper;
using NLog; using NLog;
using NzbDrone.Common.Instrumentation; using NzbDrone.Common.Instrumentation;
@ -39,35 +39,20 @@ public DatabaseType DatabaseType
{ {
get get
{ {
using (var db = _datamapperFactory()) using var db = _datamapperFactory();
{
return db is SQLiteConnection ? DatabaseType.SQLite : DatabaseType.PostgreSQL; return db is SQLiteConnection ? DatabaseType.SQLite : DatabaseType.PostgreSQL;
} }
} }
}
public Version Version public Version Version
{ {
get get
{ {
using (var db = _datamapperFactory()) using var db = _datamapperFactory();
{ var dbConnection = db as DbConnection;
string version;
try return DatabaseVersionParser.ParseServerVersion(dbConnection.ServerVersion);
{
version = db.QueryFirstOrDefault<string>("SHOW server_version");
// Postgres can return extra info about operating system on version call, ignore this
version = Regex.Replace(version, @"\(.*?\)", "");
}
catch
{
version = db.QueryFirstOrDefault<string>("SELECT sqlite_version()");
}
return new Version(version);
}
} }
} }

View file

@ -0,0 +1,16 @@
using System;
using System.Text.RegularExpressions;
namespace NzbDrone.Core.Datastore;
public static class DatabaseVersionParser
{
private static readonly Regex VersionRegex = new (@"^[^ ]+", RegexOptions.Compiled);
public static Version ParseServerVersion(string serverVersion)
{
var match = VersionRegex.Match(serverVersion);
return match.Success ? new Version(match.Value) : null;
}
}