From 3d920808ed4e0b153b6a9c3e1eae3e0855c788f0 Mon Sep 17 00:00:00 2001 From: jvyden Date: Sat, 3 Aug 2024 14:56:12 -0400 Subject: [PATCH 1/2] Fix incorrect SequentialIdStorage migration behavior --- Refresh.GameServer/Database/GameDatabaseContext.cs | 4 +++- Refresh.GameServer/Database/ISequentialId.cs | 2 +- Refresh.GameServer/Types/Levels/GameLevel.cs | 1 + Refresh.GameServer/Types/Photos/GamePhoto.cs | 1 + Refresh.GameServer/Types/Reviews/GameReview.cs | 1 + 5 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Refresh.GameServer/Database/GameDatabaseContext.cs b/Refresh.GameServer/Database/GameDatabaseContext.cs index 408dd7f2..0a4a9462 100644 --- a/Refresh.GameServer/Database/GameDatabaseContext.cs +++ b/Refresh.GameServer/Database/GameDatabaseContext.cs @@ -79,7 +79,9 @@ private int GetOrCreateSequentialId() where T : IRealmObject, ISequentialId storage = new SequentialIdStorage { TypeName = name, - SequentialId = this._realm.All().Count() * 2, // skip over a bunch of ids incase table is broken + SequentialId = this._realm.All() + .AsEnumerable() // because realm. + .Max(t => t.SequentialId) + 1, }; // no need to do write block, this should only be called in a write transaction diff --git a/Refresh.GameServer/Database/ISequentialId.cs b/Refresh.GameServer/Database/ISequentialId.cs index 44892663..2616a782 100644 --- a/Refresh.GameServer/Database/ISequentialId.cs +++ b/Refresh.GameServer/Database/ISequentialId.cs @@ -2,5 +2,5 @@ namespace Refresh.GameServer.Database; public interface ISequentialId { - int SequentialId { set; } + int SequentialId { get; set; } } \ No newline at end of file diff --git a/Refresh.GameServer/Types/Levels/GameLevel.cs b/Refresh.GameServer/Types/Levels/GameLevel.cs index af994f11..d67e5de4 100644 --- a/Refresh.GameServer/Types/Levels/GameLevel.cs +++ b/Refresh.GameServer/Types/Levels/GameLevel.cs @@ -127,6 +127,7 @@ public GameSkillReward[] SkillRewards public int SequentialId { + get => this.LevelId; set => this.LevelId = value; } diff --git a/Refresh.GameServer/Types/Photos/GamePhoto.cs b/Refresh.GameServer/Types/Photos/GamePhoto.cs index 5a0cdf06..d5a409a0 100644 --- a/Refresh.GameServer/Types/Photos/GamePhoto.cs +++ b/Refresh.GameServer/Types/Photos/GamePhoto.cs @@ -139,6 +139,7 @@ private void ClearSubjects() [JsonIgnore] public int SequentialId { + get => this.PhotoId; set => this.PhotoId = value; } } \ No newline at end of file diff --git a/Refresh.GameServer/Types/Reviews/GameReview.cs b/Refresh.GameServer/Types/Reviews/GameReview.cs index e672c25a..6ab1461a 100644 --- a/Refresh.GameServer/Types/Reviews/GameReview.cs +++ b/Refresh.GameServer/Types/Reviews/GameReview.cs @@ -23,6 +23,7 @@ public partial class GameReview : IRealmObject, ISequentialId public int SequentialId { + get => this.ReviewId; set => this.ReviewId = value; } } \ No newline at end of file From 72bd398263229f1ae7ed740da1ccb063753e4ffa Mon Sep 17 00:00:00 2001 From: jvyden Date: Sat, 3 Aug 2024 14:59:24 -0400 Subject: [PATCH 2/2] Handle sequence with no elements --- Refresh.GameServer/Database/GameDatabaseContext.cs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/Refresh.GameServer/Database/GameDatabaseContext.cs b/Refresh.GameServer/Database/GameDatabaseContext.cs index 0a4a9462..5bdd216b 100644 --- a/Refresh.GameServer/Database/GameDatabaseContext.cs +++ b/Refresh.GameServer/Database/GameDatabaseContext.cs @@ -75,15 +75,21 @@ private int GetOrCreateSequentialId() where T : IRealmObject, ISequentialId storage.SequentialId += 1; return storage.SequentialId; } + + int objectCount = this._realm.All().Count(); storage = new SequentialIdStorage { TypeName = name, - SequentialId = this._realm.All() - .AsEnumerable() // because realm. - .Max(t => t.SequentialId) + 1, }; + if (objectCount != 0) + storage.SequentialId = this._realm.All() + .AsEnumerable() // because realm. + .Max(t => t.SequentialId) + 1; + else + storage.SequentialId = 0; + // no need to do write block, this should only be called in a write transaction this.SequentialIdStorage.Add(storage);