| | | 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 Microsoft.EntityFrameworkCore; |
| | | 10 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace Jellyfin.Server.Migrations.Routines; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Migration to refresh CleanName values for all library items and CleanValue values for all item values. |
| | | 16 | | /// </summary> |
| | | 17 | | [JellyfinMigration("2026-06-10T12:00:00", nameof(RefreshCleanNamesAndValues))] |
| | | 18 | | [JellyfinMigrationBackup(JellyfinDb = true)] |
| | | 19 | | public class RefreshCleanNamesAndValues : IAsyncMigrationRoutine |
| | | 20 | | { |
| | | 21 | | private readonly IStartupLogger<RefreshCleanNamesAndValues> _logger; |
| | | 22 | | private readonly IDbContextFactory<JellyfinDbContext> _dbProvider; |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Initializes a new instance of the <see cref="RefreshCleanNamesAndValues"/> class. |
| | | 26 | | /// </summary> |
| | | 27 | | /// <param name="logger">The logger.</param> |
| | | 28 | | /// <param name="dbProvider">Instance of the <see cref="IDbContextFactory{JellyfinDbContext}"/> interface.</param> |
| | | 29 | | public RefreshCleanNamesAndValues( |
| | | 30 | | IStartupLogger<RefreshCleanNamesAndValues> logger, |
| | | 31 | | IDbContextFactory<JellyfinDbContext> dbProvider) |
| | | 32 | | { |
| | 0 | 33 | | _logger = logger; |
| | 0 | 34 | | _dbProvider = dbProvider; |
| | 0 | 35 | | } |
| | | 36 | | |
| | | 37 | | /// <inheritdoc /> |
| | | 38 | | public async Task PerformAsync(CancellationToken cancellationToken) |
| | | 39 | | { |
| | 0 | 40 | | await RefreshCleanNamesAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 41 | | await RefreshCleanValuesAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | private async Task RefreshCleanNamesAsync(CancellationToken cancellationToken) |
| | | 45 | | { |
| | | 46 | | const int Limit = 10000; |
| | 0 | 47 | | int itemCount = 0; |
| | | 48 | | |
| | 0 | 49 | | var sw = Stopwatch.StartNew(); |
| | | 50 | | |
| | 0 | 51 | | using var context = _dbProvider.CreateDbContext(); |
| | 0 | 52 | | var records = context.BaseItems.Count(b => !string.IsNullOrEmpty(b.Name)); |
| | 0 | 53 | | _logger.LogInformation("Refreshing CleanName for {Count} library items", records); |
| | | 54 | | |
| | 0 | 55 | | var processedInPartition = 0; |
| | | 56 | | |
| | 0 | 57 | | await foreach (var item in context.BaseItems |
| | 0 | 58 | | .Where(b => !string.IsNullOrEmpty(b.Name)) |
| | 0 | 59 | | .OrderBy(e => e.Id) |
| | 0 | 60 | | .WithPartitionProgress((partition) => _logger.LogInformation("Processed: {Offset}/{Total} - Up |
| | 0 | 61 | | .PartitionEagerAsync(Limit, cancellationToken) |
| | 0 | 62 | | .WithCancellation(cancellationToken) |
| | 0 | 63 | | .ConfigureAwait(false)) |
| | | 64 | | { |
| | | 65 | | try |
| | | 66 | | { |
| | 0 | 67 | | var newCleanName = string.IsNullOrWhiteSpace(item.Name) ? string.Empty : item.Name.GetCleanValue(); |
| | 0 | 68 | | if (!string.Equals(newCleanName, item.CleanName, StringComparison.Ordinal)) |
| | | 69 | | { |
| | 0 | 70 | | _logger.LogDebug( |
| | 0 | 71 | | "Updating CleanName for item {Id}: '{OldValue}' -> '{NewValue}'", |
| | 0 | 72 | | item.Id, |
| | 0 | 73 | | item.CleanName, |
| | 0 | 74 | | newCleanName); |
| | 0 | 75 | | item.CleanName = newCleanName; |
| | 0 | 76 | | itemCount++; |
| | | 77 | | } |
| | 0 | 78 | | } |
| | 0 | 79 | | catch (Exception ex) |
| | | 80 | | { |
| | 0 | 81 | | _logger.LogWarning(ex, "Failed to update CleanName for item {Id} ({Name})", item.Id, item.Name); |
| | 0 | 82 | | } |
| | | 83 | | |
| | 0 | 84 | | processedInPartition++; |
| | | 85 | | |
| | 0 | 86 | | if (processedInPartition >= Limit) |
| | | 87 | | { |
| | 0 | 88 | | await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| | | 89 | | // Clear tracked entities to avoid memory growth across partitions |
| | 0 | 90 | | context.ChangeTracker.Clear(); |
| | 0 | 91 | | processedInPartition = 0; |
| | | 92 | | } |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | // Save any remaining changes after the loop |
| | 0 | 96 | | if (processedInPartition > 0) |
| | | 97 | | { |
| | 0 | 98 | | await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 99 | | context.ChangeTracker.Clear(); |
| | | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | _logger.LogInformation( |
| | 0 | 103 | | "Refreshed CleanName for {UpdatedCount} out of {TotalCount} items in {Time}", |
| | 0 | 104 | | itemCount, |
| | 0 | 105 | | records, |
| | 0 | 106 | | sw.Elapsed); |
| | 0 | 107 | | } |
| | | 108 | | |
| | | 109 | | private async Task RefreshCleanValuesAsync(CancellationToken cancellationToken) |
| | | 110 | | { |
| | | 111 | | const int Limit = 10000; |
| | 0 | 112 | | int itemCount = 0; |
| | | 113 | | |
| | 0 | 114 | | var sw = Stopwatch.StartNew(); |
| | | 115 | | |
| | 0 | 116 | | using var context = _dbProvider.CreateDbContext(); |
| | 0 | 117 | | var records = context.ItemValues.Count(b => !string.IsNullOrEmpty(b.Value)); |
| | 0 | 118 | | _logger.LogInformation("Refreshing CleanValue for {Count} item values", records); |
| | | 119 | | |
| | 0 | 120 | | var processedInPartition = 0; |
| | | 121 | | |
| | 0 | 122 | | await foreach (var item in context.ItemValues |
| | 0 | 123 | | .Where(b => !string.IsNullOrEmpty(b.Value)) |
| | 0 | 124 | | .OrderBy(e => e.ItemValueId) |
| | 0 | 125 | | .WithPartitionProgress((partition) => _logger.LogInformation("Processed: {Offset}/{Total} - Up |
| | 0 | 126 | | .PartitionEagerAsync(Limit, cancellationToken) |
| | 0 | 127 | | .WithCancellation(cancellationToken) |
| | 0 | 128 | | .ConfigureAwait(false)) |
| | | 129 | | { |
| | | 130 | | try |
| | | 131 | | { |
| | 0 | 132 | | var newCleanValue = string.IsNullOrWhiteSpace(item.Value) ? string.Empty : item.Value.GetCleanValue(); |
| | 0 | 133 | | if (!string.Equals(newCleanValue, item.CleanValue, StringComparison.Ordinal)) |
| | | 134 | | { |
| | 0 | 135 | | _logger.LogDebug( |
| | 0 | 136 | | "Updating CleanValue for item value {Id}: '{OldValue}' -> '{NewValue}'", |
| | 0 | 137 | | item.ItemValueId, |
| | 0 | 138 | | item.CleanValue, |
| | 0 | 139 | | newCleanValue); |
| | 0 | 140 | | item.CleanValue = newCleanValue; |
| | 0 | 141 | | itemCount++; |
| | | 142 | | } |
| | 0 | 143 | | } |
| | 0 | 144 | | catch (Exception ex) |
| | | 145 | | { |
| | 0 | 146 | | _logger.LogWarning(ex, "Failed to update CleanValue for item value {Id} ({Value})", item.ItemValueId, it |
| | 0 | 147 | | } |
| | | 148 | | |
| | 0 | 149 | | processedInPartition++; |
| | | 150 | | |
| | 0 | 151 | | if (processedInPartition >= Limit) |
| | | 152 | | { |
| | 0 | 153 | | await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| | | 154 | | // Clear tracked entities to avoid memory growth across partitions |
| | 0 | 155 | | context.ChangeTracker.Clear(); |
| | 0 | 156 | | processedInPartition = 0; |
| | | 157 | | } |
| | | 158 | | } |
| | | 159 | | |
| | | 160 | | // Save any remaining changes after the loop |
| | 0 | 161 | | if (processedInPartition > 0) |
| | | 162 | | { |
| | 0 | 163 | | await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false); |
| | 0 | 164 | | context.ChangeTracker.Clear(); |
| | | 165 | | } |
| | | 166 | | |
| | 0 | 167 | | _logger.LogInformation( |
| | 0 | 168 | | "Refreshed CleanValue for {UpdatedCount} out of {TotalCount} item values in {Time}", |
| | 0 | 169 | | itemCount, |
| | 0 | 170 | | records, |
| | 0 | 171 | | sw.Elapsed); |
| | 0 | 172 | | } |
| | | 173 | | } |