Skip to content

Commit f47dde9

Browse files
committed
Another pin migration fix
1 parent 5f7e4a9 commit f47dde9

2 files changed

Lines changed: 17 additions & 26 deletions

File tree

Refresh.Database/GameDatabaseContext.Pins.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ private IEnumerable<PinProgressRelation> GetPinProgressesByUser(GameUser user, b
169169
public DatabaseList<PinProgressRelation> GetPinProgressesByUser(GameUser user, bool isBeta, TokenPlatform platform, int skip, int count)
170170
=> new(this.GetPinProgressesByUser(user, isBeta, platform), skip, count);
171171

172+
public IQueryable<PinProgressRelation> GetAllPinProgressesByUserAndId(GameUser user, long pinId)
173+
=> this.PinProgressRelations.Where(p => p.PublisherId == user.UserId && p.PinId == pinId);
174+
172175
public PinProgressRelation? GetUserPinProgress(long pinId, GameUser user, bool isBeta, TokenPlatform platform)
173176
=> this.PinProgressRelations.FirstOrDefault(p => p.PinId == pinId && p.PublisherId == user.UserId
174177
&& (p.IsBeta == isBeta && p.Platform == platform || p.Platform == TokenPlatform.Website));
Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
using MongoDB.Bson;
2-
using Refresh.Common.Time;
3-
using Refresh.Database.Models.Authentication;
1+
using Refresh.Database.Models.Authentication;
42
using Refresh.Database.Models.Pins;
53
using Refresh.Database.Models.Relations;
4+
using Refresh.Database.Models.Users;
65
using Refresh.Workers;
76

87
namespace Refresh.Interfaces.Workers.Migrations;
98

10-
public class CorrectWebsitePinProgressPlatformMigration : MigrationJob<PinProgressRelation>
9+
// Easier to do this by user than by pin relation, to avoid various pagination issues
10+
public class CorrectWebsitePinProgressPlatformMigration : MigrationJob<GameUser>
1111
{
1212
private readonly List<long> WebsitePinIds =
1313
[
@@ -16,36 +16,25 @@ public class CorrectWebsitePinProgressPlatformMigration : MigrationJob<PinProgre
1616
(long)ServerPins.SignIntoWebsite,
1717
];
1818

19-
protected override IQueryable<PinProgressRelation> SortAndFilter(IQueryable<PinProgressRelation> query)
19+
protected override IQueryable<GameUser> SortAndFilter(IQueryable<GameUser> query)
2020
{
21-
return query
22-
.Where(p => this.WebsitePinIds.Contains(p.PinId))
23-
.OrderBy(p => p.PinId);
21+
return query.OrderBy(u => u.UserId);
2422
}
2523

26-
protected override int Migrate(WorkContext context, PinProgressRelation[] batch)
24+
protected override int Migrate(WorkContext context, GameUser[] batch)
2725
{
28-
int pinsLeft = batch.Length;
29-
30-
foreach (long pinId in this.WebsitePinIds)
26+
foreach (GameUser user in batch)
3127
{
32-
IEnumerable<IGrouping<ObjectId, PinProgressRelation>> pinsByUser = batch
33-
.Where(r => r.PinId == pinId)
34-
.GroupBy(r => r.PublisherId);
35-
36-
foreach (IEnumerable<PinProgressRelation> group in pinsByUser)
28+
foreach (long pinId in this.WebsitePinIds)
3729
{
38-
// Should never happen, but just incase
39-
if (!group.Any()) continue;
30+
List<PinProgressRelation> pinsByUserAndId = context.Database.GetAllPinProgressesByUserAndId(user, pinId).ToList();
31+
if (pinsByUserAndId.Count <= 0) continue; // no need to deduplicate if there is nothing (do still migrate if there's only 1 pin, to overwrite its platform)
4032

41-
// Find best one by the current user
42-
PinProgressRelation relationToMigrate = group.MaxBy(r => r.Progress)!;
43-
List<PinProgressRelation> relationsToRemove = group.ToList();
33+
PinProgressRelation relationToMigrate = pinsByUserAndId.MaxBy(r => r.Progress)!;
4434

45-
foreach (PinProgressRelation relation in group)
35+
foreach (PinProgressRelation relation in pinsByUserAndId)
4636
{
4737
context.Database.RemovePinProgress(relation, false);
48-
pinsLeft--;
4938
}
5039

5140
// Now take the best progress we've just got and add it as a website pin, preserving other old metadata
@@ -61,11 +50,10 @@ protected override int Migrate(WorkContext context, PinProgressRelation[] batch)
6150
};
6251

6352
context.Database.AddPinProgress(newRelation, false);
64-
pinsLeft++;
6553
}
6654
}
6755

6856
context.Database.SaveChanges();
69-
return pinsLeft;
57+
return batch.Length;
7058
}
7159
}

0 commit comments

Comments
 (0)