From 471c46201c33220c046925e964e24070d768fbc1 Mon Sep 17 00:00:00 2001 From: "Collin M. Barrett" Date: Tue, 16 Nov 2021 16:06:36 -0600 Subject: [PATCH] =?UTF-8?q?feat(dir):=20=E2=9C=A8=20add=20computed=20Chang?= =?UTF-8?q?e=20col=20for=20TPH=20discriminator?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ConfigurationExtensions.cs | 1 + .../Persistence/Queries/Entities/Change.cs | 35 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/services/Directory/FilterLists.Directory.Infrastructure/ConfigurationExtensions.cs b/services/Directory/FilterLists.Directory.Infrastructure/ConfigurationExtensions.cs index f78ddcb1c..7e7531b1a 100644 --- a/services/Directory/FilterLists.Directory.Infrastructure/ConfigurationExtensions.cs +++ b/services/Directory/FilterLists.Directory.Infrastructure/ConfigurationExtensions.cs @@ -37,6 +37,7 @@ public static void AddInfrastructure(this IServiceCollection services, IConfigur services.AddDbContextPool(o => { o.UseNpgsql(configuration.GetConnectionString("DirectoryConnection")) + .UseSnakeCaseNamingConvention() #if DEBUG .LogTo(Console.WriteLine, LogLevel.Information); o.EnableSensitiveDataLogging(); diff --git a/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Queries/Entities/Change.cs b/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Queries/Entities/Change.cs index 05c682f61..79a4be35c 100644 --- a/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Queries/Entities/Change.cs +++ b/services/Directory/FilterLists.Directory.Infrastructure/Persistence/Queries/Entities/Change.cs @@ -1,4 +1,6 @@ -using System.Text.Json; +using System.Globalization; +using System.Text.Json; +using EFCore.NamingConventions.Internal; using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; @@ -14,6 +16,7 @@ public record Change public string? RejectedReason { get; private init; } public JsonDocument? AggregateBefore { get; private init; } public JsonDocument? AggregateAfter { get; private init; } + public string? AggregateType { get; private init; } public int? FilterListId { get; private init; } public FilterList? FilterList { get; } public string? LanguageIso6391 { get; private init; } @@ -30,9 +33,39 @@ public record Change public Tag? Tag { get; } } +internal enum AggregateType +{ + FilterList, + Language, + License, + Maintainer, + Software, + Syntax, + Tag +} + internal class UpdateConfiguration : IEntityTypeConfiguration { public virtual void Configure(EntityTypeBuilder builder) { + builder + .Property(c => c.AggregateType) + .HasComputedColumnSql(BuildComputedAggregateTypeSql(), true); + + static string BuildComputedAggregateTypeSql() + { + var nameRewriter = new SnakeCaseNameRewriter(CultureInfo.InvariantCulture); + return $@" + CASE + WHEN {nameRewriter.RewriteName(nameof(Change.FilterListId))} IS NOT NULL THEN '{AggregateType.FilterList}' + WHEN {nameRewriter.RewriteName(nameof(Change.LanguageIso6391))} IS NOT NULL THEN '{AggregateType.Language}' + WHEN {nameRewriter.RewriteName(nameof(Change.LicenseId))} IS NOT NULL THEN '{AggregateType.License}' + WHEN {nameRewriter.RewriteName(nameof(Change.MaintainerId))} IS NOT NULL THEN '{AggregateType.Maintainer}' + WHEN {nameRewriter.RewriteName(nameof(Change.SoftwareId))} IS NOT NULL THEN '{AggregateType.Software}' + WHEN {nameRewriter.RewriteName(nameof(Change.SyntaxId))} IS NOT NULL THEN '{AggregateType.Syntax}' + WHEN {nameRewriter.RewriteName(nameof(Change.TagId))} IS NOT NULL THEN '{AggregateType.Tag}' + ELSE NULL + END"; + } } }