< Summary - Jellyfin

Information
Class: Jellyfin.Server.Migrations.Routines.MigrateRatingLevels
Assembly: jellyfin
File(s): /srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/MigrateRatingLevels.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 28
Coverable lines: 28
Total lines: 67
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 8
Branch coverage: 0%
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
.ctor(...)100%210%
Perform()0%7280%

File(s)

/srv/git/jellyfin/Jellyfin.Server/Migrations/Routines/MigrateRatingLevels.cs

#LineLine coverage
 1using System;
 2using System.Linq;
 3using Jellyfin.Database.Implementations;
 4using MediaBrowser.Model.Globalization;
 5using Microsoft.EntityFrameworkCore;
 6using Microsoft.Extensions.Logging;
 7
 8namespace Jellyfin.Server.Migrations.Routines;
 9
 10/// <summary>
 11/// Migrate rating levels.
 12/// </summary>
 13#pragma warning disable CS0618 // Type or member is obsolete
 14[JellyfinMigration("2025-04-20T22:00:00", nameof(MigrateRatingLevels))]
 15#pragma warning restore CS0618 // Type or member is obsolete
 16internal class MigrateRatingLevels : IDatabaseMigrationRoutine
 17{
 18    private readonly ILogger<MigrateRatingLevels> _logger;
 19    private readonly IDbContextFactory<JellyfinDbContext> _provider;
 20    private readonly ILocalizationManager _localizationManager;
 21
 22    public MigrateRatingLevels(
 23        IDbContextFactory<JellyfinDbContext> provider,
 24        ILoggerFactory loggerFactory,
 25        ILocalizationManager localizationManager)
 26    {
 027        _provider = provider;
 028        _localizationManager = localizationManager;
 029        _logger = loggerFactory.CreateLogger<MigrateRatingLevels>();
 030    }
 31
 32    /// <inheritdoc/>
 33    public void Perform()
 34    {
 035        _logger.LogInformation("Recalculating parental rating levels based on rating string.");
 036        using var context = _provider.CreateDbContext();
 037        using var transaction = context.Database.BeginTransaction();
 038        var ratings = context.BaseItems.AsNoTracking().Select(e => e.OfficialRating).Distinct();
 039        foreach (var rating in ratings)
 40        {
 041            if (string.IsNullOrEmpty(rating))
 42            {
 043                int? value = null;
 044                context.BaseItems
 045                    .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
 046                    .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, value));
 047                context.BaseItems
 048                    .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
 049                    .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, value));
 50            }
 51            else
 52            {
 053                var ratingValue = _localizationManager.GetRatingScore(rating);
 054                var score = ratingValue?.Score;
 055                var subScore = ratingValue?.SubScore;
 056                context.BaseItems
 057                    .Where(e => e.OfficialRating == rating)
 058                    .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, score));
 059                context.BaseItems
 060                    .Where(e => e.OfficialRating == rating)
 061                    .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, subScore));
 62            }
 63        }
 64
 065        transaction.Commit();
 066    }
 67}