< Summary - Jellyfin

Information
Class: Jellyfin.Server.Migrations.Routines.FixIncorrectOwnerIdRelationships
Assembly: jellyfin
File(s): /srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/FixIncorrectOwnerIdRelationships.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 175
Coverable lines: 175
Total lines: 341
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 48
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 5/4/2026 - 12:15:16 AM Line coverage: 0% (0/175) Branch coverage: 0% (0/48) Total lines: 341 5/4/2026 - 12:15:16 AM Line coverage: 0% (0/175) Branch coverage: 0% (0/48) Total lines: 341

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
PerformAsync()100%210%
RemoveDuplicateItemsAsync()0%272160%
ClearIncorrectOwnerIdsAsync()0%2040%
ReassignOrphanedExtrasAsync()0%342180%
PopulatePrimaryVersionIdAsync()0%110100%

File(s)

/srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/FixIncorrectOwnerIdRelationships.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Linq;
 4using System.Threading;
 5using System.Threading.Tasks;
 6using Jellyfin.Database.Implementations;
 7using Jellyfin.Server.ServerSetupApp;
 8using MediaBrowser.Controller.Library;
 9using MediaBrowser.Controller.Persistence;
 10using Microsoft.EntityFrameworkCore;
 11using Microsoft.Extensions.Logging;
 12
 13namespace Jellyfin.Server.Migrations.Routines;
 14
 15/// <summary>
 16/// Fixes incorrect OwnerId relationships where video/movie items are children of other video/movie items.
 17/// These are alternate versions (4K vs 1080p) that were incorrectly linked as parent-child relationships
 18/// by the auto-merge logic. Only legitimate extras (trailers, behind-the-scenes) should have OwnerId set.
 19/// Also removes duplicate database entries for the same file path.
 20/// </summary>
 21[JellyfinMigration("2026-01-15T12:00:00", nameof(FixIncorrectOwnerIdRelationships))]
 22[JellyfinMigrationBackup(JellyfinDb = true)]
 23public class FixIncorrectOwnerIdRelationships : IAsyncMigrationRoutine
 24{
 25    private readonly IStartupLogger<FixIncorrectOwnerIdRelationships> _logger;
 26    private readonly IDbContextFactory<JellyfinDbContext> _dbContextFactory;
 27    private readonly ILibraryManager _libraryManager;
 28    private readonly IItemPersistenceService _persistenceService;
 29
 30    /// <summary>
 31    /// Initializes a new instance of the <see cref="FixIncorrectOwnerIdRelationships"/> class.
 32    /// </summary>
 33    /// <param name="logger">The startup logger.</param>
 34    /// <param name="dbContextFactory">The database context factory.</param>
 35    /// <param name="libraryManager">The library manager.</param>
 36    /// <param name="persistenceService">The item persistence service.</param>
 37    public FixIncorrectOwnerIdRelationships(
 38        IStartupLogger<FixIncorrectOwnerIdRelationships> logger,
 39        IDbContextFactory<JellyfinDbContext> dbContextFactory,
 40        ILibraryManager libraryManager,
 41        IItemPersistenceService persistenceService)
 42    {
 043        _logger = logger;
 044        _dbContextFactory = dbContextFactory;
 045        _libraryManager = libraryManager;
 046        _persistenceService = persistenceService;
 047    }
 48
 49    /// <inheritdoc/>
 50    public async Task PerformAsync(CancellationToken cancellationToken)
 51    {
 052        var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false);
 053        await using (context.ConfigureAwait(false))
 54        {
 55            // Step 1: Find and remove duplicate database entries (same Path, different IDs)
 056            await RemoveDuplicateItemsAsync(context, cancellationToken).ConfigureAwait(false);
 57
 58            // Step 2: Clear incorrect OwnerId for video/movie items that are children of other video/movie items
 059            await ClearIncorrectOwnerIdsAsync(context, cancellationToken).ConfigureAwait(false);
 60
 61            // Step 3: Reassign orphaned extras to correct parents
 062            await ReassignOrphanedExtrasAsync(context, cancellationToken).ConfigureAwait(false);
 63
 64            // Step 4: Populate PrimaryVersionId for alternate version children
 065            await PopulatePrimaryVersionIdAsync(context, cancellationToken).ConfigureAwait(false);
 66        }
 067    }
 68
 69    private async Task RemoveDuplicateItemsAsync(JellyfinDbContext context, CancellationToken cancellationToken)
 70    {
 71        // Find all paths that have duplicate entries
 072        var duplicatePaths = await context.BaseItems
 073            .Where(b => b.Path != null)
 074            .GroupBy(b => b.Path)
 075            .Where(g => g.Count() > 1)
 076            .Select(g => g.Key)
 077            .ToListAsync(cancellationToken)
 078            .ConfigureAwait(false);
 79
 080        if (duplicatePaths.Count == 0)
 81        {
 082            _logger.LogInformation("No duplicate items found, skipping duplicate removal.");
 083            return;
 84        }
 85
 086        _logger.LogInformation("Found {Count} paths with duplicate database entries", duplicatePaths.Count);
 87
 88        // Collect all duplicate IDs to delete in one batch
 089        var allIdsToDelete = new List<Guid>();
 90        const int progressLogStep = 500;
 091        var processedPaths = 0;
 092        foreach (var path in duplicatePaths)
 93        {
 094            cancellationToken.ThrowIfCancellationRequested();
 95
 096            if (processedPaths > 0 && processedPaths % progressLogStep == 0)
 97            {
 098                _logger.LogInformation("Resolving duplicates: {Processed}/{Total} paths", processedPaths, duplicatePaths
 99            }
 100
 0101            processedPaths++;
 102
 103            // Get all items with this path
 0104            var itemsWithPath = await context.BaseItems
 0105                .Where(b => b.Path == path)
 0106                .Select(b => new
 0107                {
 0108                    b.Id,
 0109                    b.Type,
 0110                    b.DateCreated,
 0111                    HasOwnedExtras = context.BaseItems.Any(c => c.OwnerId.HasValue && c.OwnerId.Value.Equals(b.Id)),
 0112                    HasDirectChildren = context.BaseItems.Any(c => c.ParentId.HasValue && c.ParentId.Value.Equals(b.Id))
 0113                })
 0114                .ToListAsync(cancellationToken)
 0115                .ConfigureAwait(false);
 116
 0117            if (itemsWithPath.Count <= 1)
 118            {
 119                continue;
 120            }
 121
 122            // Keep the item that has direct children, then owned extras, then prefer non-Folder types, then newest
 0123            var itemToKeep = itemsWithPath
 0124                .OrderByDescending(i => i.HasDirectChildren)
 0125                .ThenByDescending(i => i.HasOwnedExtras)
 0126                .ThenByDescending(i => i.Type != "MediaBrowser.Controller.Entities.Folder")
 0127                .ThenByDescending(i => i.DateCreated)
 0128                .First();
 0129            if (itemToKeep is null)
 130            {
 131                continue;
 132            }
 133
 0134            allIdsToDelete.AddRange(itemsWithPath.Where(i => !i.Id.Equals(itemToKeep.Id)).Select(i => i.Id));
 0135        }
 136
 0137        if (allIdsToDelete.Count > 0)
 138        {
 139            // Batch-resolve items for metadata path cleanup, then delete all at once
 0140            var itemsToDelete = allIdsToDelete
 0141                .Select(id => _libraryManager.GetItemById(id))
 0142                .Where(item => item is not null)
 0143                .ToList();
 0144            _libraryManager.DeleteItemsUnsafeFast(itemsToDelete!);
 145
 146            // Fall back to direct DB deletion for any items that couldn't be resolved via LibraryManager
 0147            var deletedIds = itemsToDelete.Select(i => i!.Id).ToHashSet();
 0148            var unresolvedIds = allIdsToDelete.Where(id => !deletedIds.Contains(id)).ToList();
 0149            if (unresolvedIds.Count > 0)
 150            {
 0151                _persistenceService.DeleteItem(unresolvedIds);
 152            }
 153        }
 154
 0155        _logger.LogInformation("Successfully removed {Count} duplicate database entries", allIdsToDelete.Count);
 0156    }
 157
 158    private async Task ClearIncorrectOwnerIdsAsync(JellyfinDbContext context, CancellationToken cancellationToken)
 159    {
 160        // Find video/movie items with incorrect OwnerId (ExtraType is NULL or 0, pointing to another video/movie)
 0161        var incorrectChildrenWithParent = await context.BaseItems
 0162            .Where(b => b.OwnerId.HasValue
 0163                && (b.ExtraType == null || b.ExtraType == 0)
 0164                && (b.Type == "MediaBrowser.Controller.Entities.Video" || b.Type == "MediaBrowser.Controller.Entities.Mo
 0165            .Where(b => context.BaseItems.Any(parent =>
 0166                parent.Id.Equals(b.OwnerId!.Value)
 0167                && (parent.Type == "MediaBrowser.Controller.Entities.Video" || parent.Type == "MediaBrowser.Controller.E
 0168            .ToListAsync(cancellationToken)
 0169            .ConfigureAwait(false);
 170
 171        // Also find orphaned items (parent doesn't exist)
 0172        var orphanedChildren = await context.BaseItems
 0173            .Where(b => b.OwnerId.HasValue
 0174                && (b.ExtraType == null || b.ExtraType == 0)
 0175                && (b.Type == "MediaBrowser.Controller.Entities.Video" || b.Type == "MediaBrowser.Controller.Entities.Mo
 0176            .Where(b => !context.BaseItems.Any(parent => parent.Id.Equals(b.OwnerId!.Value)))
 0177            .ToListAsync(cancellationToken)
 0178            .ConfigureAwait(false);
 179
 0180        var totalIncorrect = incorrectChildrenWithParent.Count + orphanedChildren.Count;
 0181        if (totalIncorrect == 0)
 182        {
 0183            _logger.LogInformation("No items with incorrect OwnerId found, skipping OwnerId cleanup.");
 0184            return;
 185        }
 186
 0187        _logger.LogInformation(
 0188            "Found {Count} video/movie items with incorrect OwnerId relationships ({WithParent} with parent, {Orphaned} 
 0189            totalIncorrect,
 0190            incorrectChildrenWithParent.Count,
 0191            orphanedChildren.Count);
 192
 193        // Clear OwnerId for all incorrect items
 0194        var allIncorrectItems = incorrectChildrenWithParent.Concat(orphanedChildren).ToList();
 0195        foreach (var item in allIncorrectItems)
 196        {
 0197            item.OwnerId = null;
 198        }
 199
 0200        await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
 0201        _logger.LogInformation("Successfully cleared OwnerId for {Count} items", totalIncorrect);
 0202    }
 203
 204    private async Task ReassignOrphanedExtrasAsync(JellyfinDbContext context, CancellationToken cancellationToken)
 205    {
 206        // Find extras whose parent was deleted during duplicate removal
 0207        var orphanedExtras = await context.BaseItems
 0208            .Where(b => b.ExtraType != null && b.ExtraType != 0 && b.OwnerId.HasValue)
 0209            .Where(b => !context.BaseItems.Any(parent => parent.Id.Equals(b.OwnerId!.Value)))
 0210            .ToListAsync(cancellationToken)
 0211            .ConfigureAwait(false);
 212
 0213        if (orphanedExtras.Count == 0)
 214        {
 0215            _logger.LogInformation("No orphaned extras found, skipping reassignment.");
 0216            return;
 217        }
 218
 0219        _logger.LogInformation("Found {Count} orphaned extras to reassign", orphanedExtras.Count);
 220        const int extraProgressLogStep = 500;
 221
 222        // Build a lookup of directory -> first video/movie item for parent resolution
 0223        var extraDirectories = orphanedExtras
 0224            .Where(e => !string.IsNullOrEmpty(e.Path))
 0225            .Select(e => System.IO.Path.GetDirectoryName(e.Path))
 0226            .Where(d => !string.IsNullOrEmpty(d))
 0227            .Distinct()
 0228            .ToList();
 229
 230        // Load all potential parent video/movies with paths in one query
 0231        var videoTypes = new[]
 0232        {
 0233            "MediaBrowser.Controller.Entities.Video",
 0234            "MediaBrowser.Controller.Entities.Movies.Movie"
 0235        };
 0236        var potentialParents = await context.BaseItems
 0237            .Where(b => b.Path != null && videoTypes.Contains(b.Type))
 0238            .Select(b => new { b.Id, b.Path })
 0239            .ToListAsync(cancellationToken)
 0240            .ConfigureAwait(false);
 241
 242        // Build directory -> parent ID mapping
 0243        var dirToParent = new Dictionary<string, Guid>();
 0244        foreach (var dir in extraDirectories)
 245        {
 0246            var parent = potentialParents
 0247                .Where(p => p.Path!.StartsWith(dir!, StringComparison.OrdinalIgnoreCase))
 0248                .OrderBy(p => p.Id)
 0249                .FirstOrDefault();
 0250            if (parent is not null)
 251            {
 0252                dirToParent[dir!] = parent.Id;
 253            }
 254        }
 255
 0256        var reassignedCount = 0;
 0257        var processedExtras = 0;
 0258        foreach (var extra in orphanedExtras)
 259        {
 0260            if (processedExtras > 0 && processedExtras % extraProgressLogStep == 0)
 261            {
 0262                _logger.LogInformation("Reassigning orphaned extras: {Processed}/{Total}", processedExtras, orphanedExtr
 263            }
 264
 0265            processedExtras++;
 266
 0267            if (string.IsNullOrEmpty(extra.Path))
 268            {
 269                continue;
 270            }
 271
 0272            var extraDirectory = System.IO.Path.GetDirectoryName(extra.Path);
 0273            if (!string.IsNullOrEmpty(extraDirectory) && dirToParent.TryGetValue(extraDirectory, out var parentId))
 274            {
 0275                extra.OwnerId = parentId;
 0276                reassignedCount++;
 277            }
 278            else
 279            {
 0280                extra.OwnerId = null;
 281            }
 282        }
 283
 0284        await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
 0285        _logger.LogInformation("Successfully reassigned {Count} orphaned extras", reassignedCount);
 0286    }
 287
 288    private async Task PopulatePrimaryVersionIdAsync(JellyfinDbContext context, CancellationToken cancellationToken)
 289    {
 290        // Find all alternate version relationships where child's PrimaryVersionId is not set
 291        // ChildType 2 = LocalAlternateVersion, ChildType 3 = LinkedAlternateVersion
 0292        var alternateVersionLinks = await context.LinkedChildren
 0293            .Where(lc => (lc.ChildType == Jellyfin.Database.Implementations.Entities.LinkedChildType.LocalAlternateVersi
 0294                       || lc.ChildType == Jellyfin.Database.Implementations.Entities.LinkedChildType.LinkedAlternateVers
 0295            .Join(
 0296                context.BaseItems,
 0297                lc => lc.ChildId,
 0298                item => item.Id,
 0299                (lc, item) => new { lc.ParentId, lc.ChildId, item.PrimaryVersionId })
 0300            .Where(x => !x.PrimaryVersionId.HasValue || !x.PrimaryVersionId.Value.Equals(x.ParentId))
 0301            .ToListAsync(cancellationToken)
 0302            .ConfigureAwait(false);
 303
 0304        if (alternateVersionLinks.Count == 0)
 305        {
 0306            _logger.LogInformation("No alternate version items need PrimaryVersionId population, skipping.");
 0307            return;
 308        }
 309
 0310        _logger.LogInformation("Found {Count} alternate version items that need PrimaryVersionId populated", alternateVe
 311
 312        // Batch-load all child items in a single query
 0313        var childIds = alternateVersionLinks.Select(l => l.ChildId).Distinct().ToList();
 0314        var childItems = await context.BaseItems
 0315            .WhereOneOrMany(childIds, b => b.Id)
 0316            .ToDictionaryAsync(b => b.Id, cancellationToken)
 0317            .ConfigureAwait(false);
 318
 0319        var updatedCount = 0;
 320        const int linkProgressLogStep = 1000;
 0321        var processedLinks = 0;
 0322        foreach (var link in alternateVersionLinks)
 323        {
 0324            if (processedLinks > 0 && processedLinks % linkProgressLogStep == 0)
 325            {
 0326                _logger.LogInformation("Populating PrimaryVersionId: {Processed}/{Total} links", processedLinks, alterna
 327            }
 328
 0329            processedLinks++;
 330
 0331            if (childItems.TryGetValue(link.ChildId, out var childItem))
 332            {
 0333                childItem.PrimaryVersionId = link.ParentId;
 0334                updatedCount++;
 335            }
 336        }
 337
 0338        await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
 0339        _logger.LogInformation("Successfully populated PrimaryVersionId for {Count} alternate version items", updatedCou
 0340    }
 341}