| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Jellyfin.Data.Enums; |
| | | 7 | | using Jellyfin.Database.Implementations; |
| | | 8 | | using Jellyfin.Server.ServerSetupApp; |
| | | 9 | | using MediaBrowser.Controller.Entities; |
| | | 10 | | using MediaBrowser.Controller.Entities.TV; |
| | | 11 | | using MediaBrowser.Controller.Library; |
| | | 12 | | using Microsoft.EntityFrameworkCore; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | |
| | | 15 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Recomputes the presentation unique key for every series so existing items adopt the folder-set-free key format. |
| | | 19 | | /// </summary> |
| | | 20 | | [JellyfinMigration("2026-07-23T12:00:00", nameof(RecomputeSeriesPresentationKey))] |
| | | 21 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 22 | | internal class RecomputeSeriesPresentationKey : IAsyncMigrationRoutine |
| | | 23 | | { |
| | | 24 | | private readonly IStartupLogger<RecomputeSeriesPresentationKey> _logger; |
| | | 25 | | private readonly ILibraryManager _libraryManager; |
| | | 26 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="RecomputeSeriesPresentationKey"/> class. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="logger">The startup logger.</param> |
| | | 32 | | /// <param name="libraryManager">The library manager.</param> |
| | | 33 | | /// <param name="dbProvider">The database context factory.</param> |
| | | 34 | | public RecomputeSeriesPresentationKey( |
| | | 35 | | IStartupLogger<RecomputeSeriesPresentationKey> logger, |
| | | 36 | | ILibraryManager libraryManager, |
| | | 37 | | IDbContextFactory<JellyfinDbContext> dbProvider) |
| | | 38 | | { |
| | 0 | 39 | | _logger = logger; |
| | 0 | 40 | | _libraryManager = libraryManager; |
| | 0 | 41 | | _dbProvider = dbProvider; |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | | 46 | | { |
| | 0 | 47 | | var series = _libraryManager.GetItemList(new InternalItemsQuery |
| | 0 | 48 | | { |
| | 0 | 49 | | IncludeItemTypes = [BaseItemKind.Series] |
| | 0 | 50 | | }).OfType<Series>().ToArray(); |
| | | 51 | | |
| | 0 | 52 | | _logger.LogInformation("Recomputing presentation unique key for {Count} series", series.Length); |
| | | 53 | | |
| | | 54 | | const int ProgressInterval = 250; |
| | 0 | 55 | | var sw = Stopwatch.StartNew(); |
| | 0 | 56 | | var processed = 0; |
| | 0 | 57 | | var updated = 0; |
| | | 58 | | |
| | 0 | 59 | | var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 60 | | await using (dbContext.ConfigureAwait(false)) |
| | | 61 | | { |
| | 0 | 62 | | foreach (var item in series) |
| | | 63 | | { |
| | 0 | 64 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 65 | | |
| | 0 | 66 | | if (++processed % ProgressInterval == 0) |
| | | 67 | | { |
| | 0 | 68 | | _logger.LogInformation("Processed {Processed}/{Total} series - Updated: {Updated} - Time: {Elapsed}" |
| | | 69 | | } |
| | | 70 | | |
| | 0 | 71 | | var oldKey = item.PresentationUniqueKey; |
| | 0 | 72 | | var newKey = item.CreatePresentationUniqueKey(); |
| | 0 | 73 | | if (string.Equals(oldKey, newKey, StringComparison.Ordinal)) |
| | | 74 | | { |
| | | 75 | | continue; |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | // Write only the changed column instead of re-persisting the whole item. |
| | 0 | 79 | | var id = item.Id; |
| | 0 | 80 | | await dbContext.BaseItems |
| | 0 | 81 | | .Where(e => e.Id.Equals(id)) |
| | 0 | 82 | | .ExecuteUpdateAsync(e => e.SetProperty(f => f.PresentationUniqueKey, newKey), cancellationToken) |
| | 0 | 83 | | .ConfigureAwait(false); |
| | | 84 | | |
| | | 85 | | // Seasons and episodes cache the series key in SeriesPresentationUniqueKey and are matched |
| | | 86 | | // to the series by it. Re-point every child still carrying the old key in a single set-based |
| | | 87 | | // update so they stay attached without waiting for the next scan. |
| | 0 | 88 | | if (!string.IsNullOrEmpty(oldKey)) |
| | | 89 | | { |
| | 0 | 90 | | await dbContext.BaseItems |
| | 0 | 91 | | .Where(e => e.SeriesPresentationUniqueKey == oldKey) |
| | 0 | 92 | | .ExecuteUpdateAsync(e => e.SetProperty(f => f.SeriesPresentationUniqueKey, newKey), cancellation |
| | 0 | 93 | | .ConfigureAwait(false); |
| | | 94 | | } |
| | | 95 | | |
| | 0 | 96 | | updated++; |
| | 0 | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | 0 | 100 | | _logger.LogInformation("Recomputed presentation unique key for {Updated} of {Count} series in {Elapsed}", update |
| | 0 | 101 | | } |
| | | 102 | | } |