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
@@ -1209,6 +1209,54 @@ public class AuditLogRepositoryTests : IClassFixture<MsSqlMigrationFixture>
Assert.DoesNotContain(new DateTime(2026, 8, 1, 0, 0, 0, DateTimeKind.Utc), boundaries);
}
/// <summary>
/// AUD-6 regression: rows stamped before the FIRST partition boundary (partition 1 — a site
/// with a reset clock, a back-dated row) used to be invisible to the purge enumeration, so
/// they outlived retention and the archive forever. Partition 1 is now offered as the month
/// start of its newest row, and switching that value out purges exactly partition 1.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[SkippableFact]
public async Task GetPartitionBoundariesOlderThanAsync_IncludesPartitionOne_AndItSwitchesOut()
{
Skip.IfNot(_fixture.Available, _fixture.SkipReason);
var siteId = NewSiteId();
await using var context = CreateContext();
var repo = new AuditLogRepository(context);
// Two rows before the first boundary (2026-01-01) and one inside a regular partition.
await repo.InsertIfNotExistsAsync(NewEvent(siteId, occurredAtUtc: new DateTime(2019, 3, 10, 0, 0, 0, DateTimeKind.Utc)));
await repo.InsertIfNotExistsAsync(NewEvent(siteId, occurredAtUtc: new DateTime(2019, 5, 15, 12, 0, 0, DateTimeKind.Utc)));
var survivor = NewEvent(siteId, occurredAtUtc: new DateTime(2026, 7, 12, 0, 0, 0, DateTimeKind.Utc));
await repo.InsertIfNotExistsAsync(survivor);
var boundaries = await repo.GetPartitionBoundariesOlderThanAsync(new DateTime(2026, 7, 1, 0, 0, 0, DateTimeKind.Utc));
// Partition 1 is offered as the month start of its newest row.
var partitionOne = new DateTime(2019, 5, 1, 0, 0, 0, DateTimeKind.Utc);
Assert.Contains(partitionOne, boundaries);
Assert.Equal(partitionOne, boundaries[0]);
var purged = await repo.SwitchOutPartitionAsync(partitionOne);
Assert.True(purged >= 2, $"expected the two partition-1 rows to be purged, got {purged}");
await using var read = CreateContext();
var left = await read.Database
.SqlQueryRaw<int>("SELECT COUNT(*) AS Value FROM dbo.AuditLog WHERE OccurredAtUtc < '2026-01-01'")
.SingleAsync();
Assert.Equal(0, left);
var survivorId = survivor.EventId;
var survived = await read.Database
.SqlQuery<int>($"SELECT COUNT(*) AS Value FROM dbo.AuditLog WHERE EventId = {survivorId}")
.SingleAsync();
Assert.Equal(1, survived);
// Nothing is left in partition 1, so it is no longer offered.
var after = await repo.GetPartitionBoundariesOlderThanAsync(new DateTime(2026, 7, 1, 0, 0, 0, DateTimeKind.Utc));
Assert.DoesNotContain(after, b => b < new DateTime(2026, 1, 1, 0, 0, 0, DateTimeKind.Utc));
}
// ------------------------------------------------------------------------
// GetKpiSnapshotAsync — Health-dashboard Audit KPI tiles
// ------------------------------------------------------------------------