From f2c2e77b27444109a72051ebc999c74431bffbe6 Mon Sep 17 00:00:00 2001 From: pgallik Date: Thu, 7 Dec 2023 14:16:35 +0100 Subject: [PATCH] more views --- .../IntegrationContext.cs | 4 +- .../Migrations/20231206120000_add_views.cs | 17 +++++-- ...dressWithoutLinkedParcelOrBuildingUnits.cs | 4 +- .../ActiveAddressWithoutLinkedParcels.cs | 4 +- ...tiveStreetnameWithoutLinkedRoadSegments.cs | 35 ++++++++++++++ .../AddressesLinkedToMultipleBuildingUnits.cs | 2 +- .../Views/AddressesWithMultipleLinks.cs | 42 +++++++++++++++++ .../Views/AddressesWithoutPostalCode.cs | 33 +++++++++++++ ...ion.cs => BuildingUnitAddressRelations.cs} | 12 ++--- ...sRelation.cs => ParcelAddressRelations.cs} | 12 ++--- .../Views/ParcelsLinkedToMultipleAddresses.cs | 2 +- .../Models/Views/ViewQueries.cs | 46 +++++++++++++++++++ 12 files changed, 190 insertions(+), 23 deletions(-) create mode 100644 src/Basisregisters.IntegrationDb.Schema/Models/Views/ActiveStreetnameWithoutLinkedRoadSegments.cs create mode 100644 src/Basisregisters.IntegrationDb.Schema/Models/Views/AddressesWithMultipleLinks.cs create mode 100644 src/Basisregisters.IntegrationDb.Schema/Models/Views/AddressesWithoutPostalCode.cs rename src/Basisregisters.IntegrationDb.Schema/Models/Views/{BuildingUnitAddressRelation.cs => BuildingUnitAddressRelations.cs} (75%) rename src/Basisregisters.IntegrationDb.Schema/Models/Views/{ParcelAddressRelation.cs => ParcelAddressRelations.cs} (77%) diff --git a/src/Basisregisters.IntegrationDb.Schema/IntegrationContext.cs b/src/Basisregisters.IntegrationDb.Schema/IntegrationContext.cs index d19b8644..c7e91a64 100644 --- a/src/Basisregisters.IntegrationDb.Schema/IntegrationContext.cs +++ b/src/Basisregisters.IntegrationDb.Schema/IntegrationContext.cs @@ -23,8 +23,8 @@ public class IntegrationContext : DbContext public DbSet Parcels { get; set; } public DbSet PostInfo { get; set; } public DbSet RoadSegments { get; set; } - public DbSet ParcelAddressRelations { get; set; } - public DbSet BuildingUnitAddressRelations { get; set; } + public DbSet ParcelAddressRelations { get; set; } + public DbSet BuildingUnitAddressRelations { get; set; } public DbSet ActiveAddressWithoutParcelOrBuildingUnitRelations { get; set; } public IntegrationContext() { } diff --git a/src/Basisregisters.IntegrationDb.Schema/Migrations/20231206120000_add_views.cs b/src/Basisregisters.IntegrationDb.Schema/Migrations/20231206120000_add_views.cs index 192cd24d..1f77d134 100644 --- a/src/Basisregisters.IntegrationDb.Schema/Migrations/20231206120000_add_views.cs +++ b/src/Basisregisters.IntegrationDb.Schema/Migrations/20231206120000_add_views.cs @@ -15,21 +15,32 @@ protected override void Up(MigrationBuilder migrationBuilder) migrationBuilder.Sql(ViewQueries.VIEW_ParcelAddressRelations); migrationBuilder.Sql(ViewQueries.VIEW_AddressesLinkedToMultipleBuildingUnits); + migrationBuilder.Sql(ViewQueries.VIEW_AddressesLinkedToMultipleParcels); + migrationBuilder.Sql(ViewQueries.VIEW_AddressesWithMultipleLinks); + migrationBuilder.Sql(ViewQueries.VIEW_AddressesWithoutPostalCode); + migrationBuilder.Sql(ViewQueries.VIEW_ParcelsLinkedToMultipleAddresses); + migrationBuilder.Sql(ViewQueries.VIEW_ActiveAddressWithoutLinkedParcels); migrationBuilder.Sql(ViewQueries.VIEW_ActiveAddressWithoutLinkedParcelOrBuildingUnit); - migrationBuilder.Sql(ViewQueries.VIEW_AddressesLinkedToMultipleParcels); + migrationBuilder.Sql(ViewQueries.VIEW_ActiveStreetnameWithoutLinkedRoadSegments); } protected override void Down(MigrationBuilder migrationBuilder) { string DropView(string viewName) => @$"DROP VIEW ""{IntegrationContext.Schema}"".""{viewName}"""; + migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_AddressesWithMultipleLinks))); migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_AddressesLinkedToMultipleBuildingUnits))); - migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_ParcelsLinkedToMultipleAddresses))); + migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_AddressesLinkedToMultipleParcels))); + migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_AddressesWithoutPostalCode))); + migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_ActiveAddressWithoutLinkedParcels))); migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_ActiveAddressWithoutLinkedParcelOrBuildingUnit))); - migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_AddressesLinkedToMultipleParcels))); + + migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_ParcelsLinkedToMultipleAddresses))); + + migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_ActiveStreetnameWithoutLinkedRoadSegments))); migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_BuildingUnitAddressRelations))); migrationBuilder.Sql(DropView(nameof(ViewQueries.VIEW_ParcelAddressRelations))); diff --git a/src/Basisregisters.IntegrationDb.Schema/Models/Views/ActiveAddressWithoutLinkedParcelOrBuildingUnits.cs b/src/Basisregisters.IntegrationDb.Schema/Models/Views/ActiveAddressWithoutLinkedParcelOrBuildingUnits.cs index 486da783..da6aa968 100644 --- a/src/Basisregisters.IntegrationDb.Schema/Models/Views/ActiveAddressWithoutLinkedParcelOrBuildingUnits.cs +++ b/src/Basisregisters.IntegrationDb.Schema/Models/Views/ActiveAddressWithoutLinkedParcelOrBuildingUnits.cs @@ -18,9 +18,9 @@ public sealed class ActiveAddressWithoutLinkedParcelOrBuildingUnitsConfiguration public void Configure(EntityTypeBuilder builder) { builder - .ToView("ActiveAddressWithoutLinkedParcelOrBuildings", IntegrationContext.Schema) + .ToView(nameof(ActiveAddressWithoutLinkedParcelOrBuildingUnits), IntegrationContext.Schema) .HasNoKey() - .ToSqlQuery(@$"select + .ToSqlQuery(@$"SELECT ""AddressPersistentLocalId"", ""NisCode"", ""Timestamp"" diff --git a/src/Basisregisters.IntegrationDb.Schema/Models/Views/ActiveAddressWithoutLinkedParcels.cs b/src/Basisregisters.IntegrationDb.Schema/Models/Views/ActiveAddressWithoutLinkedParcels.cs index fe30fa6a..afc5d49f 100644 --- a/src/Basisregisters.IntegrationDb.Schema/Models/Views/ActiveAddressWithoutLinkedParcels.cs +++ b/src/Basisregisters.IntegrationDb.Schema/Models/Views/ActiveAddressWithoutLinkedParcels.cs @@ -21,11 +21,11 @@ public void Configure(EntityTypeBuilder build builder .ToView(nameof(ActiveAddressWithoutLinkedParcels), IntegrationContext.Schema) .HasNoKey() - .ToSqlQuery(@$"select + .ToSqlQuery(@$"SELECT ""AddressPersistentLocalId"" ,""NisCode"" ,""Timestamp"" - FROM ""Integration"".""{ViewQueries.VIEW_ActiveAddressWithoutLinkedParcels}"" "); + FROM ""{IntegrationContext.Schema}"".""{ViewQueries.VIEW_ActiveAddressWithoutLinkedParcels}"" "); builder.HasIndex(x => x.AddressPersistentLocalId); builder.HasIndex(x => x.NisCode); diff --git a/src/Basisregisters.IntegrationDb.Schema/Models/Views/ActiveStreetnameWithoutLinkedRoadSegments.cs b/src/Basisregisters.IntegrationDb.Schema/Models/Views/ActiveStreetnameWithoutLinkedRoadSegments.cs new file mode 100644 index 00000000..37bbf076 --- /dev/null +++ b/src/Basisregisters.IntegrationDb.Schema/Models/Views/ActiveStreetnameWithoutLinkedRoadSegments.cs @@ -0,0 +1,35 @@ +namespace Basisregisters.IntegrationDb.Schema.Models.Views +{ + using System; + using Microsoft.EntityFrameworkCore; + using Microsoft.EntityFrameworkCore.Metadata.Builders; + + public class ActiveStreetnameWithoutLinkedRoadSegments + { + public int StreetNamePersistentLocalId { get; set; } + public int NisCode { get; set; } + public DateTime Timestamp { get; set; } + + + public ActiveStreetnameWithoutLinkedRoadSegments() { } + } + + public sealed class ActiveStreetnameWithoutLinkedRoadSegmentsConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder + .ToView(nameof(ActiveStreetnameWithoutLinkedRoadSegments), IntegrationContext.Schema) + .HasNoKey() + .ToSqlQuery(@$" + SELECT + ""StreetNamePersistentLocalId"", + ""NisCode"", + ""Timestamp"" + FROM ""{IntegrationContext.Schema}"".""{nameof(ViewQueries.VIEW_ActiveStreetnameWithoutLinkedRoadSegments)}"" "); + + builder.HasIndex(x => x.StreetNamePersistentLocalId); + builder.HasIndex(x => x.NisCode); + } + } +} diff --git a/src/Basisregisters.IntegrationDb.Schema/Models/Views/AddressesLinkedToMultipleBuildingUnits.cs b/src/Basisregisters.IntegrationDb.Schema/Models/Views/AddressesLinkedToMultipleBuildingUnits.cs index c5993a0a..89f658a8 100644 --- a/src/Basisregisters.IntegrationDb.Schema/Models/Views/AddressesLinkedToMultipleBuildingUnits.cs +++ b/src/Basisregisters.IntegrationDb.Schema/Models/Views/AddressesLinkedToMultipleBuildingUnits.cs @@ -23,7 +23,7 @@ public void Configure(EntityTypeBuilder .ToView(nameof(AddressesLinkedToMultipleBuildingUnits), IntegrationContext.Schema) .HasNoKey() .ToSqlQuery(@$" - select + SELECT ""AddressPersistentLocalId"", ""LinkedBuildingUnitCount"", ""NisCode"", diff --git a/src/Basisregisters.IntegrationDb.Schema/Models/Views/AddressesWithMultipleLinks.cs b/src/Basisregisters.IntegrationDb.Schema/Models/Views/AddressesWithMultipleLinks.cs new file mode 100644 index 00000000..db568085 --- /dev/null +++ b/src/Basisregisters.IntegrationDb.Schema/Models/Views/AddressesWithMultipleLinks.cs @@ -0,0 +1,42 @@ +namespace Basisregisters.IntegrationDb.Schema.Models.Views +{ + using System; + using Microsoft.EntityFrameworkCore; + using Microsoft.EntityFrameworkCore.Metadata.Builders; + + public class AddressesWithMultipleLinks + { + public int AddressPersistentLocalId { get; set; } + public string Status { get; set; } + public bool IsRemoved { get; set; } + public int LinkedBuildingUnitCount { get; set; } + public int LinkedParcelCount { get; set; } + public int NisCode { get; set; } + public DateTime Timestamp { get; set; } + + public AddressesWithMultipleLinks() { } + } + + public sealed class AddressesWithMultipleLinksConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder + .ToView(nameof(AddressesWithMultipleLinks), IntegrationContext.Schema) + .HasNoKey() + .ToSqlQuery(@$" + SELECT + ""AddressPersistentLocalId"", + ""Status"", + ""IsRemoved"", + ""LinkedBuildingUnitCount"", + ""LinkedParcelCount"", + ""NisCode"", + ""CURRENT_TIMESTAMP AS ""Timestamp"" + FROM ""{IntegrationContext.Schema}"".""{nameof(ViewQueries.VIEW_AddressesWithMultipleLinks)}"" "); + + builder.HasIndex(x => x.AddressPersistentLocalId); + builder.HasIndex(x => x.NisCode); + } + } +} diff --git a/src/Basisregisters.IntegrationDb.Schema/Models/Views/AddressesWithoutPostalCode.cs b/src/Basisregisters.IntegrationDb.Schema/Models/Views/AddressesWithoutPostalCode.cs new file mode 100644 index 00000000..3db25304 --- /dev/null +++ b/src/Basisregisters.IntegrationDb.Schema/Models/Views/AddressesWithoutPostalCode.cs @@ -0,0 +1,33 @@ +namespace Basisregisters.IntegrationDb.Schema.Models.Views +{ + using System; + using Microsoft.EntityFrameworkCore; + using Microsoft.EntityFrameworkCore.Metadata.Builders; + + public class AddressesWithoutPostalCode + { + public int AddressPersistentLocalId { get; set; } + public int NisCode { get; set; } + public DateTime Timestamp { get; set; } + + public AddressesWithoutPostalCode() { } + } + + public sealed class AddressesWithoutPostalCodeConfiguration : IEntityTypeConfiguration + { + public void Configure(EntityTypeBuilder builder) + { + builder + .ToView(nameof(AddressesWithoutPostalCode), IntegrationContext.Schema) + .HasNoKey() + .ToSqlQuery(@$" + SELECT + ""AddressPersistentLocalId"", + ""NisCode"", + ""Timestamp"" + FROM ""{IntegrationContext.Schema}"".""{nameof(ViewQueries.VIEW_AddressesWithoutPostalCode)}"" "); + + builder.HasIndex(x => x.AddressPersistentLocalId); + } + } +} diff --git a/src/Basisregisters.IntegrationDb.Schema/Models/Views/BuildingUnitAddressRelation.cs b/src/Basisregisters.IntegrationDb.Schema/Models/Views/BuildingUnitAddressRelations.cs similarity index 75% rename from src/Basisregisters.IntegrationDb.Schema/Models/Views/BuildingUnitAddressRelation.cs rename to src/Basisregisters.IntegrationDb.Schema/Models/Views/BuildingUnitAddressRelations.cs index df32112d..bd505270 100644 --- a/src/Basisregisters.IntegrationDb.Schema/Models/Views/BuildingUnitAddressRelation.cs +++ b/src/Basisregisters.IntegrationDb.Schema/Models/Views/BuildingUnitAddressRelations.cs @@ -3,23 +3,23 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; - public class BuildingUnitAddressRelation + public class BuildingUnitAddressRelations { public int BuildingUnitPersistentLocalId { get; set; } public string AddressPersistentLocalId { get; set; } - public BuildingUnitAddressRelation() { } + public BuildingUnitAddressRelations() { } } - public sealed class BuildingUnitAddressRelationConfiguration : IEntityTypeConfiguration + public sealed class BuildingUnitAddressRelationConfiguration : IEntityTypeConfiguration { - public void Configure(EntityTypeBuilder builder) + public void Configure(EntityTypeBuilder builder) { builder - .ToView("BuildingUnitAddressRelations", IntegrationContext.Schema) + .ToView(nameof(BuildingUnitAddressRelations), IntegrationContext.Schema) .HasNoKey() .ToSqlQuery(@$" - select + SELECT ""BuildingUnitPersistentLocalId"", ""AddressPersistentLocalId"" FROM ""{IntegrationContext.Schema}"".""{nameof(ViewQueries.VIEW_BuildingUnitAddressRelations)}"" "); diff --git a/src/Basisregisters.IntegrationDb.Schema/Models/Views/ParcelAddressRelation.cs b/src/Basisregisters.IntegrationDb.Schema/Models/Views/ParcelAddressRelations.cs similarity index 77% rename from src/Basisregisters.IntegrationDb.Schema/Models/Views/ParcelAddressRelation.cs rename to src/Basisregisters.IntegrationDb.Schema/Models/Views/ParcelAddressRelations.cs index ae46504f..5e4b7acf 100644 --- a/src/Basisregisters.IntegrationDb.Schema/Models/Views/ParcelAddressRelation.cs +++ b/src/Basisregisters.IntegrationDb.Schema/Models/Views/ParcelAddressRelations.cs @@ -4,24 +4,24 @@ using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; - public class ParcelAddressRelation + public class ParcelAddressRelations { public string CaPaKey { get; set; } public int? AddressPersistentLocalId { get; set; } public DateTime Timestamp { get; set; } - public ParcelAddressRelation() { } + public ParcelAddressRelations() { } } - public sealed class ParcelAddressRelationConfiguration : IEntityTypeConfiguration + public sealed class ParcelAddressRelationConfiguration : IEntityTypeConfiguration { - public void Configure(EntityTypeBuilder builder) + public void Configure(EntityTypeBuilder builder) { builder - .ToView("ParcelAddressRelations", IntegrationContext.Schema) + .ToView(nameof(ParcelAddressRelations), IntegrationContext.Schema) .HasNoKey() .ToSqlQuery(@$" - select + SELECT ""CaPaKey"", ""AddressPersistentLocalId"" FROM ""{IntegrationContext.Schema}"".""{nameof(ViewQueries.VIEW_ParcelAddressRelations)}"" "); diff --git a/src/Basisregisters.IntegrationDb.Schema/Models/Views/ParcelsLinkedToMultipleAddresses.cs b/src/Basisregisters.IntegrationDb.Schema/Models/Views/ParcelsLinkedToMultipleAddresses.cs index e22dec62..8812e1a6 100644 --- a/src/Basisregisters.IntegrationDb.Schema/Models/Views/ParcelsLinkedToMultipleAddresses.cs +++ b/src/Basisregisters.IntegrationDb.Schema/Models/Views/ParcelsLinkedToMultipleAddresses.cs @@ -22,7 +22,7 @@ public void Configure(EntityTypeBuilder builde .ToView(nameof(ParcelsLinkedToMultipleAddresses), IntegrationContext.Schema) .HasNoKey() .ToSqlQuery(@$" - select + SELECT ""CaPaKey"", ""NisCode"", ""LinkedAddressCount"" diff --git a/src/Basisregisters.IntegrationDb.Schema/Models/Views/ViewQueries.cs b/src/Basisregisters.IntegrationDb.Schema/Models/Views/ViewQueries.cs index 39c3b9d7..78688d37 100644 --- a/src/Basisregisters.IntegrationDb.Schema/Models/Views/ViewQueries.cs +++ b/src/Basisregisters.IntegrationDb.Schema/Models/Views/ViewQueries.cs @@ -58,10 +58,28 @@ GROUP BY COUNT(*) > 1; "; + public const string VIEW_AddressesWithMultipleLinks = $@" + CREATE MATERIALIZED VIEW IF NOT EXISTS ""Integration"".""{nameof(VIEW_AddressesWithMultipleLinks)}"" AS + SELECT + address.""PersistentLocalId"" AS ""AddressPersistentLocalId"", + address.""Status"", + address.""IsRemoved"", + buLinks.""LinkedBuildingUnitCount"", + parcelLinks.""LinkedParcelCount"", + address.""NisCode"", + CURRENT_TIMESTAMP AS ""Timestamp"" + FROM ""Integration"".""Addresses"" address + JOIN ""Integration"".""VIEW_AddressesLinkedToMultipleBuildingUnits"" buLinks + on address.""PersistentLocalId"" = buLinks.""AddressPersistentLocalId"" + JOIN ""Integration"".""VIEW_AddressesLinkedToMultipleParcels"" parcelLinks + on address.""PersistentLocalId"" = parcelLinks.""AddressPersistentLocalId"" + "; + public const string VIEW_ParcelsLinkedToMultipleAddresses = $@" CREATE MATERIALIZED VIEW IF NOT EXISTS ""Integration"".""{nameof(VIEW_ParcelsLinkedToMultipleAddresses)}"" AS SELECT p.""CaPaKey"", + p.""Status"", COUNT(*) AS ""LinkedAddressCount"", CURRENT_TIMESTAMP AS ""Timestamp"" @@ -117,5 +135,33 @@ AND address.""Status"" ILIKE 'inGebruik' ) ORDER BY a.""PersistentLocalId"" "; + + public const string VIEW_ActiveStreetnameWithoutLinkedRoadSegments = $@" + CREATE MATERIALIZED VIEW IF NOT EXISTS ""Integration"".""{nameof(VIEW_ActiveStreetnameWithoutLinkedRoadSegments)}"" AS + SELECT + streetname.""PersistentLocalId"" AS ""StreetNamePersistentLocalId"", + streetname.""NisCode"", + CURRENT_TIMESTAMP AS ""Timestamp"" + FROM ""Integration"".""StreetNames"" streetname + WHERE NOT EXISTS ( + SELECT 1 + FROM ""Integration"".""RoadSegments"" roadsegment + WHERE roadsegment.""LeftSideStreetNameId"" = streetname.""PersistentLocalId"" + AND roadsegment.""RightSideStreetNameId"" = streetname.""PersistentLocalId"" + ) + AND streetname.""Status"" ILIKE 'ingebruik' + AND streetname.""IsRemoved"" = false + "; + + public const string VIEW_AddressesWithoutPostalCode = $@" + CREATE MATERIALIZED VIEW IF NOT EXISTS ""Integration"".""{nameof(VIEW_AddressesWithoutPostalCode)}"" AS + SELECT + ""PersistentLocalId"" AS ""AddressPersistentLocalId"", + ""NisCode""::int, + CURRENT_TIMESTAMP AS ""Timestamp"" + + FROM ""Integration"".""Addresses"" + WHERE ""PostalCode"" = '' OR ""PostalCode"" IS NULL + "; } }