| | | 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 | | // Batch-resolve items for metadata path cleanup, then delete all at once |
| | 0 | 80 | | var itemsToDelete = new List<BaseItem>(); |
| | 0 | 81 | | foreach (var itemId in orphanedItemIds) |
| | | 82 | | { |
| | 0 | 83 | | itemsToDelete.Add(BaseItemMapper.DeserializeBaseItem( |
| | 0 | 84 | | new Database.Implementations.Entities.BaseItemEntity() |
| | 0 | 85 | | { |
| | 0 | 86 | | Id = itemId.Id, |
| | 0 | 87 | | Path = itemId.Path, |
| | 0 | 88 | | Type = itemId.Type |
| | 0 | 89 | | }, |
| | 0 | 90 | | _logger, |
| | 0 | 91 | | null, |
| | 0 | 92 | | true)!); |
| | | 93 | | } |
| | | 94 | | |
| | 0 | 95 | | _libraryManager.DeleteItemsUnsafeFast(itemsToDelete); |
| | | 96 | | |
| | 0 | 97 | | _logger.LogInformation("Successfully removed {Count} orphaned extras", itemsToDelete.Count); |
| | | 98 | | } |
| | 0 | 99 | | } |
| | | 100 | | } |