Fix Crescendo SQLite upgrade collision

Fixes #21 by making the December legacy room rename safe when SQLite already has a Crescendo row.
This commit is contained in:
Alexa
2026-06-22 12:05:44 +03:00
committed by GitHub
parent 8426091e91
commit 1794d4e777
+30 -7
View File
@@ -180,8 +180,13 @@ public static class LegacyUpgrades
// either no-op (Crescendo now exists) or the only writer. // either no-op (Crescendo now exists) or the only writer.
for (var attempt = 1; attempt <= 2; attempt++) for (var attempt = 1; attempt <= 2; attempt++)
{ {
var crescendoExists = await db.Rooms.AnyAsync(r => r.Name == "Crescendo", ct); var renameCandidates = await db.Rooms
var bloodMoon = await db.Rooms.FirstOrDefaultAsync(r => r.Name == "BloodMoon", ct); .Where(r => r.Name.ToLower() == "bloodmoon" || r.Name.ToLower() == "crescendo")
.ToListAsync(ct);
var crescendoExists = renameCandidates.Any(r =>
string.Equals(r.Name, "Crescendo", StringComparison.OrdinalIgnoreCase));
var bloodMoon = renameCandidates.FirstOrDefault(r =>
string.Equals(r.Name, "BloodMoon", StringComparison.OrdinalIgnoreCase));
if (bloodMoon is null && crescendoExists) if (bloodMoon is null && crescendoExists)
{ {
@@ -217,15 +222,15 @@ public static class LegacyUpgrades
{ {
// Both exist — the rename ran on a different row at // Both exist — the rename ran on a different row at
// some point, and the original BloodMoon row is now // some point, and the original BloodMoon row is now
// a stale duplicate. Park it under CrescendoLegacy // a stale duplicate. Park it under a unique legacy
// and hide from browse so /goto/name/... still // name and hide from browse so /goto/name/... still
// resolves Crescendo to the right row. // resolves Crescendo to the right row.
bloodMoon.Name = "CrescendoLegacy"; bloodMoon.Name = await PickUniqueRoomNameAsync(db, "CrescendoLegacy", bloodMoon.Id, ct);
bloodMoon.HiddenFromBrowse = true; bloodMoon.HiddenFromBrowse = true;
await db.SaveChangesAsync(ct); await db.SaveChangesAsync(ct);
logger.LogInformation( logger.LogInformation(
"[legacy-upgrade] RenameBloodMoonToCrescendo: applied — Rooms.Id={Id} parked as CrescendoLegacy (Crescendo already existed on a different row)", "[legacy-upgrade] RenameBloodMoonToCrescendo: applied — Rooms.Id={Id} parked as {Name} (Crescendo already existed on a different row)",
bloodMoon.Id); bloodMoon.Id, bloodMoon.Name);
return; return;
} }
} }
@@ -241,4 +246,22 @@ public static class LegacyUpgrades
} }
} }
} }
private static async Task<string> PickUniqueRoomNameAsync(
DorkNetDbContext db, string preferred, long currentRoomId, CancellationToken ct)
{
var taken = await db.Rooms
.Where(r => r.Id != currentRoomId && r.Name.ToLower().StartsWith(preferred.ToLower()))
.Select(r => r.Name)
.ToListAsync(ct);
if (!taken.Any(n => string.Equals(n, preferred, StringComparison.OrdinalIgnoreCase)))
return preferred;
var suffix = 2;
while (taken.Any(n => string.Equals(n, $"{preferred}{suffix}", StringComparison.OrdinalIgnoreCase)))
suffix++;
return $"{preferred}{suffix}";
}
} }