From c62a07405e074c0ed2055ac1c8c92be0ede20d96 Mon Sep 17 00:00:00 2001 From: JPVenson Date: Sat, 19 Jul 2025 20:34:51 +0300 Subject: [PATCH] improve userdata migration (#14488) --- .../Routines/MigrateLibraryUserData.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/Jellyfin.Server/Migrations/Routines/MigrateLibraryUserData.cs b/Jellyfin.Server/Migrations/Routines/MigrateLibraryUserData.cs index beb8c0a9bd..8a0a1741f1 100644 --- a/Jellyfin.Server/Migrations/Routines/MigrateLibraryUserData.cs +++ b/Jellyfin.Server/Migrations/Routines/MigrateLibraryUserData.cs @@ -8,6 +8,7 @@ using System.Threading; using System.Threading.Tasks; using Emby.Server.Implementations.Data; using Jellyfin.Database.Implementations; +using Jellyfin.Database.Implementations.Entities; using Jellyfin.Server.Implementations.Item; using Jellyfin.Server.ServerSetupApp; using MediaBrowser.Controller; @@ -78,6 +79,8 @@ internal class MigrateLibraryUserData : IAsyncMigrationRoutine WHERE NOT EXISTS(SELECT 1 FROM TypedBaseItems WHERE TypedBaseItems.UserDataKey = UserDatas.key) """); + + var importedUserData = new Dictionary>(); foreach (var entity in queryResult) { var userData = MigrateLibraryDb.GetUserData(users, entity, userIdBlacklist, _logger); @@ -95,9 +98,22 @@ internal class MigrateLibraryUserData : IAsyncMigrationRoutine continue; } + var ogId = userData.ItemId; userData.ItemId = BaseItemRepository.PlaceholderId; userData.RetentionDate = retentionDate; - dbContext.UserData.Add(userData); + if (!importedUserData.TryGetValue(ogId, out var importUserData)) + { + importUserData = []; + importedUserData[ogId] = importUserData; + } + + importUserData.Add(userData); + } + + foreach (var item in importedUserData) + { + await dbContext.UserData.Where(e => e.ItemId == item.Key).ExecuteDeleteAsync(cancellationToken).ConfigureAwait(false); + dbContext.UserData.AddRange(item.Value.DistinctBy(e => e.CustomDataKey)); // old userdata can have fucked up duplicates } _logger.LogInformation("Try saving {NewSaved} UserData entries.", dbContext.UserData.Local.Count);