< Summary - Jellyfin

Information
Class: Jellyfin.Server.Migrations.Routines.RefreshInternalDateModified
Assembly: jellyfin
File(s): /srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/20250420230000_RefreshInternalDateModified.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 54
Coverable lines: 54
Total lines: 140
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 5/31/2026 - 12:15:44 AM Line coverage: 0% (0/52) Branch coverage: 0% (0/16) Total lines: 13111/13/2026 - 12:19:51 AM Line coverage: 0% (0/54) Branch coverage: 0% (0/16) Total lines: 140 5/31/2026 - 12:15:44 AM Line coverage: 0% (0/52) Branch coverage: 0% (0/16) Total lines: 13111/13/2026 - 12:19:51 AM Line coverage: 0% (0/54) Branch coverage: 0% (0/16) Total lines: 140

Coverage delta

Coverage delta 1 -1

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
PerformAsync(...)100%210%
PerformCore()0%272160%

File(s)

/srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/20250420230000_RefreshInternalDateModified.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Diagnostics;
 4using System.Linq;
 5using System.Threading;
 6using System.Threading.Tasks;
 7using Jellyfin.Database.Implementations;
 8using Jellyfin.Database.Implementations.Entities;
 9using Jellyfin.Extensions;
 10using MediaBrowser.Controller;
 11using MediaBrowser.Controller.Configuration;
 12using MediaBrowser.Controller.Entities;
 13using MediaBrowser.Controller.Entities.Audio;
 14using MediaBrowser.Controller.Library;
 15using MediaBrowser.Model.IO;
 16using Microsoft.EntityFrameworkCore;
 17using Microsoft.Extensions.Logging;
 18
 19namespace Jellyfin.Server.Migrations.Routines;
 20
 21/// <summary>
 22/// Migration to re-read creation dates for library items with internal metadata paths.
 23/// </summary>
 24[JellyfinMigration("2025-04-20T23:00:00", nameof(RefreshInternalDateModified))]
 25public class RefreshInternalDateModified : IDatabaseMigrationRoutine
 26{
 27    private readonly ILogger<RefreshInternalDateModified> _logger;
 28    private readonly IDbContextFactory<JellyfinDbContext> _dbProvider;
 29    private readonly IFileSystem _fileSystem;
 30    private readonly IServerApplicationHost _applicationHost;
 31    private readonly bool _useFileCreationTimeForDateAdded;
 32
 033    private IReadOnlyList<string> _internalTypes = [
 034         typeof(Genre).FullName!,
 035         typeof(MusicGenre).FullName!,
 036         typeof(MusicArtist).FullName!,
 037         typeof(People).FullName!,
 038         typeof(Studio).FullName!
 039    ];
 40
 41    private IReadOnlyList<string> _internalPaths;
 42
 43    /// <summary>
 44    /// Initializes a new instance of the <see cref="RefreshInternalDateModified"/> class.
 45    /// </summary>
 46    /// <param name="applicationHost">Instance of the <see cref="IServerApplicationHost"/> interface.</param>
 47    /// <param name="applicationPaths">Instance of the <see cref="IServerApplicationPaths"/> interface.</param>
 48    /// <param name="configurationManager">Instance of the <see cref="IServerConfigurationManager"/> interface.</param>
 49    /// <param name="dbProvider">Instance of the <see cref="IDbContextFactory{JellyfinDbContext}"/> interface.</param>
 50    /// <param name="logger">The logger.</param>
 51    /// <param name="fileSystem">Instance of the <see cref="IFileSystem"/> interface.</param>
 52    public RefreshInternalDateModified(
 53        IServerApplicationHost applicationHost,
 54        IServerApplicationPaths applicationPaths,
 55        IServerConfigurationManager configurationManager,
 56        IDbContextFactory<JellyfinDbContext> dbProvider,
 57        ILogger<RefreshInternalDateModified> logger,
 58        IFileSystem fileSystem)
 59    {
 060        _dbProvider = dbProvider;
 061        _logger = logger;
 062        _fileSystem = fileSystem;
 063        _applicationHost = applicationHost;
 064        _internalPaths = [
 065            applicationPaths.ArtistsPath,
 066            applicationPaths.GenrePath,
 067            applicationPaths.MusicGenrePath,
 068            applicationPaths.StudioPath,
 069            applicationPaths.PeoplePath
 070        ];
 071        _useFileCreationTimeForDateAdded = configurationManager.GetMetadataConfiguration().UseFileCreationTimeForDateAdd
 072    }
 73
 74    /// <inheritdoc />
 75    public Task PerformAsync(CancellationToken cancellationToken)
 76    {
 77        // This routine predates the async interface and has not been ported to async database access yet.
 078        PerformCore();
 079        return Task.CompletedTask;
 80    }
 81
 82    private void PerformCore()
 83    {
 84        const int Limit = 5000;
 085        int itemCount = 0, offset = 0;
 86
 087        var sw = Stopwatch.StartNew();
 88
 089        using var context = _dbProvider.CreateDbContext();
 090        var records = context.BaseItems.Count(b => _internalTypes.Contains(b.Type));
 091        _logger.LogInformation("Checking if {Count} potentially internal items require refreshed DateModified", records)
 92
 93        do
 94        {
 095            var results = context.BaseItems
 096                            .Where(b => _internalTypes.Contains(b.Type))
 097                            .OrderBy(e => e.Id)
 098                            .Skip(offset)
 099                            .Take(Limit)
 0100                            .ToList();
 101
 0102            foreach (var item in results)
 103            {
 0104                var itemPath = item.Path;
 0105                if (itemPath is not null)
 106                {
 0107                    var realPath = _applicationHost.ExpandVirtualPath(item.Path);
 0108                    if (_internalPaths.Any(path => realPath.StartsWith(path, StringComparison.Ordinal)))
 109                    {
 0110                        var writeTime = _fileSystem.GetLastWriteTimeUtc(realPath);
 0111                        var itemModificationTime = item.DateModified;
 0112                        if (writeTime != itemModificationTime)
 113                        {
 0114                            _logger.LogDebug("Reset file modification date: Old: {Old} - New: {New} - Path: {Path}", ite
 0115                            item.DateModified = writeTime;
 0116                            if (_useFileCreationTimeForDateAdded)
 117                            {
 0118                                item.DateCreated = _fileSystem.GetCreationTimeUtc(realPath);
 119                            }
 120
 0121                            itemCount++;
 122                        }
 123                    }
 124                }
 125            }
 126
 0127            offset += Limit;
 0128            if (offset > records)
 129            {
 0130                offset = records;
 131            }
 132
 0133            _logger.LogInformation("Checked: {Count} - Refreshed: {Items} - Time: {Time}", offset, itemCount, sw.Elapsed
 0134        } while (offset < records);
 135
 0136        context.SaveChanges();
 137
 0138        _logger.LogInformation("Refreshed DateModified for {Count} items in {Time}", itemCount, sw.Elapsed);
 0139    }
 140}