| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Jellyfin.Database.Implementations; |
| | | 7 | | using Jellyfin.Server.Migrations.Stages; |
| | | 8 | | using Jellyfin.Server.ServerSetupApp; |
| | | 9 | | using MediaBrowser.Controller.Channels; |
| | | 10 | | using MediaBrowser.Controller.Configuration; |
| | | 11 | | using MediaBrowser.Controller.Entities; |
| | | 12 | | using MediaBrowser.Controller.Library; |
| | | 13 | | using MediaBrowser.Controller.LiveTv; |
| | | 14 | | using MediaBrowser.Controller.MediaSegments; |
| | | 15 | | using MediaBrowser.Controller.Persistence; |
| | | 16 | | using MediaBrowser.Model.IO; |
| | | 17 | | using Microsoft.EntityFrameworkCore; |
| | | 18 | | using Microsoft.Extensions.Logging; |
| | | 19 | | |
| | | 20 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Removes orphaned extras (items with OwnerId pointing to non-existent items). |
| | | 24 | | /// Must run before EF migrations that add FK constraints on OwnerId. |
| | | 25 | | /// </summary> |
| | | 26 | | [JellyfinMigration("2026-01-13T23:00:00", nameof(CleanupOrphanedExtras), Stage = JellyfinMigrationStageTypes.CoreInitial |
| | | 27 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 28 | | public class CleanupOrphanedExtras : IAsyncMigrationRoutine |
| | | 29 | | { |
| | | 30 | | private readonly IStartupLogger<CleanupOrphanedExtras> _logger; |
| | | 31 | | private readonly IDbContextFactory<JellyfinDbContext> _dbContextFactory; |
| | | 32 | | private readonly ILibraryManager _libraryManager; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Initializes a new instance of the <see cref="CleanupOrphanedExtras"/> class. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="logger">The startup logger.</param> |
| | | 38 | | /// <param name="dbContextFactory">The database context factory.</param> |
| | | 39 | | /// <param name="libraryManager">The library manager.</param> |
| | | 40 | | /// <param name="itemRepository">The item repository.</param> |
| | | 41 | | /// <param name="itemCountService">The item count service.</param> |
| | | 42 | | /// <param name="channelManager">The channel manager.</param> |
| | | 43 | | /// <param name="recordingsManager">The recordings manager.</param> |
| | | 44 | | /// <param name="mediaSourceManager">The media source manager.</param> |
| | | 45 | | /// <param name="mediaSegmentManager">The media segments manager.</param> |
| | | 46 | | /// <param name="configurationManager">The configuration manager.</param> |
| | | 47 | | /// <param name="fileSystem">The file system.</param> |
| | | 48 | | public CleanupOrphanedExtras( |
| | | 49 | | IStartupLogger<CleanupOrphanedExtras> logger, |
| | | 50 | | IDbContextFactory<JellyfinDbContext> dbContextFactory, |
| | | 51 | | ILibraryManager libraryManager, |
| | | 52 | | IItemRepository itemRepository, |
| | | 53 | | IItemCountService itemCountService, |
| | | 54 | | IChannelManager channelManager, |
| | | 55 | | IRecordingsManager recordingsManager, |
| | | 56 | | IMediaSourceManager mediaSourceManager, |
| | | 57 | | IMediaSegmentManager mediaSegmentManager, |
| | | 58 | | IServerConfigurationManager configurationManager, |
| | | 59 | | IFileSystem fileSystem) |
| | | 60 | | { |
| | 0 | 61 | | _logger = logger; |
| | 0 | 62 | | _dbContextFactory = dbContextFactory; |
| | 0 | 63 | | _libraryManager = libraryManager; |
| | 0 | 64 | | BaseItem.LibraryManager ??= libraryManager; |
| | 0 | 65 | | BaseItem.ItemRepository ??= itemRepository; |
| | 0 | 66 | | BaseItem.ItemCountService ??= itemCountService; |
| | 0 | 67 | | BaseItem.ChannelManager ??= channelManager; |
| | 0 | 68 | | BaseItem.MediaSourceManager ??= mediaSourceManager; |
| | 0 | 69 | | BaseItem.MediaSegmentManager ??= mediaSegmentManager; |
| | 0 | 70 | | BaseItem.ConfigurationManager ??= configurationManager; |
| | 0 | 71 | | BaseItem.FileSystem ??= fileSystem; |
| | 0 | 72 | | Video.RecordingsManager ??= recordingsManager; |
| | 0 | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc/> |
| | | 76 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | | 77 | | { |
| | 0 | 78 | | var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 79 | | await using (context.ConfigureAwait(false)) |
| | | 80 | | { |
| | 0 | 81 | | var orphanedItemIds = await context.BaseItems |
| | 0 | 82 | | .Where(b => b.OwnerId.HasValue && !b.OwnerId.Value.Equals(Guid.Empty)) |
| | 0 | 83 | | .Where(b => !context.BaseItems.Any(parent => parent.Id.Equals(b.OwnerId!.Value))) |
| | 0 | 84 | | .Select(b => b.Id) |
| | 0 | 85 | | .ToListAsync(cancellationToken) |
| | 0 | 86 | | .ConfigureAwait(false); |
| | | 87 | | |
| | 0 | 88 | | if (orphanedItemIds.Count == 0) |
| | | 89 | | { |
| | 0 | 90 | | _logger.LogInformation("No orphaned extras found, skipping migration."); |
| | 0 | 91 | | return; |
| | | 92 | | } |
| | | 93 | | |
| | 0 | 94 | | _logger.LogInformation("Found {Count} orphaned extras to remove", orphanedItemIds.Count); |
| | | 95 | | |
| | | 96 | | // Batch-resolve items for metadata path cleanup, then delete all at once |
| | 0 | 97 | | var itemsToDelete = new List<BaseItem>(); |
| | 0 | 98 | | foreach (var itemId in orphanedItemIds) |
| | | 99 | | { |
| | 0 | 100 | | var item = _libraryManager.GetItemById(itemId); |
| | 0 | 101 | | if (item is not null) |
| | | 102 | | { |
| | 0 | 103 | | itemsToDelete.Add(item); |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | _libraryManager.DeleteItemsUnsafeFast(itemsToDelete); |
| | | 108 | | |
| | 0 | 109 | | _logger.LogInformation("Successfully removed {Count} orphaned extras", itemsToDelete.Count); |
| | | 110 | | } |
| | 0 | 111 | | } |
| | | 112 | | } |