| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Diagnostics; |
| | | 4 | | using System.Globalization; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Jellyfin.Data.Enums; |
| | | 9 | | using Jellyfin.Database.Implementations; |
| | | 10 | | using Jellyfin.Server.ServerSetupApp; |
| | | 11 | | using MediaBrowser.Controller.Entities; |
| | | 12 | | using MediaBrowser.Controller.Entities.TV; |
| | | 13 | | using MediaBrowser.Controller.Library; |
| | | 14 | | using Microsoft.EntityFrameworkCore; |
| | | 15 | | using Microsoft.Extensions.Logging; |
| | | 16 | | |
| | | 17 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Recomputes the presentation unique key of every series and season so merged series are scoped to their own library. |
| | | 21 | | /// </summary> |
| | | 22 | | [JellyfinMigration("2026-08-21T12:00:00", nameof(RecomputeSeriesPresentationKey))] |
| | | 23 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 24 | | internal class RecomputeSeriesPresentationKey : IAsyncMigrationRoutine |
| | | 25 | | { |
| | | 26 | | private readonly IStartupLogger<RecomputeSeriesPresentationKey> _logger; |
| | | 27 | | private readonly ILibraryManager _libraryManager; |
| | | 28 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Initializes a new instance of the <see cref="RecomputeSeriesPresentationKey"/> class. |
| | | 32 | | /// </summary> |
| | | 33 | | /// <param name="logger">The startup logger.</param> |
| | | 34 | | /// <param name="libraryManager">The library manager.</param> |
| | | 35 | | /// <param name="dbProvider">The database context factory.</param> |
| | | 36 | | public RecomputeSeriesPresentationKey( |
| | | 37 | | IStartupLogger<RecomputeSeriesPresentationKey> logger, |
| | | 38 | | ILibraryManager libraryManager, |
| | | 39 | | IDbContextFactory<JellyfinDbContext> dbProvider) |
| | | 40 | | { |
| | 0 | 41 | | _logger = logger; |
| | 0 | 42 | | _libraryManager = libraryManager; |
| | 0 | 43 | | _dbProvider = dbProvider; |
| | 0 | 44 | | } |
| | | 45 | | |
| | | 46 | | /// <inheritdoc /> |
| | | 47 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | | 48 | | { |
| | 0 | 49 | | var series = _libraryManager.GetItemList(new InternalItemsQuery |
| | 0 | 50 | | { |
| | 0 | 51 | | IncludeItemTypes = [BaseItemKind.Series] |
| | 0 | 52 | | }).OfType<Series>().ToArray(); |
| | | 53 | | |
| | 0 | 54 | | _logger.LogInformation("Recomputing presentation unique key for {Count} series", series.Length); |
| | | 55 | | |
| | | 56 | | const int ProgressInterval = 250; |
| | 0 | 57 | | var sw = Stopwatch.StartNew(); |
| | 0 | 58 | | var newSeriesKeys = new Dictionary<Guid, string>(); |
| | 0 | 59 | | var processed = 0; |
| | 0 | 60 | | var updated = 0; |
| | | 61 | | |
| | 0 | 62 | | var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 63 | | await using (dbContext.ConfigureAwait(false)) |
| | | 64 | | { |
| | 0 | 65 | | foreach (var item in series) |
| | | 66 | | { |
| | 0 | 67 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 68 | | |
| | 0 | 69 | | if (++processed % ProgressInterval == 0) |
| | | 70 | | { |
| | 0 | 71 | | _logger.LogInformation("Processed {Processed}/{Total} series - Updated: {Updated} - Time: {Elapsed}" |
| | | 72 | | } |
| | | 73 | | |
| | 0 | 74 | | var newKey = item.CreatePresentationUniqueKey(); |
| | 0 | 75 | | newSeriesKeys[item.Id] = newKey; |
| | | 76 | | |
| | 0 | 77 | | if (string.Equals(item.PresentationUniqueKey, newKey, StringComparison.Ordinal)) |
| | | 78 | | { |
| | | 79 | | continue; |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | // Write only the changed column instead of re-persisting the whole item. |
| | 0 | 83 | | var id = item.Id; |
| | 0 | 84 | | await dbContext.BaseItems |
| | 0 | 85 | | .Where(e => e.Id.Equals(id)) |
| | 0 | 86 | | .ExecuteUpdateAsync(e => e.SetProperty(f => f.PresentationUniqueKey, newKey), cancellationToken) |
| | 0 | 87 | | .ConfigureAwait(false); |
| | | 88 | | |
| | | 89 | | // Seasons and episodes are matched to their series by SeriesPresentationUniqueKey, so |
| | | 90 | | // re-point them here instead of waiting for the next scan. Scoped by SeriesId rather than |
| | | 91 | | // by the old key: that key can be shared by every library holding the series, so matching |
| | | 92 | | // on it would drag the other libraries' children along. |
| | 0 | 93 | | await dbContext.BaseItems |
| | 0 | 94 | | .Where(e => e.SeriesId.HasValue && e.SeriesId.Value.Equals(id)) |
| | 0 | 95 | | .ExecuteUpdateAsync(e => e.SetProperty(f => f.SeriesPresentationUniqueKey, newKey), cancellationToke |
| | 0 | 96 | | .ConfigureAwait(false); |
| | | 97 | | |
| | 0 | 98 | | updated++; |
| | 0 | 99 | | } |
| | | 100 | | |
| | 0 | 101 | | var updatedSeasons = await RecomputeSeasonsAsync(dbContext, newSeriesKeys, cancellationToken).ConfigureAwait |
| | | 102 | | |
| | 0 | 103 | | _logger.LogInformation( |
| | 0 | 104 | | "Recomputed presentation unique key for {Updated} of {Count} series and {UpdatedSeasons} seasons in {Ela |
| | 0 | 105 | | updated, |
| | 0 | 106 | | series.Length, |
| | 0 | 107 | | updatedSeasons, |
| | 0 | 108 | | sw.Elapsed); |
| | | 109 | | } |
| | 0 | 110 | | } |
| | | 111 | | |
| | | 112 | | private async Task<int> RecomputeSeasonsAsync(JellyfinDbContext dbContext, Dictionary<Guid, string> newSeriesKeys, C |
| | | 113 | | { |
| | | 114 | | // A season's own key embeds its series' key, so it goes stale with it. |
| | 0 | 115 | | var seasons = _libraryManager.GetItemList(new InternalItemsQuery |
| | 0 | 116 | | { |
| | 0 | 117 | | IncludeItemTypes = [BaseItemKind.Season] |
| | 0 | 118 | | }).OfType<Season>().ToArray(); |
| | | 119 | | |
| | 0 | 120 | | var updated = 0; |
| | | 121 | | |
| | 0 | 122 | | foreach (var season in seasons) |
| | | 123 | | { |
| | 0 | 124 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 125 | | |
| | | 126 | | // Without an index number the season keeps the base key, which carries no series key at all. |
| | 0 | 127 | | if (!season.IndexNumber.HasValue |
| | 0 | 128 | | || !newSeriesKeys.TryGetValue(season.SeriesId, out var seriesKey)) |
| | | 129 | | { |
| | | 130 | | continue; |
| | | 131 | | } |
| | | 132 | | |
| | | 133 | | // Mirrors Season.CreatePresentationUniqueKey. |
| | 0 | 134 | | var newKey = seriesKey + "-" + season.IndexNumber.Value.ToString("000", CultureInfo.InvariantCulture); |
| | 0 | 135 | | if (string.Equals(season.PresentationUniqueKey, newKey, StringComparison.Ordinal)) |
| | | 136 | | { |
| | | 137 | | continue; |
| | | 138 | | } |
| | | 139 | | |
| | 0 | 140 | | var id = season.Id; |
| | 0 | 141 | | await dbContext.BaseItems |
| | 0 | 142 | | .Where(e => e.Id.Equals(id)) |
| | 0 | 143 | | .ExecuteUpdateAsync(e => e.SetProperty(f => f.PresentationUniqueKey, newKey), cancellationToken) |
| | 0 | 144 | | .ConfigureAwait(false); |
| | | 145 | | |
| | 0 | 146 | | updated++; |
| | | 147 | | } |
| | | 148 | | |
| | 0 | 149 | | return updated; |
| | 0 | 150 | | } |
| | | 151 | | } |