forked from LittleBigRefresh/Refresh
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCorrectWebsitePinProgressPlatformMigration.cs
More file actions
71 lines (60 loc) · 2.55 KB
/
Copy pathCorrectWebsitePinProgressPlatformMigration.cs
File metadata and controls
71 lines (60 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
using MongoDB.Bson;
using Refresh.Common.Time;
using Refresh.Database.Models.Authentication;
using Refresh.Database.Models.Pins;
using Refresh.Database.Models.Relations;
using Refresh.Workers;
namespace Refresh.Interfaces.Workers.Migrations;
public class CorrectWebsitePinProgressPlatformMigration : MigrationJob<PinProgressRelation>
{
private readonly List<long> WebsitePinIds =
[
(long)ServerPins.HeartPlayerOnWebsite,
(long)ServerPins.QueueLevelOnWebsite,
(long)ServerPins.SignIntoWebsite,
];
protected override IQueryable<PinProgressRelation> SortAndFilter(IQueryable<PinProgressRelation> query)
{
return query
.Where(p => this.WebsitePinIds.Contains(p.PinId))
.OrderBy(p => p.PinId);
}
protected override int Migrate(WorkContext context, PinProgressRelation[] batch)
{
int pinsLeft = batch.Length;
foreach (long pinId in this.WebsitePinIds)
{
IEnumerable<IGrouping<ObjectId, PinProgressRelation>> pinsByUser = batch
.Where(r => r.PinId == pinId)
.GroupBy(r => r.PublisherId);
foreach (IEnumerable<PinProgressRelation> group in pinsByUser)
{
// Should never happen, but just incase
if (!group.Any()) continue;
// Find best one by the current user
PinProgressRelation relationToMigrate = group.MaxBy(r => r.Progress)!;
List<PinProgressRelation> relationsToRemove = group.ToList();
foreach (PinProgressRelation relation in group)
{
context.Database.RemovePinProgress(relation, false);
pinsLeft--;
}
// Now take the best progress we've just got and add it as a website pin, preserving other old metadata
PinProgressRelation newRelation = new()
{
PinId = relationToMigrate.PinId,
Progress = relationToMigrate.Progress,
PublisherId = relationToMigrate.PublisherId,
FirstPublished = relationToMigrate.FirstPublished,
LastUpdated = relationToMigrate.LastUpdated,
IsBeta = false, // doesn't matter here
Platform = TokenPlatform.Website,
};
context.Database.AddPinProgress(newRelation, false);
pinsLeft++;
}
}
context.Database.SaveChanges();
return pinsLeft;
}
}