< 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: 31
Coverable lines: 31
Total lines: 74
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%
get_Id()100%210%
get_Name()100%210%
get_PerformOnNewInstall()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    internal class MigrateRatingLevels : IDatabaseMigrationRoutine
 14    {
 15        private readonly ILogger<MigrateRatingLevels> _logger;
 16        private readonly IDbContextFactory<JellyfinDbContext> _provider;
 17        private readonly ILocalizationManager _localizationManager;
 18
 19        public MigrateRatingLevels(
 20            IDbContextFactory<JellyfinDbContext> provider,
 21            ILoggerFactory loggerFactory,
 22            ILocalizationManager localizationManager)
 23        {
 024            _provider = provider;
 025            _localizationManager = localizationManager;
 026            _logger = loggerFactory.CreateLogger<MigrateRatingLevels>();
 027        }
 28
 29        /// <inheritdoc/>
 030        public Guid Id => Guid.Parse("{98724538-EB11-40E3-931A-252C55BDDE7A}");
 31
 32        /// <inheritdoc/>
 033        public string Name => "MigrateRatingLevels";
 34
 35        /// <inheritdoc/>
 036        public bool PerformOnNewInstall => false;
 37
 38        /// <inheritdoc/>
 39        public void Perform()
 40        {
 041            _logger.LogInformation("Recalculating parental rating levels based on rating string.");
 042            using var context = _provider.CreateDbContext();
 043            using var transaction = context.Database.BeginTransaction();
 044            var ratings = context.BaseItems.AsNoTracking().Select(e => e.OfficialRating).Distinct();
 045            foreach (var rating in ratings)
 46            {
 047                if (string.IsNullOrEmpty(rating))
 48                {
 049                    int? value = null;
 050                    context.BaseItems
 051                        .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
 052                        .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, value));
 053                    context.BaseItems
 054                        .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
 055                        .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, value));
 56                }
 57                else
 58                {
 059                    var ratingValue = _localizationManager.GetRatingScore(rating);
 060                    var score = ratingValue?.Score;
 061                    var subScore = ratingValue?.SubScore;
 062                    context.BaseItems
 063                        .Where(e => e.OfficialRating == rating)
 064                        .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, score));
 065                    context.BaseItems
 066                        .Where(e => e.OfficialRating == rating)
 067                        .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, subScore));
 68                }
 69            }
 70
 071            transaction.Commit();
 072        }
 73    }
 74}