| | | 1 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 2 | | using Microsoft.EntityFrameworkCore; |
| | | 3 | | using Microsoft.EntityFrameworkCore.Metadata.Builders; |
| | | 4 | | |
| | | 5 | | namespace ArturRios.Heimdall.Data.EntityMaps; |
| | | 6 | | |
| | | 7 | | internal static class AuditLogDbMap |
| | | 8 | | { |
| | | 9 | | public static void Configure(this EntityTypeBuilder<AuditLog> auditLog) |
| | | 10 | | { |
| | 4 | 11 | | auditLog.ToTable("audit_log"); |
| | 4 | 12 | | auditLog.HasKey(x => x.Id); |
| | | 13 | | |
| | 4 | 14 | | auditLog.Property(x => x.PublicId).IsRequired(); |
| | 4 | 15 | | auditLog.HasIndex(x => x.PublicId).IsUnique(); |
| | 4 | 16 | | auditLog.HasIndex(x => x.CreatedAt); |
| | 4 | 17 | | auditLog.HasIndex(x => x.ActorPersonId); |
| | | 18 | | |
| | | 19 | | // UC-48 filters by target ("what happened to this entity?"); without this that is a scan of |
| | | 20 | | // a table that only grows. |
| | 4 | 21 | | auditLog.HasIndex(x => x.TargetId); |
| | | 22 | | |
| | | 23 | | // NFR-21's pass selects the entries past the attribution period oldest first, and only those that |
| | | 24 | | // still have an actor, or a target, to clear (#206). Partial, so each holds only the rows its query |
| | | 25 | | // can still return and shrinks as the pass clears them; created with CONCURRENTLY by |
| | | 26 | | // AddAuditRetentionIndexes, since audit_log is written on every command. |
| | 4 | 27 | | auditLog.HasIndex(x => x.CreatedAt, "ix_audit_log_created_at_attributed") |
| | 4 | 28 | | .HasDatabaseName("ix_audit_log_created_at_attributed") |
| | 4 | 29 | | .HasFilter("actor_person_id IS NOT NULL"); |
| | 4 | 30 | | auditLog.HasIndex(x => x.CreatedAt, "ix_audit_log_created_at_targeted") |
| | 4 | 31 | | .HasDatabaseName("ix_audit_log_created_at_targeted") |
| | 4 | 32 | | .HasFilter("target_id IS NOT NULL"); |
| | | 33 | | |
| | 4 | 34 | | auditLog.Property(x => x.Action).IsRequired(); |
| | 4 | 35 | | auditLog.Property(x => x.CreatedAt).HasDefaultValueSql("now()"); |
| | | 36 | | |
| | | 37 | | // ActorPersonId/ActorRole/TargetId are plain nullable columns — no FK, no required navigation. |
| | 4 | 38 | | } |
| | | 39 | | } |