Skip to content
72 changes: 14 additions & 58 deletions Refresh.Database/GameDatabaseContext.Photos.cs
Original file line number Diff line number Diff line change
Expand Up @@ -119,67 +119,20 @@ public GamePhoto UploadPhoto(IPhotoUpload photo, IEnumerable<IPhotoUploadSubject
return newPhoto;
}

/// <remarks>
/// Migration only!!
/// </remarks>
public void MigratePhotoSubjects(GamePhoto photo, bool saveChanges)
public GamePhotoSubject AddSubjectForPhoto(GamePhoto photo, int playerId, string displayName, GameUser? user, List<float> bounds, bool save = true)
{
List<GamePhotoSubject> subjects = [];

#pragma warning disable CS0618 // obsoletion

// If DisplayName is not null, there is a subject in that spot
if (photo.Subject1DisplayName != null)
{
subjects.Add(new()
{
Photo = photo,
PlayerId = 1,
DisplayName = photo.Subject1DisplayName,
User = photo.Subject1User,
Bounds = photo.Subject1Bounds,
});
}

if (photo.Subject2DisplayName != null)
{
subjects.Add(new()
{
Photo = photo,
PlayerId = 2,
DisplayName = photo.Subject2DisplayName,
User = photo.Subject2User,
Bounds = photo.Subject2Bounds,
});
}

if (photo.Subject3DisplayName != null)
{
subjects.Add(new()
{
Photo = photo,
PlayerId = 3,
DisplayName = photo.Subject3DisplayName,
User = photo.Subject3User,
Bounds = photo.Subject3Bounds,
});
}

if (photo.Subject4DisplayName != null)
GamePhotoSubject subject = new()
{
subjects.Add(new()
{
Photo = photo,
PlayerId = 4,
DisplayName = photo.Subject4DisplayName,
User = photo.Subject4User,
Bounds = photo.Subject4Bounds,
});
}
#pragma warning restore CS0618
Photo = photo,
PlayerId = playerId,
DisplayName = displayName,
User = user,
Bounds = bounds,
};

this.GamePhotoSubjects.AddRange(subjects);
if (saveChanges) this.SaveChanges();
this.GamePhotoSubjects.Add(subject);
if (save) this.SaveChanges();
return subject;
}

public void RemovePhoto(GamePhoto photo)
Expand Down Expand Up @@ -227,6 +180,9 @@ public IQueryable<GamePhotoSubject> GetSubjectsInPhoto(GamePhoto photo)
.Where(s => s.PhotoId == photo.PhotoId)
.OrderBy(s => s.PlayerId);

public int GetTotalSubjectsInPhoto(GamePhoto photo)
=> this.GamePhotoSubjects.Count(s => s.PhotoId == photo.PhotoId);

public IQueryable<GameUser> GetUsersInPhoto(GamePhoto photo)
=> this.GetSubjectsInPhoto(photo)
.Where(s => s.User != null)
Expand Down
9 changes: 4 additions & 5 deletions Refresh.Database/GameDatabaseContext.Pins.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,19 @@ private IEnumerable<ProfilePinRelation> GetProfilePinsByUser(GameUser user, Toke

public DatabaseList<ProfilePinRelation> GetProfilePinsByUser(GameUser user, TokenGame game, TokenPlatform platform, int skip, int count)
=> new(this.GetProfilePinsByUser(user, game, platform), skip, count);

#region Migration Methods

public void AddPinProgress(PinProgressRelation relation, bool save)
{
this.PinProgressRelations.Add(relation);
if (save) this.SaveChanges();
}

public void RemoveAllPinProgressesByIdAndUser(long pinId, ObjectId userId, bool save)
public void RemovePinProgress(PinProgressRelation relation, bool save)
{
this.PinProgressRelations.RemoveRange(p => p.PinId == pinId && p.PublisherId == userId);
this.PinProgressRelations.Remove(relation);
if (save) this.SaveChanges();
}

#endregion
public int GetTotalPinProgresses()
=> this.PinProgressRelations.Count();
}
14 changes: 0 additions & 14 deletions Refresh.Database/GameDatabaseContext.Relations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -477,20 +477,6 @@ public GameReview UpdateReview(ISubmitReviewRequest updateInfo, GameReview revie
this.SaveChanges();
return review;
}

public void MigrateReviewLabels(IEnumerable<GameReview> reviews)
{
foreach (GameReview review in reviews)
{
#pragma warning disable CS0618 // LabelsString is obsolete
if (string.IsNullOrWhiteSpace(review.LabelsString)) continue;

review.Labels = LabelExtensions.FromLbpCommaList(review.LabelsString).ToList();
#pragma warning restore CS0618
}

this.SaveChanges();
}

public void DeleteReviewsPostedByUser(GameUser user)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ protected override IQueryable<GameLevel> SortAndFilter(IQueryable<GameLevel> que
.OrderBy(l => l.LevelId);
}

protected override void Migrate(WorkContext context, GameLevel[] batch)
protected override int Migrate(WorkContext context, GameLevel[] batch)
{
foreach (GameLevel level in batch)
{
context.Database.ApplyLevelMetadataFromAttributes(level);
}

context.Database.SaveChanges();
return batch.Length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,14 @@ protected override IQueryable<GameUser> SortAndFilter(IQueryable<GameUser> query
|| u.BetaPlanetsHash != "0");
}

protected override void Migrate(WorkContext context, GameUser[] batch)
protected override int Migrate(WorkContext context, GameUser[] batch)
{
foreach (GameUser user in batch)
{
context.Database.UpdatePlanetModdedStatus(user);
}

context.Database.SaveChanges();
return batch.Length;
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
using Refresh.Database.Models.Comments;
using Refresh.Database.Models.Comments;
using Refresh.Database.Models.Levels;
using Refresh.Workers;

namespace Refresh.Interfaces.Workers.Migrations;

public class BackfillReviewLabelsMigration : MigrationJob<GameReview>
{
protected override void Migrate(WorkContext context, GameReview[] batch)
protected override int Migrate(WorkContext context, GameReview[] batch)
{
context.Database.MigrateReviewLabels(batch);
foreach (GameReview review in batch)
{
#pragma warning disable CS0618 // LabelsString is obsolete
if (string.IsNullOrWhiteSpace(review.LabelsString)) continue;

context.Database.Update(review);
review.Labels = LabelExtensions.FromLbpCommaList(review.LabelsString).ToList();
#pragma warning restore CS0618
}

context.Database.SaveChanges();
return batch.Length;
}

protected override IQueryable<GameReview> SortAndFilter(IQueryable<GameReview> query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ namespace Refresh.Interfaces.Workers.Migrations;

public class BackfillRevisionMigration : MigrationJob<GameLevel>
{
protected override void Migrate(WorkContext context, GameLevel[] batch)
protected override int Migrate(WorkContext context, GameLevel[] batch)
{
foreach (GameLevel level in batch)
{
context.Database.CreateRevisionForLevel(level, null);
}
return batch.Length;
}

protected override IQueryable<GameLevel> SortAndFilter(IQueryable<GameLevel> query)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ protected override IQueryable<GameLevel> SortAndFilter(IQueryable<GameLevel> que
return query.OrderBy(l => l.LevelId);
}

protected override void Migrate(WorkContext context, GameLevel[] batch)
protected override int Migrate(WorkContext context, GameLevel[] batch)
{
foreach (GameLevel level in batch)
{
context.Database.RecalculateScoreStatistics(level);
}
context.Database.SaveChanges();
return batch.Length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ protected override IQueryable<GameLevel> SortAndFilter(IQueryable<GameLevel> que
return query.OrderBy(l => l.LevelId);
}

protected override void Migrate(WorkContext context, GameLevel[] batch)
protected override int Migrate(WorkContext context, GameLevel[] batch)
{
foreach (GameLevel level in batch)
{
Expand All @@ -26,5 +26,7 @@ protected override void Migrate(WorkContext context, GameLevel[] batch)
level.MinPlayers = Math.Clamp(level.MinPlayers, 1, 4);
level.MaxPlayers = Math.Clamp(level.MaxPlayers, 1, 4);
}

return batch.Length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

namespace Refresh.Interfaces.Workers.Migrations;

public class CorrectWebsitePinProgressPlatform : MigrationJob<PinProgressRelation>
public class CorrectWebsitePinProgressPlatformMigration : MigrationJob<PinProgressRelation>
{
private List<long> websitePinIds =
private readonly List<long> WebsitePinIds =
[
(long)ServerPins.HeartPlayerOnWebsite,
(long)ServerPins.QueueLevelOnWebsite,
Expand All @@ -19,13 +19,15 @@ public class CorrectWebsitePinProgressPlatform : MigrationJob<PinProgressRelatio
protected override IQueryable<PinProgressRelation> SortAndFilter(IQueryable<PinProgressRelation> query)
{
return query
.Where(p => this.websitePinIds.Contains(p.PinId))
.Where(p => this.WebsitePinIds.Contains(p.PinId))
.OrderBy(p => p.PinId);
}

protected override void Migrate(WorkContext context, PinProgressRelation[] batch)
protected override int Migrate(WorkContext context, PinProgressRelation[] batch)
{
foreach (long pinId in this.websitePinIds)
int pinsLeft = batch.Length;

foreach (long pinId in this.WebsitePinIds)
{
IEnumerable<IGrouping<ObjectId, PinProgressRelation>> pinsByUser = batch
.Where(r => r.PinId == pinId)
Expand All @@ -38,12 +40,16 @@ protected override void Migrate(WorkContext context, PinProgressRelation[] batch

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

// Remove all already existing progresses to remove duplicates
context.Database.RemoveAllPinProgressesByIdAndUser(pinId, relationToMigrate.PublisherId, false);
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
context.Database.AddPinProgress(new()
PinProgressRelation newRelation = new()
{
PinId = relationToMigrate.PinId,
Progress = relationToMigrate.Progress,
Expand All @@ -52,10 +58,14 @@ protected override void Migrate(WorkContext context, PinProgressRelation[] batch
LastUpdated = relationToMigrate.LastUpdated,
IsBeta = false, // doesn't matter here
Platform = TokenPlatform.Website,
}, false);
};

context.Database.AddPinProgress(newRelation, false);
pinsLeft++;
}
}

context.Database.SaveChanges();
return pinsLeft;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ protected override IQueryable<GameUser> SortAndFilter(IQueryable<GameUser> query
.OrderBy(u => u.UserId); // can't use join date here as we're changing the join date when we delete data
}

protected override void Migrate(WorkContext context, GameUser[] batch)
protected override int Migrate(WorkContext context, GameUser[] batch)
{
foreach (GameUser user in batch)
{
context.Logger.LogWarning(RefreshContext.Worker, $"Deleting {user.Username}'s account again to ensure data has been wiped...");
context.Database.DeleteUser(user);
}
return batch.Length;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,46 @@ public class MoveSubjectsOutOfGamePhotosMigration : MigrationJob<GamePhoto>
{
protected override IQueryable<GamePhoto> SortAndFilter(IQueryable<GamePhoto> query)
{
return query.OrderBy(p => p.PhotoId);
return query
.Where(p => p.Subjects.Count == 0)
.OrderBy(p => p.PhotoId);
}

protected override void Migrate(WorkContext context, GamePhoto[] batch)
protected override int Migrate(WorkContext context, GamePhoto[] batch)
{
foreach (GamePhoto photo in batch)
{
context.Database.MigratePhotoSubjects(photo, false);
// Extra check to be sure
if (context.Database.GetTotalSubjectsInPhoto(photo) > 0)
continue;

#pragma warning disable CS0618 // obsoletion

// If DisplayName is not null, there is a subject in that spot
if (photo.Subject1DisplayName != null)
{
context.Database.AddSubjectForPhoto(photo, 1, photo.Subject1DisplayName, photo.Subject1User, photo.Subject1Bounds, false);
}

if (photo.Subject2DisplayName != null)
{
context.Database.AddSubjectForPhoto(photo, 2, photo.Subject2DisplayName, photo.Subject2User, photo.Subject2Bounds, false);
}

if (photo.Subject3DisplayName != null)
{
context.Database.AddSubjectForPhoto(photo, 3, photo.Subject3DisplayName, photo.Subject3User, photo.Subject3Bounds, false);
}

if (photo.Subject4DisplayName != null)
{
context.Database.AddSubjectForPhoto(photo, 4, photo.Subject4DisplayName, photo.Subject4User, photo.Subject4Bounds, false);
}

#pragma warning restore CS0618
}

context.Database.SaveChanges();
return batch.Length;
}
}
2 changes: 1 addition & 1 deletion Refresh.Interfaces.Workers/RefreshWorkerManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static WorkerManager Create(Logger logger, IDataStore dataStore, GameData
manager.AddJob<BackfillReviewLabelsMigration>();
manager.AddJob<ClampPlayerLimitsMigration>();
manager.AddJob<BackfillModdedPlanetFlagsMigration>();
manager.AddJob<CorrectWebsitePinProgressPlatform>();
manager.AddJob<CorrectWebsitePinProgressPlatformMigration>();
manager.AddJob<CalculateScoreRanksMigration>();
manager.AddJob<MoveSubjectsOutOfGamePhotosMigration>();

Expand Down
Loading
Loading