1- using MongoDB . Bson ;
2- using Refresh . Common . Time ;
3- using Refresh . Database . Models . Authentication ;
1+ using Refresh . Database . Models . Authentication ;
42using Refresh . Database . Models . Pins ;
53using Refresh . Database . Models . Relations ;
4+ using Refresh . Database . Models . Users ;
65using Refresh . Workers ;
76
87namespace 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