Skip to content

Commit c97da2e

Browse files
committed
Fix website pin migration not deleting leftover duplicate pins
1 parent 6a44a76 commit c97da2e

2 files changed

Lines changed: 14 additions & 6 deletions

File tree

Refresh.Database/GameDatabaseContext.Pins.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using Refresh.Database.Models.Users;
33
using Refresh.Database.Models.Relations;
44
using Refresh.Database.Models.Pins;
5+
using MongoDB.Bson;
56

67
namespace Refresh.Database;
78

@@ -179,16 +180,20 @@ private IEnumerable<ProfilePinRelation> GetProfilePinsByUser(GameUser user, Toke
179180

180181
public DatabaseList<ProfilePinRelation> GetProfilePinsByUser(GameUser user, TokenGame game, TokenPlatform platform, int skip, int count)
181182
=> new(this.GetProfilePinsByUser(user, game, platform), skip, count);
183+
184+
#region Migration Methods
182185

183186
public void AddPinProgress(PinProgressRelation relation, bool save)
184187
{
185188
this.PinProgressRelations.Add(relation);
186189
if (save) this.SaveChanges();
187190
}
188191

189-
public void RemovePinProgresses(IEnumerable<PinProgressRelation> relations, bool save)
192+
public void RemoveAllPinProgressesByIdAndUser(long pinId, ObjectId userId, bool save)
190193
{
191-
this.PinProgressRelations.RemoveRange(relations);
194+
this.PinProgressRelations.RemoveRange(p => p.PinId == pinId && p.PublisherId == userId);
192195
if (save) this.SaveChanges();
193196
}
197+
198+
#endregion
194199
}

Refresh.Interfaces.Workers/Migrations/CorrectWebsitePinProgressPlatform.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,16 @@ protected override void Migrate(WorkContext context, PinProgressRelation[] batch
3333

3434
foreach (IEnumerable<PinProgressRelation> group in pinsByUser)
3535
{
36+
// Should never happen, but just incase
3637
if (!group.Any()) continue;
3738

38-
// Find and migrate one progress
39+
// Find best one by the current user
3940
PinProgressRelation relationToMigrate = group.MaxBy(r => r.Progress)!;
41+
42+
// Remove all already existing progresses to remove duplicates
43+
context.Database.RemoveAllPinProgressesByIdAndUser(pinId, relationToMigrate.PublisherId, false);
44+
45+
// Now take the best progress we've just got and add it as a website pin, preserving other old metadata
4046
context.Database.AddPinProgress(new()
4147
{
4248
PinId = relationToMigrate.PinId,
@@ -47,9 +53,6 @@ protected override void Migrate(WorkContext context, PinProgressRelation[] batch
4753
IsBeta = false, // doesn't matter here
4854
Platform = TokenPlatform.Website,
4955
}, false);
50-
51-
// Remove all others
52-
context.Database.RemovePinProgresses(group, false);
5356
}
5457
}
5558

0 commit comments

Comments
 (0)