< Summary - Jellyfin

Information
Class: Jellyfin.Server.Migrations.Routines.RefreshCleanNamesAndValues
Assembly: jellyfin
File(s): /srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/20260610120000_RefreshCleanNamesAndValues.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 86
Coverable lines: 86
Total lines: 173
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 24
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 6/14/2026 - 12:16:28 AM Line coverage: 0% (0/86) Branch coverage: 0% (0/24) Total lines: 173 6/14/2026 - 12:16:28 AM Line coverage: 0% (0/86) Branch coverage: 0% (0/24) Total lines: 173

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
PerformAsync()100%210%
RefreshCleanNamesAsync()0%156120%
RefreshCleanValuesAsync()0%156120%

File(s)

/srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/20260610120000_RefreshCleanNamesAndValues.cs

#LineLine coverage
 1using System;
 2using System.Diagnostics;
 3using System.Linq;
 4using System.Threading;
 5using System.Threading.Tasks;
 6using Jellyfin.Database.Implementations;
 7using Jellyfin.Extensions;
 8using Jellyfin.Server.ServerSetupApp;
 9using Microsoft.EntityFrameworkCore;
 10using Microsoft.Extensions.Logging;
 11
 12namespace 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)]
 19public 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    {
 033        _logger = logger;
 034        _dbProvider = dbProvider;
 035    }
 36
 37    /// <inheritdoc />
 38    public async Task PerformAsync(CancellationToken cancellationToken)
 39    {
 040        await RefreshCleanNamesAsync(cancellationToken).ConfigureAwait(false);
 041        await RefreshCleanValuesAsync(cancellationToken).ConfigureAwait(false);
 042    }
 43
 44    private async Task RefreshCleanNamesAsync(CancellationToken cancellationToken)
 45    {
 46        const int Limit = 10000;
 047        int itemCount = 0;
 48
 049        var sw = Stopwatch.StartNew();
 50
 051        using var context = _dbProvider.CreateDbContext();
 052        var records = context.BaseItems.Count(b => !string.IsNullOrEmpty(b.Name));
 053        _logger.LogInformation("Refreshing CleanName for {Count} library items", records);
 54
 055        var processedInPartition = 0;
 56
 057        await foreach (var item in context.BaseItems
 058                          .Where(b => !string.IsNullOrEmpty(b.Name))
 059                          .OrderBy(e => e.Id)
 060                          .WithPartitionProgress((partition) => _logger.LogInformation("Processed: {Offset}/{Total} - Up
 061                          .PartitionEagerAsync(Limit, cancellationToken)
 062                          .WithCancellation(cancellationToken)
 063                          .ConfigureAwait(false))
 64        {
 65            try
 66            {
 067                var newCleanName = string.IsNullOrWhiteSpace(item.Name) ? string.Empty : item.Name.GetCleanValue();
 068                if (!string.Equals(newCleanName, item.CleanName, StringComparison.Ordinal))
 69                {
 070                    _logger.LogDebug(
 071                        "Updating CleanName for item {Id}: '{OldValue}' -> '{NewValue}'",
 072                        item.Id,
 073                        item.CleanName,
 074                        newCleanName);
 075                    item.CleanName = newCleanName;
 076                    itemCount++;
 77                }
 078            }
 079            catch (Exception ex)
 80            {
 081                _logger.LogWarning(ex, "Failed to update CleanName for item {Id} ({Name})", item.Id, item.Name);
 082            }
 83
 084            processedInPartition++;
 85
 086            if (processedInPartition >= Limit)
 87            {
 088                await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
 89                // Clear tracked entities to avoid memory growth across partitions
 090                context.ChangeTracker.Clear();
 091                processedInPartition = 0;
 92            }
 93        }
 94
 95        // Save any remaining changes after the loop
 096        if (processedInPartition > 0)
 97        {
 098            await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
 099            context.ChangeTracker.Clear();
 100        }
 101
 0102        _logger.LogInformation(
 0103            "Refreshed CleanName for {UpdatedCount} out of {TotalCount} items in {Time}",
 0104            itemCount,
 0105            records,
 0106            sw.Elapsed);
 0107    }
 108
 109    private async Task RefreshCleanValuesAsync(CancellationToken cancellationToken)
 110    {
 111        const int Limit = 10000;
 0112        int itemCount = 0;
 113
 0114        var sw = Stopwatch.StartNew();
 115
 0116        using var context = _dbProvider.CreateDbContext();
 0117        var records = context.ItemValues.Count(b => !string.IsNullOrEmpty(b.Value));
 0118        _logger.LogInformation("Refreshing CleanValue for {Count} item values", records);
 119
 0120        var processedInPartition = 0;
 121
 0122        await foreach (var item in context.ItemValues
 0123                          .Where(b => !string.IsNullOrEmpty(b.Value))
 0124                          .OrderBy(e => e.ItemValueId)
 0125                          .WithPartitionProgress((partition) => _logger.LogInformation("Processed: {Offset}/{Total} - Up
 0126                          .PartitionEagerAsync(Limit, cancellationToken)
 0127                          .WithCancellation(cancellationToken)
 0128                          .ConfigureAwait(false))
 129        {
 130            try
 131            {
 0132                var newCleanValue = string.IsNullOrWhiteSpace(item.Value) ? string.Empty : item.Value.GetCleanValue();
 0133                if (!string.Equals(newCleanValue, item.CleanValue, StringComparison.Ordinal))
 134                {
 0135                    _logger.LogDebug(
 0136                        "Updating CleanValue for item value {Id}: '{OldValue}' -> '{NewValue}'",
 0137                        item.ItemValueId,
 0138                        item.CleanValue,
 0139                        newCleanValue);
 0140                    item.CleanValue = newCleanValue;
 0141                    itemCount++;
 142                }
 0143            }
 0144            catch (Exception ex)
 145            {
 0146                _logger.LogWarning(ex, "Failed to update CleanValue for item value {Id} ({Value})", item.ItemValueId, it
 0147            }
 148
 0149            processedInPartition++;
 150
 0151            if (processedInPartition >= Limit)
 152            {
 0153                await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
 154                // Clear tracked entities to avoid memory growth across partitions
 0155                context.ChangeTracker.Clear();
 0156                processedInPartition = 0;
 157            }
 158        }
 159
 160        // Save any remaining changes after the loop
 0161        if (processedInPartition > 0)
 162        {
 0163            await context.SaveChangesAsync(cancellationToken).ConfigureAwait(false);
 0164            context.ChangeTracker.Clear();
 165        }
 166
 0167        _logger.LogInformation(
 0168            "Refreshed CleanValue for {UpdatedCount} out of {TotalCount} item values in {Time}",
 0169            itemCount,
 0170            records,
 0171            sw.Elapsed);
 0172    }
 173}