| | | 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.Implementations.Item; |
| | | 8 | | using Jellyfin.Server.Migrations.Stages; |
| | | 9 | | using Jellyfin.Server.ServerSetupApp; |
| | | 10 | | using MediaBrowser.Controller.Channels; |
| | | 11 | | using MediaBrowser.Controller.Configuration; |
| | | 12 | | using MediaBrowser.Controller.Entities; |
| | | 13 | | using MediaBrowser.Controller.Library; |
| | | 14 | | using MediaBrowser.Controller.LiveTv; |
| | | 15 | | using MediaBrowser.Controller.MediaSegments; |
| | | 16 | | using MediaBrowser.Controller.Persistence; |
| | | 17 | | using MediaBrowser.Model.IO; |
| | | 18 | | using Microsoft.EntityFrameworkCore; |
| | | 19 | | using Microsoft.Extensions.Logging; |
| | | 20 | | |
| | | 21 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Removes orphaned extras (items with OwnerId pointing to non-existent items). |
| | | 25 | | /// Must run before EF migrations that add FK constraints on OwnerId. |
| | | 26 | | /// </summary> |
| | | 27 | | [JellyfinMigration("2026-01-13T23:00:00", nameof(CleanupOrphanedExtras), Stage = JellyfinMigrationStageTypes.AppInitiali |
| | | 28 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 29 | | public class CleanupOrphanedExtras : IAsyncMigrationRoutine |
| | | 30 | | { |
| | | 31 | | private readonly IStartupLogger<CleanupOrphanedExtras> _logger; |
| | | 32 | | private readonly IDbContextFactory<JellyfinDbContext> _dbContextFactory; |
| | | 33 | | private readonly ILibraryManager _libraryManager; |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Initializes a new instance of the <see cref="CleanupOrphanedExtras"/> class. |
| | | 37 | | /// </summary> |
| | | 38 | | /// <param name="logger">The startup logger.</param> |
| | | 39 | | /// <param name="dbContextFactory">The database context factory.</param> |
| | | 40 | | /// <param name="libraryManager">The library manager.</param> |
| | | 41 | | public CleanupOrphanedExtras( |
| | | 42 | | IStartupLogger<CleanupOrphanedExtras> logger, |
| | | 43 | | IDbContextFactory<JellyfinDbContext> dbContextFactory, |
| | | 44 | | ILibraryManager libraryManager) |
| | | 45 | | { |
| | 0 | 46 | | _logger = logger; |
| | 0 | 47 | | _dbContextFactory = dbContextFactory; |
| | 0 | 48 | | _libraryManager = libraryManager; |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | /// <inheritdoc/> |
| | | 52 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | | 53 | | { |
| | 0 | 54 | | var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 55 | | await using (context.ConfigureAwait(false)) |
| | | 56 | | { |
| | 0 | 57 | | var placeholderOwner = Guid.Parse("00000000-0000-0000-0000-000000000001"); |
| | | 58 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | 0 | 59 | | var orphanedItemIds = await context.BaseItems |
| | 0 | 60 | | .Where(b => b.OwnerId.HasValue && b.OwnerId == placeholderOwner) |
| | 0 | 61 | | .Select(b => new |
| | 0 | 62 | | { |
| | 0 | 63 | | b.Id, |
| | 0 | 64 | | b.Path, |
| | 0 | 65 | | b.Type |
| | 0 | 66 | | }) |
| | 0 | 67 | | .ToListAsync(cancellationToken) |
| | 0 | 68 | | .ConfigureAwait(false); |
| | | 69 | | #pragma warning restore RS0030 // Do not use banned APIs |
| | | 70 | | |
| | 0 | 71 | | if (orphanedItemIds.Count == 0) |
| | | 72 | | { |
| | 0 | 73 | | _logger.LogInformation("No orphaned extras found, skipping migration."); |
| | 0 | 74 | | return; |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | _logger.LogInformation("Found {Count} orphaned extras to remove", orphanedItemIds.Count); |
| | | 78 | | |
| | | 79 | | // Resolve items for metadata path cleanup, then delete in batches so we never issue one |
| | | 80 | | // massive delete transaction and progress stays visible on large libraries. |
| | 0 | 81 | | _logger.LogInformation("Deleting {Count} orphaned extras...", orphanedItemIds.Count); |
| | | 82 | | const int deleteBatchSize = 500; |
| | 0 | 83 | | var deletedSoFar = 0; |
| | 0 | 84 | | for (var offset = 0; offset < orphanedItemIds.Count; offset += deleteBatchSize) |
| | | 85 | | { |
| | 0 | 86 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 87 | | |
| | 0 | 88 | | var batch = orphanedItemIds.GetRange(offset, Math.Min(deleteBatchSize, orphanedItemIds.Count - offset)); |
| | 0 | 89 | | var itemsToDelete = batch |
| | 0 | 90 | | .Select(itemId => BaseItemMapper.DeserializeBaseItem( |
| | 0 | 91 | | new Database.Implementations.Entities.BaseItemEntity() |
| | 0 | 92 | | { |
| | 0 | 93 | | Id = itemId.Id, |
| | 0 | 94 | | Path = itemId.Path, |
| | 0 | 95 | | Type = itemId.Type |
| | 0 | 96 | | }, |
| | 0 | 97 | | _logger, |
| | 0 | 98 | | null, |
| | 0 | 99 | | true)!) |
| | 0 | 100 | | .ToList(); |
| | | 101 | | |
| | 0 | 102 | | _libraryManager.DeleteItemsUnsafeFast(itemsToDelete); |
| | | 103 | | |
| | 0 | 104 | | deletedSoFar += batch.Count; |
| | 0 | 105 | | _logger.LogInformation("Deleting orphaned extras: {Deleted}/{Total}", deletedSoFar, orphanedItemIds.Coun |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | _logger.LogInformation("Successfully removed {Count} orphaned extras", orphanedItemIds.Count); |
| | | 109 | | } |
| | 0 | 110 | | } |
| | | 111 | | } |