Skip to content

Commit ce0e14a

Browse files
committed
test: exercise FK-cascade path in DatabaseMaintenanceService wipe test
1 parent 95d965a commit ce0e14a

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

src/RustPlusBot.Persistence/Maintenance/DatabaseMaintenanceService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ await context.Database.ExecuteSqlRawAsync("PRAGMA foreign_keys = OFF", cancellat
2626

2727
foreach (var table in tables)
2828
{
29+
// Deletion order is deliberately irrelevant: PRAGMA foreign_keys = OFF (above) suspends
30+
// FK enforcement for the wipe, so do not "fix" this by adding a topological sort.
2931
// Table names come from the EF model (never user input); the identifier guard keeps
3032
// the raw statement demonstrably injection-safe for the Sonar gate.
3133
if (!IsSafeIdentifier(table!))

tests/RustPlusBot.Persistence.Tests/Maintenance/DatabaseMaintenanceServiceTests.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using Microsoft.EntityFrameworkCore;
22
using RustPlusBot.Domain.Guilds;
33
using RustPlusBot.Domain.Servers;
4+
using RustPlusBot.Domain.Switches;
45
using RustPlusBot.Persistence.Maintenance;
56

67
namespace RustPlusBot.Persistence.Tests.Maintenance;
@@ -14,10 +15,11 @@ public async Task ClearAllAsync_EmptiesEveryTable_AndKeepsSchema()
1415
await using var _ = context;
1516
await using var __ = connection;
1617

17-
context.RustServers.Add(new RustServer
18+
var serverA = new RustServer
1819
{
1920
GuildId = 1, Name = "A", Ip = "a", Port = 1
20-
});
21+
};
22+
context.RustServers.Add(serverA);
2123
context.RustServers.Add(new RustServer
2224
{
2325
GuildId = 2, Name = "B", Ip = "b", Port = 2
@@ -30,13 +32,18 @@ public async Task ClearAllAsync_EmptiesEveryTable_AndKeepsSchema()
3032
{
3133
GuildId = 2, Culture = "fr"
3234
});
35+
context.SmartSwitches.Add(new SmartSwitch
36+
{
37+
GuildId = 1, ServerId = serverA.Id, EntityId = 10, Name = "sw"
38+
});
3339
await context.SaveChangesAsync();
3440

3541
var service = new DatabaseMaintenanceService(context);
3642
await service.ClearAllAsync();
3743

3844
Assert.Empty(await context.RustServers.ToListAsync());
3945
Assert.Empty(await context.GuildSettings.ToListAsync());
46+
Assert.Empty(await context.SmartSwitches.ToListAsync());
4047

4148
// Schema still exists: a fresh insert succeeds.
4249
context.GuildSettings.Add(new GuildSettings

0 commit comments

Comments
 (0)