Skip to content

Commit 696292b

Browse files
committed
Score rank migration
1 parent 5b33c4c commit 696292b

5 files changed

Lines changed: 62 additions & 2 deletions

File tree

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using Microsoft.EntityFrameworkCore.Infrastructure;
2+
using Microsoft.EntityFrameworkCore.Migrations;
3+
4+
#nullable disable
5+
6+
namespace Refresh.Database.Migrations
7+
{
8+
/// <inheritdoc />
9+
[DbContext(typeof(GameDatabaseContext))]
10+
[Migration("20260306091826_CacheScoreRanks")]
11+
public partial class CacheScoreRanks : Migration
12+
{
13+
/// <inheritdoc />
14+
protected override void Up(MigrationBuilder migrationBuilder)
15+
{
16+
migrationBuilder.AddColumn<int>(
17+
name: "Rank",
18+
table: "GameScores",
19+
type: "integer",
20+
nullable: false,
21+
defaultValue: 0);
22+
}
23+
24+
/// <inheritdoc />
25+
protected override void Down(MigrationBuilder migrationBuilder)
26+
{
27+
migrationBuilder.DropColumn(
28+
name: "Rank",
29+
table: "GameScores");
30+
}
31+
}
32+
}

Refresh.Database/Migrations/GameDatabaseContextModelSnapshot.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,9 @@ protected override void BuildModel(ModelBuilder modelBuilder)
578578
.IsRequired()
579579
.HasColumnType("text");
580580

581+
b.Property<int>("Rank")
582+
.HasColumnType("integer");
583+
581584
b.Property<int>("Score")
582585
.HasColumnType("integer");
583586

Refresh.Database/Models/Activity/Event.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ public partial class Event
2525
/// <summary>
2626
/// The user in question that created this event.
2727
/// </summary>
28-
[Required]
29-
public GameUser User { get; set; }
28+
[ForeignKey(nameof(UserId))]
29+
[Required] public GameUser User { get; set; }
30+
[Required] public ObjectId UserId { get; set; }
3031

3132
/// <summary>
3233
/// Should this event be shown to other users on the server?
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using Refresh.Database.Models.Levels;
2+
using Refresh.Workers;
3+
4+
namespace Refresh.Interfaces.Workers.Migrations;
5+
6+
public class CalculateScoreRanksMigration : MigrationJob<GameLevel>
7+
{
8+
protected override int BatchCount => 1000;
9+
10+
protected override IQueryable<GameLevel> SortAndFilter(IQueryable<GameLevel> query)
11+
{
12+
return query.OrderBy(l => l.LevelId);
13+
}
14+
15+
protected override void Migrate(WorkContext context, GameLevel[] batch)
16+
{
17+
foreach (GameLevel level in batch)
18+
{
19+
context.Database.RecalculateScoreStatistics(level);
20+
}
21+
context.Database.SaveChanges();
22+
}
23+
}

Refresh.Interfaces.Workers/RefreshWorkerManager.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public static WorkerManager Create(Logger logger, IDataStore dataStore, GameData
2626
manager.AddJob<ClampPlayerLimitsMigration>();
2727
manager.AddJob<BackfillModdedPlanetFlagsMigration>();
2828
manager.AddJob<CorrectWebsitePinProgressPlatform>();
29+
manager.AddJob<CalculateScoreRanksMigration>();
2930

3031
return manager;
3132
}

0 commit comments

Comments
 (0)