From 2790687dc2468a126bc53f9613e2bb1e63a5a097 Mon Sep 17 00:00:00 2001 From: Matt Bishop Date: Wed, 27 Mar 2024 11:20:54 -0400 Subject: [PATCH] [PM-6938] Allow certain database operations to be skipped (#3914) * Centralize database migration logic * Clean up unused usings * Prizatize * Remove verbose flag from Docker invocation * Allow certain database operations to be skipped * Readonly --- src/Admin/Jobs/JobsHostedService.cs | 8 ++++++-- src/Core/Settings/GlobalSettings.cs | 2 ++ util/Migrator/DbMigrator.cs | 10 ++++++++-- util/Migrator/SqlServerDbMigrator.cs | 3 ++- 4 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Admin/Jobs/JobsHostedService.cs b/src/Admin/Jobs/JobsHostedService.cs index adba27970..89cf5512c 100644 --- a/src/Admin/Jobs/JobsHostedService.cs +++ b/src/Admin/Jobs/JobsHostedService.cs @@ -76,14 +76,18 @@ public class JobsHostedService : BaseJobsHostedService { new Tuple(typeof(DeleteSendsJob), everyFiveMinutesTrigger), new Tuple(typeof(DatabaseExpiredGrantsJob), everyFridayAt10pmTrigger), - new Tuple(typeof(DatabaseUpdateStatisticsJob), everySaturdayAtMidnightTrigger), - new Tuple(typeof(DatabaseRebuildlIndexesJob), everySundayAtMidnightTrigger), new Tuple(typeof(DeleteCiphersJob), everyDayAtMidnightUtc), new Tuple(typeof(DatabaseExpiredSponsorshipsJob), everyMondayAtMidnightTrigger), new Tuple(typeof(DeleteAuthRequestsJob), everyFifteenMinutesTrigger), new Tuple(typeof(DeleteUnverifiedOrganizationDomainsJob), everyDayAtTwoAmUtcTrigger), }; + if (!(_globalSettings.SqlServer?.DisableDatabaseMaintenanceJobs ?? false)) + { + jobs.Add(new Tuple(typeof(DatabaseUpdateStatisticsJob), everySaturdayAtMidnightTrigger)); + jobs.Add(new Tuple(typeof(DatabaseRebuildlIndexesJob), everySundayAtMidnightTrigger)); + } + if (!_globalSettings.SelfHosted) { jobs.Add(new Tuple(typeof(AliveJob), everyTopOfTheHourTrigger)); diff --git a/src/Core/Settings/GlobalSettings.cs b/src/Core/Settings/GlobalSettings.cs index 84037a0a1..50b4efe6f 100644 --- a/src/Core/Settings/GlobalSettings.cs +++ b/src/Core/Settings/GlobalSettings.cs @@ -221,6 +221,8 @@ public class GlobalSettings : IGlobalSettings private string _connectionString; private string _readOnlyConnectionString; private string _jobSchedulerConnectionString; + public bool SkipDatabasePreparation { get; set; } + public bool DisableDatabaseMaintenanceJobs { get; set; } public string ConnectionString { diff --git a/util/Migrator/DbMigrator.cs b/util/Migrator/DbMigrator.cs index a6ca53abd..11b80fac7 100644 --- a/util/Migrator/DbMigrator.cs +++ b/util/Migrator/DbMigrator.cs @@ -13,11 +13,14 @@ public class DbMigrator { private readonly string _connectionString; private readonly ILogger _logger; + private readonly bool _skipDatabasePreparation; - public DbMigrator(string connectionString, ILogger logger = null) + public DbMigrator(string connectionString, ILogger logger = null, + bool skipDatabasePreparation = false) { _connectionString = connectionString; _logger = logger ?? CreateLogger(); + _skipDatabasePreparation = skipDatabasePreparation; } public bool MigrateMsSqlDatabaseWithRetries(bool enableLogging = true, @@ -31,7 +34,10 @@ public class DbMigrator { try { - PrepareDatabase(cancellationToken); + if (!_skipDatabasePreparation) + { + PrepareDatabase(cancellationToken); + } var success = MigrateDatabase(enableLogging, repeatable, folderName, dryRun, cancellationToken); return success; diff --git a/util/Migrator/SqlServerDbMigrator.cs b/util/Migrator/SqlServerDbMigrator.cs index b44326082..d76b26cfb 100644 --- a/util/Migrator/SqlServerDbMigrator.cs +++ b/util/Migrator/SqlServerDbMigrator.cs @@ -10,7 +10,8 @@ public class SqlServerDbMigrator : IDbMigrator public SqlServerDbMigrator(GlobalSettings globalSettings, ILogger logger) { - _migrator = new DbMigrator(globalSettings.SqlServer.ConnectionString, logger); + _migrator = new DbMigrator(globalSettings.SqlServer.ConnectionString, logger, + globalSettings.SqlServer.SkipDatabasePreparation); } public bool MigrateDatabase(bool enableLogging = true,