77
88namespace 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}
0 commit comments