From 5b7c3850d41bd3c11428e668a94f155786d4fdb4 Mon Sep 17 00:00:00 2001 From: Andrew Horth Date: Fri, 5 Apr 2024 13:57:14 +0100 Subject: [PATCH] Revert "Revert "Allow person employment establishment to be null (to support knwing someone is working but we don't know where exactly)"" This reverts commit c5cd1d2394837c29160d9f80894cf29fd3f7aa99. --- .../Postgres/Mappings/EstablishmentMapping.cs | 2 +- .../Mappings/PersonEmploymentMapping.cs | 1 - ...05101844_EstablishmentSources.Designer.cs} | 8 +-- ...=> 20240405101844_EstablishmentSources.cs} | 56 ++++++++++++++++--- .../Migrations/TrsDbContextModelSnapshot.cs | 6 +- .../Postgres/Models/PersonEmployment.cs | 2 +- .../Events/Models/PersonEmployment.cs | 2 +- .../Jobs/RefreshEstablishmentsJob.cs | 5 +- ...vDownloadEstablishmentMasterDataService.cs | 2 +- 9 files changed, 61 insertions(+), 23 deletions(-) rename TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/{20240404170915_EstablishmentSources.Designer.cs => 20240405101844_EstablishmentSources.Designer.cs} (99%) rename TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/{20240404170915_EstablishmentSources.cs => 20240405101844_EstablishmentSources.cs} (84%) diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Mappings/EstablishmentMapping.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Mappings/EstablishmentMapping.cs index 201054a508..2243531ad6 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Mappings/EstablishmentMapping.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Mappings/EstablishmentMapping.cs @@ -29,6 +29,6 @@ public void Configure(EntityTypeBuilder builder) builder.Property(e => e.County).HasMaxLength(100).UseCollation("case_insensitive"); builder.Property(e => e.Postcode).HasMaxLength(10).UseCollation("case_insensitive"); builder.HasIndex(e => e.EstablishmentSourceId).HasDatabaseName(Establishment.EstablishmentSourceIdIndexName); - builder.HasOne().WithMany().HasForeignKey(e => e.EstablishmentSourceId).HasConstraintName("fk_establishments_establishment_id"); + builder.HasOne().WithMany().HasForeignKey(e => e.EstablishmentSourceId).HasConstraintName("fk_establishments_establishment_source_id"); } } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Mappings/PersonEmploymentMapping.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Mappings/PersonEmploymentMapping.cs index 9c6630049b..e72525e61d 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Mappings/PersonEmploymentMapping.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Mappings/PersonEmploymentMapping.cs @@ -10,7 +10,6 @@ public void Configure(EntityTypeBuilder builder) { builder.ToTable("person_employments"); builder.HasKey(e => e.PersonEmploymentId); - builder.Property(e => e.EstablishmentId).IsRequired(); builder.Property(e => e.StartDate).IsRequired(); builder.Property(e => e.EmploymentType).IsRequired(); builder.Property(e => e.CreatedOn).IsRequired(); diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/20240404170915_EstablishmentSources.Designer.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/20240405101844_EstablishmentSources.Designer.cs similarity index 99% rename from TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/20240404170915_EstablishmentSources.Designer.cs rename to TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/20240405101844_EstablishmentSources.Designer.cs index 5291ed64e5..578090294a 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/20240404170915_EstablishmentSources.Designer.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/20240405101844_EstablishmentSources.Designer.cs @@ -13,7 +13,7 @@ namespace TeachingRecordSystem.Core.DataStore.Postgres.Migrations { [DbContext(typeof(TrsDbContext))] - [Migration("20240404170915_EstablishmentSources")] + [Migration("20240405101844_EstablishmentSources")] partial class EstablishmentSources { /// @@ -1117,7 +1117,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .HasColumnType("date") .HasColumnName("end_date"); - b.Property("EstablishmentId") + b.Property("EstablishmentId") .HasColumnType("uuid") .HasColumnName("establishment_id"); @@ -1867,7 +1867,7 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) .HasForeignKey("EstablishmentSourceId") .OnDelete(DeleteBehavior.Cascade) .IsRequired() - .HasConstraintName("fk_establishments_establishment_id"); + .HasConstraintName("fk_establishments_establishment_source_id"); }); modelBuilder.Entity("TeachingRecordSystem.Core.DataStore.Postgres.Models.EytsAwardedEmailsJobItem", b => @@ -1921,8 +1921,6 @@ protected override void BuildTargetModel(ModelBuilder modelBuilder) b.HasOne("TeachingRecordSystem.Core.DataStore.Postgres.Models.Establishment", null) .WithMany() .HasForeignKey("EstablishmentId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired() .HasConstraintName("fk_person_employments_establishment_id"); b.HasOne("TeachingRecordSystem.Core.DataStore.Postgres.Models.Person", null) diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/20240404170915_EstablishmentSources.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/20240405101844_EstablishmentSources.cs similarity index 84% rename from TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/20240404170915_EstablishmentSources.cs rename to TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/20240405101844_EstablishmentSources.cs index a6a4e0948e..e8c38e5238 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/20240404170915_EstablishmentSources.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/20240405101844_EstablishmentSources.cs @@ -1,3 +1,4 @@ +using System; using Microsoft.EntityFrameworkCore.Migrations; using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; @@ -11,6 +12,18 @@ public partial class EstablishmentSources : Migration /// protected override void Up(MigrationBuilder migrationBuilder) { + migrationBuilder.DropForeignKey( + name: "fk_person_employments_establishment_id", + table: "person_employments"); + + migrationBuilder.AlterColumn( + name: "establishment_id", + table: "person_employments", + type: "uuid", + nullable: true, + oldClrType: typeof(Guid), + oldType: "uuid"); + migrationBuilder.AlterColumn( name: "urn", table: "establishments", @@ -123,11 +136,6 @@ protected override void Up(MigrationBuilder migrationBuilder) table: "person_employments", column: "person_id"); - migrationBuilder.CreateIndex( - name: "ix_establishment_establishment_source_id", - table: "establishments", - column: "establishment_source_id"); - migrationBuilder.InsertData( table: "establishment_sources", columns: new[] { "establishment_source_id", "name" }, @@ -137,22 +145,38 @@ protected override void Up(MigrationBuilder migrationBuilder) { 2, "TPS" } }); + migrationBuilder.CreateIndex( + name: "ix_establishment_establishment_source_id", + table: "establishments", + column: "establishment_source_id"); + migrationBuilder.AddForeignKey( - name: "fk_establishments_establishment_id", + name: "fk_establishments_establishment_source_id", table: "establishments", column: "establishment_source_id", principalTable: "establishment_sources", principalColumn: "establishment_source_id", onDelete: ReferentialAction.Cascade); + + migrationBuilder.AddForeignKey( + name: "fk_person_employments_establishment_id", + table: "person_employments", + column: "establishment_id", + principalTable: "establishments", + principalColumn: "establishment_id"); } /// protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropForeignKey( - name: "fk_establishments_establishment_id", + name: "fk_establishments_establishment_source_id", table: "establishments"); + migrationBuilder.DropForeignKey( + name: "fk_person_employments_establishment_id", + table: "person_employments"); + migrationBuilder.DropTable( name: "establishment_sources"); @@ -172,6 +196,16 @@ protected override void Down(MigrationBuilder migrationBuilder) name: "establishment_source_id", table: "establishments"); + migrationBuilder.AlterColumn( + name: "establishment_id", + table: "person_employments", + type: "uuid", + nullable: false, + defaultValue: new Guid("00000000-0000-0000-0000-000000000000"), + oldClrType: typeof(Guid), + oldType: "uuid", + oldNullable: true); + migrationBuilder.AlterColumn( name: "urn", table: "establishments", @@ -269,6 +303,14 @@ protected override void Down(MigrationBuilder migrationBuilder) oldClrType: typeof(int), oldType: "integer", oldNullable: true); + + migrationBuilder.AddForeignKey( + name: "fk_person_employments_establishment_id", + table: "person_employments", + column: "establishment_id", + principalTable: "establishments", + principalColumn: "establishment_id", + onDelete: ReferentialAction.Cascade); } } } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/TrsDbContextModelSnapshot.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/TrsDbContextModelSnapshot.cs index d6775b393e..08309eda3b 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/TrsDbContextModelSnapshot.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Migrations/TrsDbContextModelSnapshot.cs @@ -1114,7 +1114,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasColumnType("date") .HasColumnName("end_date"); - b.Property("EstablishmentId") + b.Property("EstablishmentId") .HasColumnType("uuid") .HasColumnName("establishment_id"); @@ -1864,7 +1864,7 @@ protected override void BuildModel(ModelBuilder modelBuilder) .HasForeignKey("EstablishmentSourceId") .OnDelete(DeleteBehavior.Cascade) .IsRequired() - .HasConstraintName("fk_establishments_establishment_id"); + .HasConstraintName("fk_establishments_establishment_source_id"); }); modelBuilder.Entity("TeachingRecordSystem.Core.DataStore.Postgres.Models.EytsAwardedEmailsJobItem", b => @@ -1918,8 +1918,6 @@ protected override void BuildModel(ModelBuilder modelBuilder) b.HasOne("TeachingRecordSystem.Core.DataStore.Postgres.Models.Establishment", null) .WithMany() .HasForeignKey("EstablishmentId") - .OnDelete(DeleteBehavior.Cascade) - .IsRequired() .HasConstraintName("fk_person_employments_establishment_id"); b.HasOne("TeachingRecordSystem.Core.DataStore.Postgres.Models.Person", null) diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Models/PersonEmployment.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Models/PersonEmployment.cs index e88def4026..d8ce067b22 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Models/PersonEmployment.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/DataStore/Postgres/Models/PersonEmployment.cs @@ -7,7 +7,7 @@ public class PersonEmployment public required Guid PersonEmploymentId { get; set; } public required Guid PersonId { get; set; } - public required Guid EstablishmentId { get; set; } + public required Guid? EstablishmentId { get; set; } public required DateOnly StartDate { get; set; } public required DateOnly? EndDate { get; set; } public required EmploymentType EmploymentType { get; set; } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Events/Models/PersonEmployment.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Events/Models/PersonEmployment.cs index bcad4ab19f..f0f2d4c123 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Events/Models/PersonEmployment.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Events/Models/PersonEmployment.cs @@ -4,7 +4,7 @@ public record PersonEmployment { public required Guid PersonEmploymentId { get; init; } public required Guid PersonId { get; init; } - public required Guid EstablishmentId { get; init; } + public required Guid? EstablishmentId { get; init; } public required DateOnly StartDate { get; init; } public required DateOnly? EndDate { get; init; } public required EmploymentType EmploymentType { get; init; } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/RefreshEstablishmentsJob.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/RefreshEstablishmentsJob.cs index 17b4b72298..8e2617fe2d 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/RefreshEstablishmentsJob.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Jobs/RefreshEstablishmentsJob.cs @@ -1,6 +1,5 @@ using TeachingRecordSystem.Core.Jobs.Scheduling; using TeachingRecordSystem.Core.Services.Establishments; -using TeachingRecordSystem.Core.Services.WorkforceData; namespace TeachingRecordSystem.Core.Jobs; @@ -9,6 +8,8 @@ public class RefreshEstablishmentsJob(IBackgroundJobScheduler backgroundJobSched public async Task ExecuteAsync(CancellationToken cancellationToken) { var refreshJobId = await backgroundJobScheduler.Enqueue(j => j.RefreshEstablishments(cancellationToken)); - await backgroundJobScheduler.ContinueJobWith(refreshJobId, j => j.UpdateLatestEstablishmentVersions(cancellationToken)); + + // The following is temporarily commented out as the current query was take over an hour to return and needs further investigation + // await backgroundJobScheduler.ContinueJobWith(refreshJobId, j => j.UpdateLatestEstablishmentVersions(cancellationToken)); } } diff --git a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/Establishments/CsvDownloadEstablishmentMasterDataService.cs b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/Establishments/CsvDownloadEstablishmentMasterDataService.cs index 97f78c0a66..b913225626 100644 --- a/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/Establishments/CsvDownloadEstablishmentMasterDataService.cs +++ b/TeachingRecordSystem/src/TeachingRecordSystem.Core/Services/Establishments/CsvDownloadEstablishmentMasterDataService.cs @@ -25,7 +25,7 @@ public async IAsyncEnumerable GetEstablishments() response.EnsureSuccessStatusCode(); using var stream = await response.Content.ReadAsStreamAsync(); - // The CSV file is encoded in Windows 1252 encoding which is almost identical to Latin1 and allows Welsh characters to be read correctly + // The CSV file is encoded in Windows 1252 encoding which is almost identical to Latin1 and allows Welsh and other special characters to be read correctly using var reader = new StreamReader(stream, Encoding.Latin1); using var csv = new CsvReader(reader, new CsvConfiguration(CultureInfo.InvariantCulture) { HasHeaderRecord = true });