< 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: 69
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 Jellyfin.Server.ServerSetupApp;
 5using MediaBrowser.Model.Globalization;
 6using Microsoft.EntityFrameworkCore;
 7using Microsoft.Extensions.Logging;
 8
 9namespace Jellyfin.Server.Migrations.Routines;
 10
 11/// <summary>
 12/// Migrate rating levels.
 13/// </summary>
 14#pragma warning disable CS0618 // Type or member is obsolete
 15[JellyfinMigration("2025-04-20T22:00:00", nameof(MigrateRatingLevels))]
 16[JellyfinMigrationBackup(JellyfinDb = true)]
 17#pragma warning restore CS0618 // Type or member is obsolete
 18internal class MigrateRatingLevels : IDatabaseMigrationRoutine
 19{
 20    private readonly IStartupLogger _logger;
 21    private readonly IDbContextFactory<JellyfinDbContext> _provider;
 22    private readonly ILocalizationManager _localizationManager;
 23
 24    public MigrateRatingLevels(
 25        IDbContextFactory<JellyfinDbContext> provider,
 26        IStartupLogger logger,
 27        ILocalizationManager localizationManager)
 28    {
 029        _provider = provider;
 030        _localizationManager = localizationManager;
 031        _logger = logger;
 032    }
 33
 34    /// <inheritdoc/>
 35    public void Perform()
 36    {
 037        _logger.LogInformation("Recalculating parental rating levels based on rating string.");
 038        using var context = _provider.CreateDbContext();
 039        using var transaction = context.Database.BeginTransaction();
 040        var ratings = context.BaseItems.AsNoTracking().Select(e => e.OfficialRating).Distinct();
 041        foreach (var rating in ratings)
 42        {
 043            if (string.IsNullOrEmpty(rating))
 44            {
 045                int? value = null;
 046                context.BaseItems
 047                    .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
 048                    .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, value));
 049                context.BaseItems
 050                    .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
 051                    .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, value));
 52            }
 53            else
 54            {
 055                var ratingValue = _localizationManager.GetRatingScore(rating);
 056                var score = ratingValue?.Score;
 057                var subScore = ratingValue?.SubScore;
 058                context.BaseItems
 059                    .Where(e => e.OfficialRating == rating)
 060                    .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, score));
 061                context.BaseItems
 062                    .Where(e => e.OfficialRating == rating)
 063                    .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, subScore));
 64            }
 65        }
 66
 067        transaction.Commit();
 068    }
 69}