fix(audit): purge/archive also cover partition 1 (rows before the first boundary) (AUD-6, WP4 task 4.11)

This commit is contained in:
Joseph Doherty
2026-12-03 10:26:08 -05:00
parent 2a41122a39
commit e719e9027e
4 changed files with 81 additions and 1 deletions
@@ -203,6 +203,13 @@ public interface IAuditLogRepository
/// <paramref name="threshold"/>. Boundaries whose partition is empty are
/// excluded (a no-op switch is wasted work). Used by the purge actor
/// to enumerate retention-eligible months on every tick.
/// <para>
/// Partition 1 — everything before the first boundary, which has no lower boundary of its
/// own — is included too (AUD-6): it is reported as the month start of its newest row, a
/// value that lies inside partition 1, so <see cref="SwitchOutPartitionAsync"/> and the cold
/// archive (both resolve the partition with <c>$partition.pf_AuditLog_Month(value)</c>)
/// purge and archive it like any other month.
/// </para>
/// </summary>
/// <param name="threshold">Only partitions whose data is entirely older than this UTC datetime are returned.</param>
/// <param name="ct">Cancellation token.</param>
@@ -827,7 +827,23 @@ VALUES
) x
WHERE x.MaxOccurredAt IS NOT NULL
AND x.MaxOccurredAt < CAST('{thresholdStr}' AS datetime2(7))
ORDER BY b.BoundaryValue;";
UNION ALL
-- AUD-6: partition 1 (everything before the FIRST boundary — back-dated rows from a
-- site whose clock was reset, or test rows) has no boundary of its own, so the
-- per-boundary enumeration above never offered it and its rows outlived retention
-- and the cold archive forever. It is identified by a stand-in value inside it: the
-- month start of its newest row (always below the first boundary, so
-- $partition.pf_AuditLog_Month maps it to partition 1 for the switch-out and the
-- archive, and it labels the archive by the age of the data).
SELECT CAST(DATEFROMPARTS(YEAR(p1.MaxOccurredAt), MONTH(p1.MaxOccurredAt), 1) AS datetime2(7))
FROM (
SELECT MAX(a.OccurredAtUtc) AS MaxOccurredAt
FROM dbo.AuditLog a
WHERE $PARTITION.pf_AuditLog_Month(a.OccurredAtUtc) = 1
) p1
WHERE p1.MaxOccurredAt IS NOT NULL
AND p1.MaxOccurredAt < CAST('{thresholdStr}' AS datetime2(7))
ORDER BY 1;";
var conn = _context.Database.GetDbConnection();
var openedHere = false;