| | | 1 | | using System; |
| | | 2 | | using System.Diagnostics; |
| | | 3 | | using System.Linq; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | using Jellyfin.Database.Implementations; |
| | | 7 | | using Jellyfin.Extensions; |
| | | 8 | | using Jellyfin.Server.ServerSetupApp; |
| | | 9 | | using MediaBrowser.Controller.Configuration; |
| | | 10 | | using MediaBrowser.Controller.Entities; |
| | | 11 | | using Microsoft.EntityFrameworkCore; |
| | | 12 | | using Microsoft.Extensions.Logging; |
| | | 13 | | |
| | | 14 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Migration to recompute the SortName of all items that have a forced sort name. |
| | | 18 | | /// </summary> |
| | | 19 | | [JellyfinMigration("2026-07-22T12:00:00", nameof(RefreshForcedSortNames))] |
| | | 20 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 21 | | public class RefreshForcedSortNames : IAsyncMigrationRoutine |
| | | 22 | | { |
| | | 23 | | private readonly IStartupLogger<RefreshForcedSortNames> _logger; |
| | | 24 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 25 | | private readonly IServerConfigurationManager _configurationManager; |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Initializes a new instance of the <see cref="RefreshForcedSortNames"/> class. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="logger">The logger.</param> |
| | | 31 | | /// <param name="dbProvider">Instance of the <see cref="IDbContextFactory{JellyfinDbContext}"/> interface.</param> |
| | | 32 | | /// <param name="configurationManager">The server configuration manager providing the sort rules.</param> |
| | | 33 | | public RefreshForcedSortNames( |
| | | 34 | | IStartupLogger<RefreshForcedSortNames> logger, |
| | | 35 | | IDbContextFactory<JellyfinDbContext> dbProvider, |
| | | 36 | | IServerConfigurationManager configurationManager) |
| | | 37 | | { |
| | 0 | 38 | | _logger = logger; |
| | 0 | 39 | | _dbProvider = dbProvider; |
| | 0 | 40 | | _configurationManager = configurationManager; |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | | 45 | | { |
| | | 46 | | const int Limit = 10000; |
| | 0 | 47 | | int itemCount = 0; |
| | | 48 | | |
| | 0 | 49 | | var configuration = _configurationManager.Configuration; |
| | | 50 | | // Only the Person type disables alphanumeric sorting; everything else uses the cleaning rules. |
| | 0 | 51 | | var personType = typeof(Person).ToString(); |
| | | 52 | | |
| | 0 | 53 | | var sw = Stopwatch.StartNew(); |
| | | 54 | | |
| | 0 | 55 | | using var context = _dbProvider.CreateDbContext(); |
| | 0 | 56 | | var records = context.BaseItems.Count(b => !string.IsNullOrEmpty(b.ForcedSortName)); |
| | 0 | 57 | | _logger.LogInformation("Refreshing SortName for {Count} library items with a forced sort name", records); |
| | | 58 | | |
| | 0 | 59 | | var processedInPartition = 0; |
| | | 60 | | |
| | 0 | 61 | | await foreach (var item in context.BaseItems |
| | 0 | 62 | | .Where(b => !string.IsNullOrEmpty(b.ForcedSortName)) |
| | 0 | 63 | | .OrderBy(e => e.Id) |
| | 0 | 64 | | .WithPartitionProgress((partition) => _logger.LogInformation("Processed: {Offset}/{Total} - Up |
| | 0 | 65 | | .PartitionEagerAsync(Limit, cancellationToken) |
| | 0 | 66 | | .WithCancellation(cancellationToken) |
| | 0 | 67 | | .ConfigureAwait(false)) |
| | | 68 | | { |
| | | 69 | | try |
| | | 70 | | { |
| | 0 | 71 | | var enableAlphaNumericSorting = !string.Equals(item.Type, personType, StringComparison.Ordinal); |
| | 0 | 72 | | var newSortName = BaseItem.GetSortName(item.ForcedSortName!, enableAlphaNumericSorting, configuration); |
| | 0 | 73 | | if (!string.Equals(newSortName, item.SortName, StringComparison.Ordinal)) |
| | | 74 | | { |
| | 0 | 75 | | _logger.LogDebug( |
| | 0 | 76 | | "Updating SortName for item {Id}: '{OldValue}' -> '{NewValue}'", |
| | 0 | 77 | | item.Id, |
| | 0 | 78 | | item.SortName, |
| | 0 | 79 | | newSortName); |
| | 0 | 80 | | item.SortName = newSortName; |
| | 0 | 81 | | itemCount++; |
| | | 82 | | } |
| | 0 | 83 | | } |
| | 0 | 84 | | catch (Exception ex) |
| | | 85 | | { |
| | 0 | 86 | | _logger.LogWarning(ex, "Failed to update SortName for item {Id} ({Name})", item.Id, item.Name); |
| | 0 | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | processedInPartition++; |
| | | 90 | | |
| | 0 | 91 | | if (processedInPartition >= Limit) |
| | | 92 | | { |
| | 0 | 93 | | await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| | | 94 | | // Clear tracked entities to avoid memory growth across partitions |
| | 0 | 95 | | context.ChangeTracker.Clear(); |
| | 0 | 96 | | processedInPartition = 0; |
| | | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | // Save any remaining changes after the loop |
| | 0 | 101 | | if (processedInPartition > 0) |
| | | 102 | | { |
| | 0 | 103 | | await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 104 | | context.ChangeTracker.Clear(); |
| | | 105 | | } |
| | | 106 | | |
| | 0 | 107 | | _logger.LogInformation( |
| | 0 | 108 | | "Refreshed SortName for {UpdatedCount} out of {TotalCount} items in {Time}", |
| | 0 | 109 | | itemCount, |
| | 0 | 110 | | records, |
| | 0 | 111 | | sw.Elapsed); |
| | 0 | 112 | | } |
| | | 113 | | } |