From 694db80d4c8e83ff381af56d2a3dde29e0855c3d Mon Sep 17 00:00:00 2001 From: Shadowghost Date: Fri, 30 Jan 2026 21:58:24 +0100 Subject: Reroute on version removal --- .../Item/BaseItemRepository.cs | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'Jellyfin.Server.Implementations/Item/BaseItemRepository.cs') diff --git a/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs b/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs index 9e6b100b66..22178e57f7 100644 --- a/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs +++ b/Jellyfin.Server.Implementations/Item/BaseItemRepository.cs @@ -3843,4 +3843,43 @@ public sealed class BaseItemRepository return result; } + + /// + public IReadOnlyList GetManualLinkedParentIds(Guid childId) + { + using var context = _dbProvider.CreateDbContext(); + return context.LinkedChildren + .Where(lc => lc.ChildId == childId && lc.ChildType == DbLinkedChildType.Manual) + .Select(lc => lc.ParentId) + .Distinct() + .ToList(); + } + + /// + public int RerouteLinkedChildren(Guid fromChildId, Guid toChildId) + { + using var context = _dbProvider.CreateDbContext(); + + // Get parents that already reference toChildId (to avoid duplicates) + var parentsWithTarget = context.LinkedChildren + .Where(lc => lc.ChildId == toChildId && lc.ChildType == DbLinkedChildType.Manual) + .Select(lc => lc.ParentId) + .ToHashSet(); + + // Update references that won't create duplicates + var updated = context.LinkedChildren + .Where(lc => lc.ChildId == fromChildId + && lc.ChildType == DbLinkedChildType.Manual + && !parentsWithTarget.Contains(lc.ParentId)) + .ExecuteUpdate(s => s.SetProperty(e => e.ChildId, toChildId)); + + // Remove references that would be duplicates + context.LinkedChildren + .Where(lc => lc.ChildId == fromChildId + && lc.ChildType == DbLinkedChildType.Manual + && parentsWithTarget.Contains(lc.ParentId)) + .ExecuteDelete(); + + return updated; + } } -- cgit v1.2.3