feat(dir): add computed Change col for TPH discriminator

This commit is contained in:
Collin M. Barrett 2021-11-16 16:06:36 -06:00
parent 83b81a668b
commit 471c46201c
2 changed files with 35 additions and 1 deletions

View file

@ -37,6 +37,7 @@ public static void AddInfrastructure(this IServiceCollection services, IConfigur
services.AddDbContextPool<CommandDbContext>(o =>
{
o.UseNpgsql(configuration.GetConnectionString("DirectoryConnection"))
.UseSnakeCaseNamingConvention()
#if DEBUG
.LogTo(Console.WriteLine, LogLevel.Information);
o.EnableSensitiveDataLogging();

View file

@ -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<Change>
{
public virtual void Configure(EntityTypeBuilder<Change> 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";
}
}
}