| | | 1 | | #pragma warning disable RS0030 // Do not use banned APIs |
| | | 2 | | |
| | | 3 | | using System; |
| | | 4 | | using System.Collections.Generic; |
| | | 5 | | using System.Linq; |
| | | 6 | | using System.Threading; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using Jellyfin.Database.Implementations; |
| | | 9 | | using Jellyfin.Server.ServerSetupApp; |
| | | 10 | | using MediaBrowser.Controller.Library; |
| | | 11 | | using MediaBrowser.Controller.Persistence; |
| | | 12 | | using Microsoft.EntityFrameworkCore; |
| | | 13 | | using Microsoft.Extensions.Logging; |
| | | 14 | | |
| | | 15 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Merges case-only duplicate people. Two passes: |
| | | 19 | | /// 1) Person BaseItems whose Name differs only by casing — Person.GetPath hashes the name |
| | | 20 | | /// verbatim, so two casings produce two distinct Person rows in BaseItems. |
| | | 21 | | /// 2) Peoples lookup rows whose Name differs only by casing within the same PersonType — |
| | | 22 | | /// UpdatePeople used to insert a second Peoples row when a metadata provider returned |
| | | 23 | | /// a different casing than the row already in the table. |
| | | 24 | | /// Both bugs cause the /Persons endpoint to list the same person twice. |
| | | 25 | | /// </summary> |
| | | 26 | | [JellyfinMigration("2026-05-08T13:00:00", nameof(MergeDuplicatePeople))] |
| | | 27 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 28 | | public class MergeDuplicatePeople : IAsyncMigrationRoutine |
| | | 29 | | { |
| | | 30 | | private const string PersonType = "MediaBrowser.Controller.Entities.Person"; |
| | | 31 | | |
| | | 32 | | private readonly IStartupLogger<MergeDuplicatePeople> _logger; |
| | | 33 | | private readonly IDbContextFactory<JellyfinDbContext> _dbContextFactory; |
| | | 34 | | private readonly ILibraryManager _libraryManager; |
| | | 35 | | private readonly IItemPersistenceService _persistenceService; |
| | | 36 | | |
| | | 37 | | /// <summary> |
| | | 38 | | /// Initializes a new instance of the <see cref="MergeDuplicatePeople"/> class. |
| | | 39 | | /// </summary> |
| | | 40 | | /// <param name="logger">The startup logger.</param> |
| | | 41 | | /// <param name="dbContextFactory">The database context factory.</param> |
| | | 42 | | /// <param name="libraryManager">The library manager.</param> |
| | | 43 | | /// <param name="persistenceService">The item persistence service.</param> |
| | | 44 | | public MergeDuplicatePeople( |
| | | 45 | | IStartupLogger<MergeDuplicatePeople> logger, |
| | | 46 | | IDbContextFactory<JellyfinDbContext> dbContextFactory, |
| | | 47 | | ILibraryManager libraryManager, |
| | | 48 | | IItemPersistenceService persistenceService) |
| | | 49 | | { |
| | 0 | 50 | | _logger = logger; |
| | 0 | 51 | | _dbContextFactory = dbContextFactory; |
| | 0 | 52 | | _libraryManager = libraryManager; |
| | 0 | 53 | | _persistenceService = persistenceService; |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <inheritdoc/> |
| | | 57 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | | 58 | | { |
| | 0 | 59 | | var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 60 | | await using (context.ConfigureAwait(false)) |
| | | 61 | | { |
| | 0 | 62 | | await MergePersonBaseItemsAsync(context, cancellationToken).ConfigureAwait(false); |
| | 0 | 63 | | await MergePeoplesRowsAsync(context, cancellationToken).ConfigureAwait(false); |
| | | 64 | | } |
| | 0 | 65 | | } |
| | | 66 | | |
| | | 67 | | private async Task MergePersonBaseItemsAsync(JellyfinDbContext context, CancellationToken cancellationToken) |
| | | 68 | | { |
| | 0 | 69 | | var persons = await context.BaseItems |
| | 0 | 70 | | .Where(b => b.Type == PersonType && b.Name != null) |
| | 0 | 71 | | .Select(b => new { b.Id, b.Name, b.DateCreated }) |
| | 0 | 72 | | .ToListAsync(cancellationToken) |
| | 0 | 73 | | .ConfigureAwait(false); |
| | | 74 | | |
| | 0 | 75 | | var groups = persons |
| | 0 | 76 | | .GroupBy(p => p.Name!.ToLowerInvariant()) |
| | 0 | 77 | | .Where(g => g.Count() > 1) |
| | 0 | 78 | | .ToList(); |
| | | 79 | | |
| | 0 | 80 | | if (groups.Count == 0) |
| | | 81 | | { |
| | 0 | 82 | | _logger.LogInformation("No case-only duplicate Person BaseItems found."); |
| | 0 | 83 | | return; |
| | | 84 | | } |
| | | 85 | | |
| | 0 | 86 | | _logger.LogInformation("Found {Count} groups of case-only duplicate Person BaseItems.", groups.Count); |
| | | 87 | | |
| | 0 | 88 | | var idsToDelete = new List<Guid>(); |
| | 0 | 89 | | foreach (var group in groups) |
| | | 90 | | { |
| | 0 | 91 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 92 | | |
| | 0 | 93 | | var groupIds = group.Select(g => g.Id).ToArray(); |
| | | 94 | | |
| | | 95 | | // Pick the keeper: the Person with the most UserData rows (favorites, image |
| | | 96 | | // refresh state) is the one users have actually interacted with. |
| | 0 | 97 | | var stats = await context.BaseItems |
| | 0 | 98 | | .Where(b => groupIds.Contains(b.Id)) |
| | 0 | 99 | | .Select(b => new |
| | 0 | 100 | | { |
| | 0 | 101 | | b.Id, |
| | 0 | 102 | | b.Name, |
| | 0 | 103 | | b.DateCreated, |
| | 0 | 104 | | UserDataCount = context.UserData.Count(u => u.ItemId == b.Id), |
| | 0 | 105 | | LinkedCount = context.LinkedChildren.Count(l => l.ParentId == b.Id || l.ChildId == b.Id), |
| | 0 | 106 | | }) |
| | 0 | 107 | | .ToListAsync(cancellationToken) |
| | 0 | 108 | | .ConfigureAwait(false); |
| | | 109 | | |
| | 0 | 110 | | var keeper = stats |
| | 0 | 111 | | .OrderByDescending(s => s.UserDataCount) |
| | 0 | 112 | | .ThenByDescending(s => s.LinkedCount) |
| | 0 | 113 | | .ThenBy(s => s.DateCreated) |
| | 0 | 114 | | .First(); |
| | | 115 | | |
| | 0 | 116 | | foreach (var dup in stats.Where(s => s.Id != keeper.Id)) |
| | | 117 | | { |
| | 0 | 118 | | var keeperId = keeper.Id; |
| | 0 | 119 | | var dupId = dup.Id; |
| | | 120 | | |
| | 0 | 121 | | await context.BaseItems |
| | 0 | 122 | | .Where(b => b.ParentId == dupId) |
| | 0 | 123 | | .ExecuteUpdateAsync(s => s.SetProperty(b => b.ParentId, keeperId), cancellationToken) |
| | 0 | 124 | | .ConfigureAwait(false); |
| | | 125 | | |
| | 0 | 126 | | await context.BaseItems |
| | 0 | 127 | | .Where(b => b.OwnerId == dupId) |
| | 0 | 128 | | .ExecuteUpdateAsync(s => s.SetProperty(b => b.OwnerId, keeperId), cancellationToken) |
| | 0 | 129 | | .ConfigureAwait(false); |
| | | 130 | | |
| | 0 | 131 | | await context.AncestorIds |
| | 0 | 132 | | .Where(a => a.ParentItemId == dupId |
| | 0 | 133 | | && context.AncestorIds.Any(k => k.ParentItemId == keeperId && k.ItemId == a.ItemId)) |
| | 0 | 134 | | .ExecuteDeleteAsync(cancellationToken) |
| | 0 | 135 | | .ConfigureAwait(false); |
| | 0 | 136 | | await context.AncestorIds |
| | 0 | 137 | | .Where(a => a.ParentItemId == dupId) |
| | 0 | 138 | | .ExecuteUpdateAsync(s => s.SetProperty(a => a.ParentItemId, keeperId), cancellationToken) |
| | 0 | 139 | | .ConfigureAwait(false); |
| | | 140 | | |
| | 0 | 141 | | await context.LinkedChildren |
| | 0 | 142 | | .Where(l => l.ParentId == dupId |
| | 0 | 143 | | && context.LinkedChildren.Any(k => k.ParentId == keeperId && k.ChildId == l.ChildId)) |
| | 0 | 144 | | .ExecuteDeleteAsync(cancellationToken) |
| | 0 | 145 | | .ConfigureAwait(false); |
| | 0 | 146 | | await context.LinkedChildren |
| | 0 | 147 | | .Where(l => l.ParentId == dupId) |
| | 0 | 148 | | .ExecuteUpdateAsync(s => s.SetProperty(l => l.ParentId, keeperId), cancellationToken) |
| | 0 | 149 | | .ConfigureAwait(false); |
| | 0 | 150 | | await context.LinkedChildren |
| | 0 | 151 | | .Where(l => l.ChildId == dupId |
| | 0 | 152 | | && context.LinkedChildren.Any(k => k.ChildId == keeperId && k.ParentId == l.ParentId)) |
| | 0 | 153 | | .ExecuteDeleteAsync(cancellationToken) |
| | 0 | 154 | | .ConfigureAwait(false); |
| | 0 | 155 | | await context.LinkedChildren |
| | 0 | 156 | | .Where(l => l.ChildId == dupId) |
| | 0 | 157 | | .ExecuteUpdateAsync(s => s.SetProperty(l => l.ChildId, keeperId), cancellationToken) |
| | 0 | 158 | | .ConfigureAwait(false); |
| | | 159 | | |
| | 0 | 160 | | await context.UserData |
| | 0 | 161 | | .Where(u => u.ItemId == dupId |
| | 0 | 162 | | && context.UserData.Any(k => k.ItemId == keeperId && k.UserId == u.UserId && k.CustomDataKey == |
| | 0 | 163 | | .ExecuteDeleteAsync(cancellationToken) |
| | 0 | 164 | | .ConfigureAwait(false); |
| | 0 | 165 | | await context.UserData |
| | 0 | 166 | | .Where(u => u.ItemId == dupId) |
| | 0 | 167 | | .ExecuteUpdateAsync(s => s.SetProperty(u => u.ItemId, keeperId), cancellationToken) |
| | 0 | 168 | | .ConfigureAwait(false); |
| | | 169 | | |
| | 0 | 170 | | idsToDelete.Add(dupId); |
| | 0 | 171 | | } |
| | | 172 | | |
| | 0 | 173 | | _logger.LogDebug( |
| | 0 | 174 | | "Merged Person BaseItems for '{Name}' into {KeeperId} ({Removed} removed).", |
| | 0 | 175 | | keeper.Name, |
| | 0 | 176 | | keeper.Id, |
| | 0 | 177 | | stats.Count - 1); |
| | 0 | 178 | | } |
| | | 179 | | |
| | 0 | 180 | | if (idsToDelete.Count == 0) |
| | | 181 | | { |
| | 0 | 182 | | return; |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | // Resolve via LibraryManager so DeleteItemsUnsafeFast can also remove the |
| | | 186 | | // %MetadataPath%/People/<Letter>/<Name> directories the duplicate stubs left behind. |
| | 0 | 187 | | var itemsToDelete = idsToDelete |
| | 0 | 188 | | .Select(id => _libraryManager.GetItemById(id)) |
| | 0 | 189 | | .Where(item => item is not null) |
| | 0 | 190 | | .ToList(); |
| | 0 | 191 | | if (itemsToDelete.Count > 0) |
| | | 192 | | { |
| | 0 | 193 | | _libraryManager.DeleteItemsUnsafeFast(itemsToDelete!); |
| | | 194 | | } |
| | | 195 | | |
| | 0 | 196 | | var deletedIds = itemsToDelete.Select(i => i!.Id).ToHashSet(); |
| | 0 | 197 | | var unresolvedIds = idsToDelete.Where(id => !deletedIds.Contains(id)).ToList(); |
| | 0 | 198 | | if (unresolvedIds.Count > 0) |
| | | 199 | | { |
| | 0 | 200 | | _persistenceService.DeleteItem(unresolvedIds); |
| | | 201 | | } |
| | | 202 | | |
| | 0 | 203 | | _logger.LogInformation("Removed {Count} duplicate Person BaseItems.", idsToDelete.Count); |
| | 0 | 204 | | } |
| | | 205 | | |
| | | 206 | | private async Task MergePeoplesRowsAsync(JellyfinDbContext context, CancellationToken cancellationToken) |
| | | 207 | | { |
| | 0 | 208 | | var people = await context.Peoples |
| | 0 | 209 | | .Select(p => new { p.Id, p.Name, p.PersonType }) |
| | 0 | 210 | | .ToListAsync(cancellationToken) |
| | 0 | 211 | | .ConfigureAwait(false); |
| | | 212 | | |
| | 0 | 213 | | var groups = people |
| | 0 | 214 | | .GroupBy(p => (Name: p.Name.ToLowerInvariant(), p.PersonType)) |
| | 0 | 215 | | .Where(g => g.Count() > 1) |
| | 0 | 216 | | .ToList(); |
| | | 217 | | |
| | 0 | 218 | | if (groups.Count == 0) |
| | | 219 | | { |
| | 0 | 220 | | _logger.LogInformation("No case-only duplicate Peoples rows found."); |
| | 0 | 221 | | return; |
| | | 222 | | } |
| | | 223 | | |
| | 0 | 224 | | _logger.LogInformation("Found {Count} groups of case-only duplicate Peoples rows.", groups.Count); |
| | | 225 | | |
| | 0 | 226 | | var idsToDelete = new List<Guid>(); |
| | 0 | 227 | | foreach (var group in groups) |
| | | 228 | | { |
| | 0 | 229 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 230 | | |
| | 0 | 231 | | var groupIds = group.Select(g => g.Id).ToArray(); |
| | | 232 | | |
| | | 233 | | // Pick the keeper: the row referenced by the most BaseItems is the one most |
| | | 234 | | // tracks/movies already point at; the duplicates are usually orphan stubs left |
| | | 235 | | // by a casing-mismatched insert. |
| | 0 | 236 | | var stats = await context.Peoples |
| | 0 | 237 | | .Where(p => groupIds.Contains(p.Id)) |
| | 0 | 238 | | .Select(p => new |
| | 0 | 239 | | { |
| | 0 | 240 | | p.Id, |
| | 0 | 241 | | p.Name, |
| | 0 | 242 | | MapCount = context.PeopleBaseItemMap.Count(m => m.PeopleId == p.Id), |
| | 0 | 243 | | }) |
| | 0 | 244 | | .ToListAsync(cancellationToken) |
| | 0 | 245 | | .ConfigureAwait(false); |
| | | 246 | | |
| | 0 | 247 | | var keeper = stats |
| | 0 | 248 | | .OrderByDescending(s => s.MapCount) |
| | 0 | 249 | | .ThenBy(s => s.Id) |
| | 0 | 250 | | .First(); |
| | | 251 | | |
| | 0 | 252 | | foreach (var dup in stats.Where(s => s.Id != keeper.Id)) |
| | | 253 | | { |
| | 0 | 254 | | var keeperId = keeper.Id; |
| | 0 | 255 | | var dupId = dup.Id; |
| | | 256 | | |
| | | 257 | | // PeopleBaseItemMap PK is (ItemId, PeopleId, Role); drop dup rows that would |
| | | 258 | | // collide on (ItemId, Role) before redirecting PeopleId. Role is nullable, so |
| | | 259 | | // match nulls explicitly. |
| | 0 | 260 | | await context.PeopleBaseItemMap |
| | 0 | 261 | | .Where(m => m.PeopleId == dupId |
| | 0 | 262 | | && context.PeopleBaseItemMap.Any(k => k.PeopleId == keeperId |
| | 0 | 263 | | && k.ItemId == m.ItemId |
| | 0 | 264 | | && (k.Role == m.Role || (k.Role == null && m.Role == null)))) |
| | 0 | 265 | | .ExecuteDeleteAsync(cancellationToken) |
| | 0 | 266 | | .ConfigureAwait(false); |
| | 0 | 267 | | await context.PeopleBaseItemMap |
| | 0 | 268 | | .Where(m => m.PeopleId == dupId) |
| | 0 | 269 | | .ExecuteUpdateAsync(s => s.SetProperty(m => m.PeopleId, keeperId), cancellationToken) |
| | 0 | 270 | | .ConfigureAwait(false); |
| | | 271 | | |
| | 0 | 272 | | idsToDelete.Add(dupId); |
| | 0 | 273 | | } |
| | | 274 | | |
| | 0 | 275 | | _logger.LogDebug( |
| | 0 | 276 | | "Merged Peoples rows for '{Name}' into {KeeperId} ({Removed} removed).", |
| | 0 | 277 | | keeper.Name, |
| | 0 | 278 | | keeper.Id, |
| | 0 | 279 | | stats.Count - 1); |
| | 0 | 280 | | } |
| | | 281 | | |
| | 0 | 282 | | if (idsToDelete.Count == 0) |
| | | 283 | | { |
| | 0 | 284 | | return; |
| | | 285 | | } |
| | | 286 | | |
| | 0 | 287 | | var idx = 0; |
| | 0 | 288 | | foreach (var item in idsToDelete.Chunk(200)) |
| | | 289 | | { |
| | 0 | 290 | | idx++; // humans count at one |
| | 0 | 291 | | _logger.LogInformation("Remove batch {BatchNo}/{MaxBatches} duplicate Peoples.", idx, idsToDelete.Count / 20 |
| | 0 | 292 | | await context.Peoples |
| | 0 | 293 | | .Where(p => item.Contains(p.Id)) |
| | 0 | 294 | | .ExecuteDeleteAsync(cancellationToken) |
| | 0 | 295 | | .ConfigureAwait(false); |
| | | 296 | | } |
| | | 297 | | |
| | 0 | 298 | | _logger.LogInformation("Removed {Count} duplicate Peoples rows.", idsToDelete.Count); |
| | 0 | 299 | | } |
| | | 300 | | } |