Contact: deterministische OrderBy in de opschoon-queries (RowLimitingOperationWithoutOrderByWarning bij opstart)

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-11 19:38:11 +01:00
co-authored by Claude Sonnet 5.5
parent 48681f0e53
commit 60ed9f4bad
2 changed files with 63 additions and 0 deletions
@@ -0,0 +1,61 @@
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using SlpModularCms.Modules.Contact.Data;
using SlpModularCms.Modules.Contact.Repositories;
namespace SlpModularCms.Modules.Contact.Tests;
/// <summary>
/// The purge queries run at start-up (the retention service) and used a 'Take' without an 'OrderBy', which EF Core
/// reports as RowLimitingOperationWithoutOrderByWarning (it showed up in Sentry after every deploy). The in-memory
/// provider never reports it, so this uses the real relational provider and turns the warning into an exception: the
/// query is compiled before any connection is opened, so an unreachable server shows that compilation succeeded.
/// </summary>
public class ContactRetentionQueryTests
{
private static ContactDbContext RelationalContextThatThrowsOnRowLimitWarning() => new(
new DbContextOptionsBuilder<ContactDbContext>()
.UseMySQL("Server=127.0.0.1;Port=1;Database=x;Uid=x;Pwd=x;Connection Timeout=1")
.ConfigureWarnings(w => w.Throw(CoreEventId.RowLimitingOperationWithoutOrderByWarning))
.Options);
[Fact]
public async Task Purging_expired_messages_has_a_deterministic_order()
{
await using var context = RelationalContextThatThrowsOnRowLimitWarning();
var sut = new ContactRepository(context);
var act = () => sut.DeleteExpiredMessagesAsync(DateTimeOffset.UtcNow, CancellationToken.None);
var thrown = (await act.Should().ThrowAsync<Exception>()).Which;
thrown.ToString().Should().NotContain("RowLimitingOperationWithoutOrderByWarning");
}
[Fact]
public async Task Purging_old_idempotency_records_has_a_deterministic_order()
{
await using var context = RelationalContextThatThrowsOnRowLimitWarning();
var sut = new ContactRepository(context);
var act = () => sut.DeleteIdempotencyOlderThanAsync(DateTimeOffset.UtcNow, CancellationToken.None);
var thrown = (await act.Should().ThrowAsync<Exception>()).Which;
thrown.ToString().Should().NotContain("RowLimitingOperationWithoutOrderByWarning");
}
[Fact]
public async Task The_message_list_and_the_mail_queue_have_a_deterministic_order()
{
await using var context = RelationalContextThatThrowsOnRowLimitWarning();
var sut = new ContactRepository(context);
var list = () => sut.ListAsync("s", [Data.Entities.ContactMessageStatus.New], 0, 25, CancellationToken.None);
var due = () => sut.GetDueForMailAsync("s", DateTimeOffset.UtcNow, 6, 10, CancellationToken.None);
(await list.Should().ThrowAsync<Exception>()).Which.ToString()
.Should().NotContain("RowLimitingOperationWithoutOrderByWarning");
(await due.Should().ThrowAsync<Exception>()).Which.ToString()
.Should().NotContain("RowLimitingOperationWithoutOrderByWarning");
}
}
@@ -123,6 +123,7 @@ public class ContactRepository(ContactDbContext context) : IContactRepository
{
var batch = await context.ContactMessages
.Where(m => m.RetentionUntil < now)
.OrderBy(m => m.RetentionUntil).ThenBy(m => m.Id)
.Take(BatchSize)
.ToListAsync(cancellationToken);
if (batch.Count == 0)
@@ -143,6 +144,7 @@ public class ContactRepository(ContactDbContext context) : IContactRepository
{
var batch = await context.IdempotencyRecords
.Where(r => r.CreatedAt < cutoff)
.OrderBy(r => r.CreatedAt).ThenBy(r => r.SiteId).ThenBy(r => r.Key)
.Take(BatchSize)
.ToListAsync(cancellationToken);
if (batch.Count == 0)