Skip to content

Commit 2e07d3e

Browse files
committed
Rework CorrectWebsitePinProgressPlatformMigration
1 parent 5bf1dcd commit 2e07d3e

3 files changed

Lines changed: 24 additions & 15 deletions

File tree

Refresh.Database/GameDatabaseContext.Pins.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -180,20 +180,19 @@ private IEnumerable<ProfilePinRelation> GetProfilePinsByUser(GameUser user, Toke
180180

181181
public DatabaseList<ProfilePinRelation> GetProfilePinsByUser(GameUser user, TokenGame game, TokenPlatform platform, int skip, int count)
182182
=> new(this.GetProfilePinsByUser(user, game, platform), skip, count);
183-
184-
#region Migration Methods
185183

186184
public void AddPinProgress(PinProgressRelation relation, bool save)
187185
{
188186
this.PinProgressRelations.Add(relation);
189187
if (save) this.SaveChanges();
190188
}
191189

192-
public void RemoveAllPinProgressesByIdAndUser(long pinId, ObjectId userId, bool save)
190+
public void RemovePinProgress(PinProgressRelation relation, bool save)
193191
{
194-
this.PinProgressRelations.RemoveRange(p => p.PinId == pinId && p.PublisherId == userId);
192+
this.PinProgressRelations.Remove(relation);
195193
if (save) this.SaveChanges();
196194
}
197195

198-
#endregion
196+
public int GetTotalPinProgresses()
197+
=> this.PinProgressRelations.Count();
199198
}

Refresh.Interfaces.Workers/Migrations/CorrectWebsitePinProgressPlatform.cs renamed to Refresh.Interfaces.Workers/Migrations/CorrectWebsitePinProgressPlatformMigration.cs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
namespace Refresh.Interfaces.Workers.Migrations;
99

10-
public class CorrectWebsitePinProgressPlatform : MigrationJob<PinProgressRelation>
10+
public class CorrectWebsitePinProgressPlatformMigration : MigrationJob<PinProgressRelation>
1111
{
12-
private List<long> websitePinIds =
12+
private readonly List<long> WebsitePinIds =
1313
[
1414
(long)ServerPins.HeartPlayerOnWebsite,
1515
(long)ServerPins.QueueLevelOnWebsite,
@@ -19,13 +19,15 @@ public class CorrectWebsitePinProgressPlatform : MigrationJob<PinProgressRelatio
1919
protected override IQueryable<PinProgressRelation> SortAndFilter(IQueryable<PinProgressRelation> query)
2020
{
2121
return query
22-
.Where(p => this.websitePinIds.Contains(p.PinId))
22+
.Where(p => this.WebsitePinIds.Contains(p.PinId))
2323
.OrderBy(p => p.PinId);
2424
}
2525

26-
protected override void Migrate(WorkContext context, PinProgressRelation[] batch)
26+
protected override int Migrate(WorkContext context, PinProgressRelation[] batch)
2727
{
28-
foreach (long pinId in this.websitePinIds)
28+
int pinsLeft = batch.Length;
29+
30+
foreach (long pinId in this.WebsitePinIds)
2931
{
3032
IEnumerable<IGrouping<ObjectId, PinProgressRelation>> pinsByUser = batch
3133
.Where(r => r.PinId == pinId)
@@ -38,12 +40,16 @@ protected override void Migrate(WorkContext context, PinProgressRelation[] batch
3840

3941
// Find best one by the current user
4042
PinProgressRelation relationToMigrate = group.MaxBy(r => r.Progress)!;
43+
List<PinProgressRelation> relationsToRemove = group.ToList();
4144

42-
// Remove all already existing progresses to remove duplicates
43-
context.Database.RemoveAllPinProgressesByIdAndUser(pinId, relationToMigrate.PublisherId, false);
45+
foreach (PinProgressRelation relation in group)
46+
{
47+
context.Database.RemovePinProgress(relation, false);
48+
pinsLeft--;
49+
}
4450

4551
// Now take the best progress we've just got and add it as a website pin, preserving other old metadata
46-
context.Database.AddPinProgress(new()
52+
PinProgressRelation newRelation = new()
4753
{
4854
PinId = relationToMigrate.PinId,
4955
Progress = relationToMigrate.Progress,
@@ -52,10 +58,14 @@ protected override void Migrate(WorkContext context, PinProgressRelation[] batch
5258
LastUpdated = relationToMigrate.LastUpdated,
5359
IsBeta = false, // doesn't matter here
5460
Platform = TokenPlatform.Website,
55-
}, false);
61+
};
62+
63+
context.Database.AddPinProgress(newRelation, false);
64+
pinsLeft++;
5665
}
5766
}
5867

5968
context.Database.SaveChanges();
69+
return pinsLeft;
6070
}
6171
}

Refresh.Interfaces.Workers/RefreshWorkerManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public static WorkerManager Create(Logger logger, IDataStore dataStore, GameData
2525
manager.AddJob<BackfillReviewLabelsMigration>();
2626
manager.AddJob<ClampPlayerLimitsMigration>();
2727
manager.AddJob<BackfillModdedPlanetFlagsMigration>();
28-
manager.AddJob<CorrectWebsitePinProgressPlatform>();
28+
manager.AddJob<CorrectWebsitePinProgressPlatformMigration>();
2929
manager.AddJob<CalculateScoreRanksMigration>();
3030
manager.AddJob<MoveSubjectsOutOfGamePhotosMigration>();
3131

0 commit comments

Comments
 (0)