| | | 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 MusicArtist records that differ only by Name casing. Prior to the case-insensitive |
| | | 19 | | /// dedup lookup added alongside this migration, the artist validator would create a second |
| | | 20 | | /// MusicArtist whenever a track tagged the artist with a different casing than the |
| | | 21 | | /// resolver-created one (e.g. "Thirty Seconds To Mars" vs. "Thirty Seconds to Mars"). |
| | | 22 | | /// </summary> |
| | | 23 | | [JellyfinMigration("2026-05-08T12:00:00", nameof(MergeDuplicateMusicArtists))] |
| | | 24 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 25 | | public class MergeDuplicateMusicArtists : IAsyncMigrationRoutine |
| | | 26 | | { |
| | | 27 | | private const string MusicArtistType = "MediaBrowser.Controller.Entities.Audio.MusicArtist"; |
| | | 28 | | |
| | | 29 | | private readonly IStartupLogger<MergeDuplicateMusicArtists> _logger; |
| | | 30 | | private readonly IDbContextFactory<JellyfinDbContext> _dbContextFactory; |
| | | 31 | | private readonly ILibraryManager _libraryManager; |
| | | 32 | | private readonly IItemPersistenceService _persistenceService; |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Initializes a new instance of the <see cref="MergeDuplicateMusicArtists"/> class. |
| | | 36 | | /// </summary> |
| | | 37 | | /// <param name="logger">The startup logger.</param> |
| | | 38 | | /// <param name="dbContextFactory">The database context factory.</param> |
| | | 39 | | /// <param name="libraryManager">The library manager.</param> |
| | | 40 | | /// <param name="persistenceService">The item persistence service.</param> |
| | | 41 | | public MergeDuplicateMusicArtists( |
| | | 42 | | IStartupLogger<MergeDuplicateMusicArtists> logger, |
| | | 43 | | IDbContextFactory<JellyfinDbContext> dbContextFactory, |
| | | 44 | | ILibraryManager libraryManager, |
| | | 45 | | IItemPersistenceService persistenceService) |
| | | 46 | | { |
| | 0 | 47 | | _logger = logger; |
| | 0 | 48 | | _dbContextFactory = dbContextFactory; |
| | 0 | 49 | | _libraryManager = libraryManager; |
| | 0 | 50 | | _persistenceService = persistenceService; |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc/> |
| | | 54 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | | 55 | | { |
| | 0 | 56 | | var context = await _dbContextFactory.CreateDbContextAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 57 | | await using (context.ConfigureAwait(false)) |
| | | 58 | | { |
| | 0 | 59 | | var artists = await context.BaseItems |
| | 0 | 60 | | .Where(b => b.Type == MusicArtistType && b.Name != null) |
| | 0 | 61 | | .Select(b => new { b.Id, b.Name, b.DateCreated }) |
| | 0 | 62 | | .ToListAsync(cancellationToken) |
| | 0 | 63 | | .ConfigureAwait(false); |
| | | 64 | | |
| | 0 | 65 | | var groups = artists |
| | 0 | 66 | | .GroupBy(a => a.Name!.ToLowerInvariant()) |
| | 0 | 67 | | .Where(g => g.Count() > 1) |
| | 0 | 68 | | .ToList(); |
| | | 69 | | |
| | 0 | 70 | | if (groups.Count == 0) |
| | | 71 | | { |
| | 0 | 72 | | _logger.LogInformation("No case-only duplicate MusicArtist records found."); |
| | 0 | 73 | | return; |
| | | 74 | | } |
| | | 75 | | |
| | 0 | 76 | | _logger.LogInformation("Found {Count} groups of case-only duplicate MusicArtist records.", groups.Count); |
| | | 77 | | |
| | 0 | 78 | | var idsToDelete = new List<Guid>(); |
| | 0 | 79 | | foreach (var group in groups) |
| | | 80 | | { |
| | 0 | 81 | | cancellationToken.ThrowIfCancellationRequested(); |
| | | 82 | | |
| | 0 | 83 | | var groupIds = group.Select(g => g.Id).ToArray(); |
| | | 84 | | |
| | | 85 | | // Pick the keeper: the artist with the most child references is the "real" one |
| | | 86 | | // (the resolver-created artist with a filesystem path); the duplicates are usually |
| | | 87 | | // empty stubs created by the validator's case-sensitive miss. |
| | 0 | 88 | | var stats = await context.BaseItems |
| | 0 | 89 | | .Where(b => groupIds.Contains(b.Id)) |
| | 0 | 90 | | .Select(b => new |
| | 0 | 91 | | { |
| | 0 | 92 | | b.Id, |
| | 0 | 93 | | b.Name, |
| | 0 | 94 | | b.DateCreated, |
| | 0 | 95 | | ChildCount = context.BaseItems.Count(c => c.ParentId == b.Id), |
| | 0 | 96 | | AncestorCount = context.AncestorIds.Count(a => a.ParentItemId == b.Id), |
| | 0 | 97 | | LinkedCount = context.LinkedChildren.Count(l => l.ParentId == b.Id || l.ChildId == b.Id), |
| | 0 | 98 | | }) |
| | 0 | 99 | | .ToListAsync(cancellationToken) |
| | 0 | 100 | | .ConfigureAwait(false); |
| | | 101 | | |
| | 0 | 102 | | var keeper = stats |
| | 0 | 103 | | .OrderByDescending(s => s.ChildCount) |
| | 0 | 104 | | .ThenByDescending(s => s.AncestorCount) |
| | 0 | 105 | | .ThenByDescending(s => s.LinkedCount) |
| | 0 | 106 | | .ThenBy(s => s.DateCreated) |
| | 0 | 107 | | .First(); |
| | | 108 | | |
| | 0 | 109 | | foreach (var dup in stats.Where(s => s.Id != keeper.Id)) |
| | | 110 | | { |
| | 0 | 111 | | var keeperId = keeper.Id; |
| | 0 | 112 | | var dupId = dup.Id; |
| | | 113 | | |
| | 0 | 114 | | await context.BaseItems |
| | 0 | 115 | | .Where(b => b.ParentId == dupId) |
| | 0 | 116 | | .ExecuteUpdateAsync(s => s.SetProperty(b => b.ParentId, keeperId), cancellationToken) |
| | 0 | 117 | | .ConfigureAwait(false); |
| | | 118 | | |
| | 0 | 119 | | await context.BaseItems |
| | 0 | 120 | | .Where(b => b.OwnerId == dupId) |
| | 0 | 121 | | .ExecuteUpdateAsync(s => s.SetProperty(b => b.OwnerId, keeperId), cancellationToken) |
| | 0 | 122 | | .ConfigureAwait(false); |
| | | 123 | | |
| | | 124 | | // AncestorIds PK is (ItemId, ParentItemId); drop rows that would collide before redirecting. |
| | 0 | 125 | | await context.AncestorIds |
| | 0 | 126 | | .Where(a => a.ParentItemId == dupId |
| | 0 | 127 | | && context.AncestorIds.Any(k => k.ParentItemId == keeperId && k.ItemId == a.ItemId)) |
| | 0 | 128 | | .ExecuteDeleteAsync(cancellationToken) |
| | 0 | 129 | | .ConfigureAwait(false); |
| | 0 | 130 | | await context.AncestorIds |
| | 0 | 131 | | .Where(a => a.ParentItemId == dupId) |
| | 0 | 132 | | .ExecuteUpdateAsync(s => s.SetProperty(a => a.ParentItemId, keeperId), cancellationToken) |
| | 0 | 133 | | .ConfigureAwait(false); |
| | | 134 | | |
| | | 135 | | // LinkedChildren PK is (ParentId, ChildId); drop colliding rows in both directions. |
| | 0 | 136 | | await context.LinkedChildren |
| | 0 | 137 | | .Where(l => l.ParentId == dupId |
| | 0 | 138 | | && context.LinkedChildren.Any(k => k.ParentId == keeperId && k.ChildId == l.ChildId)) |
| | 0 | 139 | | .ExecuteDeleteAsync(cancellationToken) |
| | 0 | 140 | | .ConfigureAwait(false); |
| | 0 | 141 | | await context.LinkedChildren |
| | 0 | 142 | | .Where(l => l.ParentId == dupId) |
| | 0 | 143 | | .ExecuteUpdateAsync(s => s.SetProperty(l => l.ParentId, keeperId), cancellationToken) |
| | 0 | 144 | | .ConfigureAwait(false); |
| | 0 | 145 | | await context.LinkedChildren |
| | 0 | 146 | | .Where(l => l.ChildId == dupId |
| | 0 | 147 | | && context.LinkedChildren.Any(k => k.ChildId == keeperId && k.ParentId == l.ParentId)) |
| | 0 | 148 | | .ExecuteDeleteAsync(cancellationToken) |
| | 0 | 149 | | .ConfigureAwait(false); |
| | 0 | 150 | | await context.LinkedChildren |
| | 0 | 151 | | .Where(l => l.ChildId == dupId) |
| | 0 | 152 | | .ExecuteUpdateAsync(s => s.SetProperty(l => l.ChildId, keeperId), cancellationToken) |
| | 0 | 153 | | .ConfigureAwait(false); |
| | | 154 | | |
| | | 155 | | // UserData has UNIQUE(UserId, CustomDataKey); keep the dup's row only when the |
| | | 156 | | // keeper has no equivalent row, otherwise the keeper's value wins. |
| | 0 | 157 | | await context.UserData |
| | 0 | 158 | | .Where(u => u.ItemId == dupId |
| | 0 | 159 | | && context.UserData.Any(k => k.ItemId == keeperId && k.UserId == u.UserId && k.CustomDataKey |
| | 0 | 160 | | .ExecuteDeleteAsync(cancellationToken) |
| | 0 | 161 | | .ConfigureAwait(false); |
| | 0 | 162 | | await context.UserData |
| | 0 | 163 | | .Where(u => u.ItemId == dupId) |
| | 0 | 164 | | .ExecuteUpdateAsync(s => s.SetProperty(u => u.ItemId, keeperId), cancellationToken) |
| | 0 | 165 | | .ConfigureAwait(false); |
| | | 166 | | |
| | 0 | 167 | | idsToDelete.Add(dupId); |
| | 0 | 168 | | } |
| | | 169 | | |
| | 0 | 170 | | _logger.LogDebug( |
| | 0 | 171 | | "Merged duplicates for '{Name}' into {KeeperId} ({Removed} removed).", |
| | 0 | 172 | | keeper.Name, |
| | 0 | 173 | | keeper.Id, |
| | 0 | 174 | | stats.Count - 1); |
| | 0 | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | if (idsToDelete.Count == 0) |
| | | 178 | | { |
| | | 179 | | return; |
| | | 180 | | } |
| | | 181 | | |
| | | 182 | | // Resolve via LibraryManager so DeleteItemsUnsafeFast can also remove the |
| | | 183 | | // %MetadataPath%/artists/<Name> directories that the duplicate stubs left behind. |
| | | 184 | | // Fall back to the persistence service for any items the LibraryManager can't resolve. |
| | 0 | 185 | | var itemsToDelete = idsToDelete |
| | 0 | 186 | | .Select(id => _libraryManager.GetItemById(id)) |
| | 0 | 187 | | .Where(item => item is not null) |
| | 0 | 188 | | .ToList(); |
| | 0 | 189 | | if (itemsToDelete.Count > 0) |
| | | 190 | | { |
| | 0 | 191 | | _libraryManager.DeleteItemsUnsafeFast(itemsToDelete!); |
| | | 192 | | } |
| | | 193 | | |
| | 0 | 194 | | var deletedIds = itemsToDelete.Select(i => i!.Id).ToHashSet(); |
| | 0 | 195 | | var unresolvedIds = idsToDelete.Where(id => !deletedIds.Contains(id)).ToList(); |
| | 0 | 196 | | if (unresolvedIds.Count > 0) |
| | | 197 | | { |
| | 0 | 198 | | _persistenceService.DeleteItem(unresolvedIds); |
| | | 199 | | } |
| | | 200 | | |
| | 0 | 201 | | _logger.LogInformation("Removed {Count} duplicate MusicArtist records.", idsToDelete.Count); |
| | 0 | 202 | | } |
| | 0 | 203 | | } |
| | | 204 | | } |