Ensure that multiple `BeatmapSetInfo` already in realm don't cause import failures

Really this shouldn't happen but I managed to make it happen. Until this
comes up again in a way that matters, let's just fix the LINQ crash from
`SingleOrDefault`.

I've tested this to work as expected in the broken scenario.
This commit is contained in:
Dean Herbert 2022-07-14 17:04:00 +09:00
parent b511c9cc3f
commit ebe0cfefd8
1 changed files with 3 additions and 4 deletions

View File

@ -80,9 +80,8 @@ protected override void PreImport(BeatmapSetInfo beatmapSet, Realm realm)
if (beatmapSet.OnlineID > 0)
{
var existingSetWithSameOnlineID = realm.All<BeatmapSetInfo>().SingleOrDefault(b => b.OnlineID == beatmapSet.OnlineID);
if (existingSetWithSameOnlineID != null)
// OnlineID should really be unique, but to avoid catastrophic failure let's iterate just to be sure.
foreach (var existingSetWithSameOnlineID in realm.All<BeatmapSetInfo>().Where(b => b.OnlineID == beatmapSet.OnlineID))
{
existingSetWithSameOnlineID.DeletePending = true;
existingSetWithSameOnlineID.OnlineID = -1;
@ -90,7 +89,7 @@ protected override void PreImport(BeatmapSetInfo beatmapSet, Realm realm)
foreach (var b in existingSetWithSameOnlineID.Beatmaps)
b.OnlineID = -1;
LogForModel(beatmapSet, $"Found existing beatmap set with same OnlineID ({beatmapSet.OnlineID}). It will be deleted.");
LogForModel(beatmapSet, $"Found existing beatmap set with same OnlineID ({beatmapSet.OnlineID}). It will be disassociated and marked for deletion.");
}
}
}