| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Threading; |
| | | 6 | | using System.Threading.Tasks; |
| | | 7 | | using Jellyfin.Database.Implementations; |
| | | 8 | | using Jellyfin.Server.ServerSetupApp; |
| | | 9 | | using Microsoft.EntityFrameworkCore; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | using LinkedChildType = Jellyfin.Database.Implementations.Entities.LinkedChildType; |
| | | 12 | | |
| | | 13 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Re-points every video that is linked as an alternate version at the primary it belongs to. |
| | | 17 | | /// </summary> |
| | | 18 | | [JellyfinMigration("2026-09-08T12:00:00", nameof(RepairAlternateVersionLinks))] |
| | | 19 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 20 | | internal class RepairAlternateVersionLinks : IAsyncMigrationRoutine |
| | | 21 | | { |
| | | 22 | | private const int BatchSize = 1000; |
| | | 23 | | |
| | | 24 | | private readonly IStartupLogger<RepairAlternateVersionLinks> _logger; |
| | | 25 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="RepairAlternateVersionLinks"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="logger">The startup logger.</param> |
| | | 31 | | /// <param name="dbProvider">The database context factory.</param> |
| | | 32 | | public RepairAlternateVersionLinks( |
| | | 33 | | IStartupLogger<RepairAlternateVersionLinks> logger, |
| | | 34 | | IDbContextFactory<JellyfinDbContext> dbProvider) |
| | | 35 | | { |
| | 5 | 36 | | _logger = logger; |
| | 5 | 37 | | _dbProvider = dbProvider; |
| | 5 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | | 42 | | { |
| | 5 | 43 | | var dbContext = await _dbProvider.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 5 | 44 | | await using (dbContext.ConfigureAwait(false)) |
| | | 45 | | { |
| | 5 | 46 | | var links = await dbContext.LinkedChildren |
| | 5 | 47 | | .Where(lc => lc.ChildType == LinkedChildType.LocalAlternateVersion |
| | 5 | 48 | | || lc.ChildType == LinkedChildType.LinkedAlternateVersion) |
| | 5 | 49 | | .Select(lc => new { lc.ParentId, lc.ChildId, lc.ChildType }) |
| | 5 | 50 | | .ToListAsync(cancellationToken) |
| | 5 | 51 | | .ConfigureAwait(false); |
| | | 52 | | |
| | 5 | 53 | | if (links.Count == 0) |
| | | 54 | | { |
| | 0 | 55 | | _logger.LogInformation("No alternate version links found, nothing to repair."); |
| | 0 | 56 | | return; |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | // A version belongs to one primary; a file-based link outranks a user-merged one, as it |
| | | 60 | | // does everywhere else these two link types meet. |
| | 5 | 61 | | var primaryByChild = links |
| | 5 | 62 | | .GroupBy(l => l.ChildId) |
| | 5 | 63 | | .ToDictionary( |
| | 5 | 64 | | g => g.Key, |
| | 5 | 65 | | g => g.OrderBy(l => l.ChildType == LinkedChildType.LocalAlternateVersion ? 0 : 1) |
| | 5 | 66 | | .First() |
| | 5 | 67 | | .ParentId); |
| | | 68 | | |
| | | 69 | | // A version that is its own primary would be hidden from every list by the repair below. |
| | 12 | 70 | | foreach (var selfLink in primaryByChild.Where(kvp => kvp.Value.Equals(kvp.Key)).ToList()) |
| | | 71 | | { |
| | 1 | 72 | | _logger.LogWarning("Skipping alternate version {ChildId}, which is linked to itself.", selfLink.Key); |
| | 1 | 73 | | primaryByChild.Remove(selfLink.Key); |
| | | 74 | | } |
| | | 75 | | |
| | 5 | 76 | | ResolvePrimaries(primaryByChild); |
| | | 77 | | |
| | 5 | 78 | | var repaired = 0; |
| | 5 | 79 | | var promoted = 0; |
| | | 80 | | |
| | | 81 | | // The primaries are loaded along with their versions: a primary that carries a |
| | | 82 | | // PrimaryVersionId of its own hides the whole group it heads. |
| | 5 | 83 | | var itemIds = primaryByChild.Keys.Concat(primaryByChild.Values).Distinct().ToList(); |
| | 18 | 84 | | for (var offset = 0; offset < itemIds.Count; offset += BatchSize) |
| | | 85 | | { |
| | 4 | 86 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 87 | | |
| | 4 | 88 | | var batch = itemIds.GetRange(offset, Math.Min(BatchSize, itemIds.Count - offset)); |
| | 4 | 89 | | var items = await dbContext.BaseItems |
| | 4 | 90 | | .Where(e => batch.Contains(e.Id)) |
| | 4 | 91 | | .ToListAsync(cancellationToken) |
| | 4 | 92 | | .ConfigureAwait(false); |
| | | 93 | | |
| | 26 | 94 | | foreach (var item in items) |
| | | 95 | | { |
| | 9 | 96 | | if (primaryByChild.TryGetValue(item.Id, out var primaryId)) |
| | | 97 | | { |
| | | 98 | | // Mirrors Video.CreatePresentationUniqueKey for a video that has a primary. |
| | 5 | 99 | | var expectedKey = primaryId.ToString("N", CultureInfo.InvariantCulture); |
| | 5 | 100 | | if (item.PrimaryVersionId.HasValue |
| | 5 | 101 | | && primaryId.Equals(item.PrimaryVersionId.Value) |
| | 5 | 102 | | && string.Equals(item.PresentationUniqueKey, expectedKey, StringComparison.Ordinal)) |
| | | 103 | | { |
| | | 104 | | continue; |
| | | 105 | | } |
| | | 106 | | |
| | 5 | 107 | | item.PrimaryVersionId = primaryId; |
| | 5 | 108 | | item.PresentationUniqueKey = expectedKey; |
| | 5 | 109 | | repaired++; |
| | | 110 | | } |
| | 4 | 111 | | else if (item.PrimaryVersionId.HasValue) |
| | | 112 | | { |
| | 1 | 113 | | if (item.OwnerId.HasValue) |
| | | 114 | | { |
| | | 115 | | // An owned item is hidden by its owner rather than by its primary, so |
| | | 116 | | // clearing the primary here would not bring the group back. |
| | 0 | 117 | | _logger.LogWarning( |
| | 0 | 118 | | "Alternate versions are linked to {ItemId}, which is owned by {OwnerId}; the group stays |
| | 0 | 119 | | item.Id, |
| | 0 | 120 | | item.OwnerId.Value); |
| | 0 | 121 | | continue; |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | // Nothing links this one as a version, so the leftover primary is stale and |
| | | 125 | | // would hide it, and with it every version linked to it. |
| | 1 | 126 | | _logger.LogWarning( |
| | 1 | 127 | | "Clearing the stale primary {PrimaryVersionId} of {ItemId}, which other versions are linked |
| | 1 | 128 | | item.PrimaryVersionId.Value, |
| | 1 | 129 | | item.Id); |
| | | 130 | | |
| | 1 | 131 | | item.PrimaryVersionId = null; |
| | 1 | 132 | | item.PresentationUniqueKey = item.Id.ToString("N", CultureInfo.InvariantCulture); |
| | 1 | 133 | | promoted++; |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | |
| | 4 | 137 | | await dbContext.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| | | 138 | | } |
| | | 139 | | |
| | 5 | 140 | | _logger.LogInformation( |
| | 5 | 141 | | "Repaired {Repaired} of {Total} alternate version links, and promoted {Promoted} primaries that were ver |
| | 5 | 142 | | repaired, |
| | 5 | 143 | | primaryByChild.Count, |
| | 5 | 144 | | promoted); |
| | 5 | 145 | | } |
| | 5 | 146 | | } |
| | | 147 | | |
| | | 148 | | private void ResolvePrimaries(Dictionary<Guid, Guid> primaryByChild) |
| | | 149 | | { |
| | 5 | 150 | | var resolvedPrimaries = new Dictionary<Guid, Guid>(primaryByChild.Count); |
| | | 151 | | |
| | 22 | 152 | | foreach (var start in primaryByChild.Keys.ToList()) |
| | | 153 | | { |
| | 6 | 154 | | if (resolvedPrimaries.ContainsKey(start)) |
| | | 155 | | { |
| | | 156 | | continue; |
| | | 157 | | } |
| | | 158 | | |
| | 5 | 159 | | var chain = new List<Guid>(); |
| | 5 | 160 | | var walked = new HashSet<Guid>(); |
| | 5 | 161 | | var current = start; |
| | | 162 | | Guid primary; |
| | | 163 | | |
| | 6 | 164 | | while (true) |
| | | 165 | | { |
| | 11 | 166 | | if (resolvedPrimaries.TryGetValue(current, out var resolved)) |
| | | 167 | | { |
| | 1 | 168 | | primary = resolved; |
| | 1 | 169 | | break; |
| | | 170 | | } |
| | | 171 | | |
| | 10 | 172 | | if (!primaryByChild.TryGetValue(current, out var next)) |
| | | 173 | | { |
| | | 174 | | // Nothing is linking this one as a version of something else, so it heads the group. |
| | 3 | 175 | | primary = current; |
| | 3 | 176 | | break; |
| | | 177 | | } |
| | | 178 | | |
| | 7 | 179 | | if (!walked.Add(current)) |
| | | 180 | | { |
| | 1 | 181 | | var loop = chain.Skip(chain.IndexOf(current)).ToList(); |
| | | 182 | | |
| | | 183 | | // Which member heads the group is arbitrary; the lowest id keeps the repair |
| | | 184 | | // stable if the migration is ever re-run over the same data. |
| | 1 | 185 | | primary = loop.Min(); |
| | 1 | 186 | | _logger.LogWarning( |
| | 1 | 187 | | "Alternate version links form a loop ({Loop}); keeping {PrimaryId} as the primary of the group." |
| | 1 | 188 | | string.Join(" -> ", loop), |
| | 1 | 189 | | primary); |
| | | 190 | | |
| | 1 | 191 | | primaryByChild.Remove(primary); |
| | 1 | 192 | | break; |
| | | 193 | | } |
| | | 194 | | |
| | 6 | 195 | | chain.Add(current); |
| | 6 | 196 | | current = next; |
| | | 197 | | } |
| | | 198 | | |
| | 22 | 199 | | foreach (var version in chain) |
| | | 200 | | { |
| | 6 | 201 | | resolvedPrimaries[version] = primary; |
| | | 202 | | } |
| | | 203 | | } |
| | | 204 | | |
| | 22 | 205 | | foreach (var (version, primary) in resolvedPrimaries) |
| | | 206 | | { |
| | 6 | 207 | | if (primary.Equals(version)) |
| | | 208 | | { |
| | | 209 | | // The member of a loop that was kept as the primary of its group. |
| | | 210 | | continue; |
| | | 211 | | } |
| | | 212 | | |
| | 5 | 213 | | primaryByChild[version] = primary; |
| | | 214 | | } |
| | 5 | 215 | | } |
| | | 216 | | } |