From c42cd8da9ef16723d2ba2e788c64697b45660aef Mon Sep 17 00:00:00 2001 From: ArneD Date: Thu, 27 Jun 2024 09:23:47 +0200 Subject: [PATCH] fix(integration): add more efficient method retrievingpersistentlocalid --- .../Infrastructure/IntegrationModule.cs | 2 +- .../PersistentLocalIdFinder.cs | 32 ++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/BuildingRegistry.Projections.Integration/Infrastructure/IntegrationModule.cs b/src/BuildingRegistry.Projections.Integration/Infrastructure/IntegrationModule.cs index 9f22cbf47..f1fbde1cc 100644 --- a/src/BuildingRegistry.Projections.Integration/Infrastructure/IntegrationModule.cs +++ b/src/BuildingRegistry.Projections.Integration/Infrastructure/IntegrationModule.cs @@ -17,7 +17,7 @@ public IntegrationModule( { var logger = loggerFactory.CreateLogger(); services.AddScoped(_ => - new PersistentLocalIdFinder(configuration.GetConnectionString("Events"))); + new PersistentLocalIdFinder(configuration.GetConnectionString("Events"), configuration.GetConnectionString("LegacyProjections"))); var connectionString = configuration.GetConnectionString("IntegrationProjections"); services.AddScoped(_ => new Addresses(connectionString)); diff --git a/src/BuildingRegistry.Projections.Integration/PersistentLocalIdFinder.cs b/src/BuildingRegistry.Projections.Integration/PersistentLocalIdFinder.cs index f6944b390..4b0bf8705 100644 --- a/src/BuildingRegistry.Projections.Integration/PersistentLocalIdFinder.cs +++ b/src/BuildingRegistry.Projections.Integration/PersistentLocalIdFinder.cs @@ -15,14 +15,29 @@ public interface IPersistentLocalIdFinder public class PersistentLocalIdFinder : IPersistentLocalIdFinder { private readonly string _eventsConnectionString; + private readonly string _legacyProjectionsConnectionString; - public PersistentLocalIdFinder(string eventsConnectionString) + public PersistentLocalIdFinder(string eventsConnectionString, string legacyProjectionsConnectionString) { _eventsConnectionString = eventsConnectionString; + _legacyProjectionsConnectionString = legacyProjectionsConnectionString; } public async Task FindBuildingPersistentLocalId(Guid buildingId) { + await using var projectionsConnection = new SqlConnection(_legacyProjectionsConnectionString); + + var sqlProjections = @" +SELECT TOP 1 PersistentLocalId +FROM [building-registry].[BuildingRegistryLegacy].[BuildingSyndicationWithCount] +WHERE BuildingId = @BuildingId AND PersistentLocalId IS NOT NULL"; + + var buildingPersistentLocalIdByProjection = await projectionsConnection.QuerySingleOrDefaultAsync( + sqlProjections, new { BuildingId = buildingId }); + + if(buildingPersistentLocalIdByProjection.HasValue) + return buildingPersistentLocalIdByProjection; + await using var connection = new SqlConnection(_eventsConnectionString); var sql = @" @@ -40,6 +55,21 @@ INNER JOIN [building-registry-events].[BuildingRegistry].[Messages] as m public async Task FindBuildingUnitPersistentLocalId(Guid buildingId, Guid buildingUnitId) { + await using var projectionsConnection = new SqlConnection(_legacyProjectionsConnectionString); + + var sqlProjections = @" +SELECT TOP 1 bu.PersistentLocalId +FROM [building-registry].[BuildingRegistryLegacy].[BuildingUnitSyndicationWithCount] bu +INNER JOIN [building-registry].[BuildingRegistryLegacy].[BuildingSyndicationWithCount] b + on b.Position = bu.Position and b.BuildingId = @BuildingId +WHERE bu.BuildingUnitId = @BuildingUnitId AND bu.PersistentLocalId IS NOT NULL"; + + var buildingUnitPersistentLocalIdByProjection = await projectionsConnection.QuerySingleOrDefaultAsync( + sqlProjections, new { BuildingId = buildingId, BuildingUnitId = buildingUnitId }); + + if(buildingUnitPersistentLocalIdByProjection.HasValue) + return buildingUnitPersistentLocalIdByProjection; + await using var connection = new SqlConnection(_eventsConnectionString); var sql = @"