From 551adf6b8176a89ffbf0020a95046ccf07ef7e47 Mon Sep 17 00:00:00 2001 From: James Gunn Date: Fri, 5 Jul 2024 11:26:13 +0100 Subject: [PATCH] Move reporting DB migrations into Worker start up (#1407) --- .../HostApplicationBuilderExtensions.cs | 10 ++++++ terraform/aks/app.tf | 2 +- terraform/aks/dqt-reporting.tf | 32 ------------------- 3 files changed, 11 insertions(+), 33 deletions(-) diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/DqtReporting/HostApplicationBuilderExtensions.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/DqtReporting/HostApplicationBuilderExtensions.cs index ec9bdea2d..2c970ac9c 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/DqtReporting/HostApplicationBuilderExtensions.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/DqtReporting/HostApplicationBuilderExtensions.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using Microsoft.PowerPlatform.Dataverse.Client; using TeachingRecordSystem.Core.Dqt; @@ -26,6 +27,15 @@ public static IHostApplicationBuilder AddDqtReporting(this IHostApplicationBuild sp => new ServiceClient(sp.GetRequiredService>().Value.CrmConnectionString)); builder.Services.AddCrmEntityChangesService(name: DqtReportingService.CrmClientName); + + builder.Services.AddStartupTask(sp => + { + var migrator = new Migrator( + connectionString: sp.GetRequiredService>().Value.ReportingDbConnectionString, + logger: sp.GetRequiredService().CreateLogger()); + migrator.MigrateDb(); + return Task.CompletedTask; + }); } return builder; diff --git a/terraform/aks/app.tf b/terraform/aks/app.tf index 541fa8cdd..dadac41da 100644 --- a/terraform/aks/app.tf +++ b/terraform/aks/app.tf @@ -228,7 +228,7 @@ module "worker_application_configuration" { module "worker_application" { source = "git::https://github.com/DFE-Digital/terraform-modules.git//aks/application?ref=testing" - depends_on = [kubernetes_job.migrations, kubernetes_job.reporting_migrations] + depends_on = [kubernetes_job.migrations] name = "worker" is_web = false diff --git a/terraform/aks/dqt-reporting.tf b/terraform/aks/dqt-reporting.tf index 01cb08a60..2242fce16 100644 --- a/terraform/aks/dqt-reporting.tf +++ b/terraform/aks/dqt-reporting.tf @@ -55,35 +55,3 @@ resource "azurerm_mssql_database" "reporting_db" { ] } } - -resource "kubernetes_job" "reporting_migrations" { - metadata { - name = "${var.service_name}-${var.environment_name}-reporting-migrations" - namespace = var.namespace - } - - spec { - template { - metadata {} - spec { - container { - name = "cli" - image = var.docker_image - command = ["trscli"] - args = ["migrate-reporting-db", "--connection-string", "$(CONNECTION_STRING)"] - - env { - name = "CONNECTION_STRING" - value = local.reporting_db_connection_string - } - } - - restart_policy = "Never" - } - } - - backoff_limit = 1 - } - - wait_for_completion = true -}