From 41cb020ff048c0a10fa1234e7376ec365743e7b5 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Tue, 6 Feb 2024 19:58:49 -0800 Subject: [PATCH] New: Log database engine version on startup (cherry picked from commit 6ab1d8e16b29e98b4d2ebb68e0356f6f2d3a2c10) --- .../000_database_engine_version_check.cs | 69 +++++++++++++++++++ .../Framework/MigrationController.cs | 3 +- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/NzbDrone.Core/Datastore/Migration/000_database_engine_version_check.cs diff --git a/src/NzbDrone.Core/Datastore/Migration/000_database_engine_version_check.cs b/src/NzbDrone.Core/Datastore/Migration/000_database_engine_version_check.cs new file mode 100644 index 000000000..93bfc0afc --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/000_database_engine_version_check.cs @@ -0,0 +1,69 @@ +using System.Data; +using System.Text.RegularExpressions; +using FluentMigrator; +using NLog; +using NzbDrone.Common.Instrumentation; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Maintenance(MigrationStage.BeforeAll, TransactionBehavior.None)] + public class DatabaseEngineVersionCheck : FluentMigrator.Migration + { + protected readonly Logger _logger; + + public DatabaseEngineVersionCheck() + { + _logger = NzbDroneLogger.GetLogger(this); + } + + public override void Up() + { + IfDatabase("sqlite").Execute.WithConnection(LogSqliteVersion); + IfDatabase("postgres").Execute.WithConnection(LogPostgresVersion); + } + + public override void Down() + { + // No-op + } + + private void LogSqliteVersion(IDbConnection conn, IDbTransaction tran) + { + using (var versionCmd = conn.CreateCommand()) + { + versionCmd.Transaction = tran; + versionCmd.CommandText = "SELECT sqlite_version();"; + + using (var reader = versionCmd.ExecuteReader()) + { + while (reader.Read()) + { + var version = reader.GetString(0); + + _logger.Info("SQLite {0}", version); + } + } + } + } + + private void LogPostgresVersion(IDbConnection conn, IDbTransaction tran) + { + using (var versionCmd = conn.CreateCommand()) + { + versionCmd.Transaction = tran; + versionCmd.CommandText = "SHOW server_version"; + + using (var reader = versionCmd.ExecuteReader()) + { + while (reader.Read()) + { + var version = reader.GetString(0); + var cleanVersion = Regex.Replace(version, @"\(.*?\)", ""); + + _logger.Info("Postgres {0}", cleanVersion); + } + } + } + } + } +} diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs index dea8365c1..8ef3b647a 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/MigrationController.cs @@ -42,12 +42,13 @@ namespace NzbDrone.Core.Datastore.Migration.Framework serviceProvider = new ServiceCollection() .AddLogging(b => b.AddNLog()) .AddFluentMigratorCore() + .Configure(cfg => cfg.IncludeUntaggedMaintenances = true) .ConfigureRunner( builder => builder .AddPostgres() .AddNzbDroneSQLite() .WithGlobalConnectionString(connectionString) - .WithMigrationsIn(Assembly.GetExecutingAssembly())) + .ScanIn(Assembly.GetExecutingAssembly()).For.All()) .Configure(opt => opt.Namespace = "NzbDrone.Core.Datastore.Migration") .Configure(opt => {