< Summary - Jellyfin

Information
Class: Jellyfin.Server.Migrations.Stages.CodeMigration
Assembly: jellyfin
File(s): /srv/git/jellyfin/Jellyfin.Server/Migrations/Stages/CodeMigration.cs
Line coverage
85%
Covered lines: 12
Uncovered lines: 2
Coverable lines: 14
Total lines: 87
Line coverage: 85.7%
Branch coverage
100%
Covered branches: 6
Total branches: 6
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
BuildCodeMigrationId()100%11100%
MigrationServices(...)100%66100%
.ctor(...)100%210%

File(s)

/srv/git/jellyfin/Jellyfin.Server/Migrations/Stages/CodeMigration.cs

#LineLine coverage
 1using System;
 2using System.Globalization;
 3using System.Threading;
 4using System.Threading.Tasks;
 5using Jellyfin.Server.ServerSetupApp;
 6using Microsoft.Extensions.DependencyInjection;
 7using Microsoft.Extensions.DependencyInjection.Extensions;
 8using Microsoft.Extensions.Logging;
 9
 10namespace Jellyfin.Server.Migrations.Stages;
 11
 12internal class CodeMigration(Type migrationType, JellyfinMigrationAttribute metadata, JellyfinMigrationBackupAttribute? 
 13{
 14    public Type MigrationType { get; } = migrationType;
 15
 16    public JellyfinMigrationAttribute Metadata { get; } = metadata;
 17
 18    public JellyfinMigrationBackupAttribute? BackupRequirements { get; set; } = migrationBackupAttribute;
 19
 20    public string BuildCodeMigrationId()
 21    {
 2688022        return Metadata.Order.ToString("yyyyMMddHHmmsss", CultureInfo.InvariantCulture) + "_" + Metadata.Name!;
 23    }
 24
 25    private IServiceCollection MigrationServices(IServiceProvider serviceProvider, IStartupLogger logger)
 26    {
 10527        var childServiceCollection = new ServiceCollection()
 10528            .AddSingleton(serviceProvider)
 10529            .AddSingleton(logger)
 10530            .AddSingleton(typeof(IStartupLogger<>), typeof(NestedStartupLogger<>))
 10531            .AddSingleton<StartupLogTopic>(logger.Topic!);
 32
 11907033        foreach (ServiceDescriptor service in serviceProvider.GetRequiredService<IServiceCollection>())
 34        {
 5943035            if (service.Lifetime == ServiceLifetime.Singleton && !service.ServiceType.IsGenericTypeDefinition)
 36            {
 3717037                childServiceCollection.AddSingleton(service.ServiceType, _ => serviceProvider.GetService(service.Service
 3717038                continue;
 39            }
 40
 2226041            childServiceCollection.Add(service);
 42        }
 43
 10544        return childServiceCollection;
 45    }
 46
 47    public async Task Perform(IServiceProvider? serviceProvider, IStartupLogger logger, CancellationToken cancellationTo
 48    {
 49#pragma warning disable CS0618 // Type or member is obsolete
 50        if (typeof(IMigrationRoutine).IsAssignableFrom(MigrationType))
 51        {
 52            if (serviceProvider is null)
 53            {
 54                ((IMigrationRoutine)Activator.CreateInstance(MigrationType)!).Perform();
 55            }
 56            else
 57            {
 58                using var migrationServices = MigrationServices(serviceProvider, logger).BuildServiceProvider();
 59                ((IMigrationRoutine)ActivatorUtilities.CreateInstance(migrationServices, MigrationType)).Perform();
 60#pragma warning restore CS0618 // Type or member is obsolete
 61            }
 62        }
 63        else if (typeof(IAsyncMigrationRoutine).IsAssignableFrom(MigrationType))
 64        {
 65            if (serviceProvider is null)
 66            {
 67                await ((IAsyncMigrationRoutine)Activator.CreateInstance(MigrationType)!).PerformAsync(cancellationToken)
 68            }
 69            else
 70            {
 71                using var migrationServices = MigrationServices(serviceProvider, logger).BuildServiceProvider();
 72                await ((IAsyncMigrationRoutine)ActivatorUtilities.CreateInstance(migrationServices, MigrationType)).Perf
 73            }
 74        }
 75        else
 76        {
 77            throw new InvalidOperationException($"The type {MigrationType} does not implement either IMigrationRoutine o
 78        }
 79    }
 80
 81    private class NestedStartupLogger<TCategory> : StartupLogger<TCategory>
 82    {
 083        public NestedStartupLogger(ILogger logger, StartupLogTopic topic) : base(logger, topic)
 84        {
 085        }
 86    }
 87}