aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/Migrations/Routines/MigrateRatingLevels.cs
blob: 9aed449882a356ac1372b798caea2bf50d1ebd12 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
using System;
using System.Linq;
using Jellyfin.Database.Implementations;
using MediaBrowser.Model.Globalization;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Server.Migrations.Routines;

/// <summary>
/// Migrate rating levels.
/// </summary>
#pragma warning disable CS0618 // Type or member is obsolete
[JellyfinMigration("2025-04-20T22:00:00", nameof(MigrateRatingLevels))]
[JellyfinMigrationBackup(JellyfinDb = true)]
#pragma warning restore CS0618 // Type or member is obsolete
internal class MigrateRatingLevels : IDatabaseMigrationRoutine
{
    private readonly ILogger<MigrateRatingLevels> _logger;
    private readonly IDbContextFactory<JellyfinDbContext> _provider;
    private readonly ILocalizationManager _localizationManager;

    public MigrateRatingLevels(
        IDbContextFactory<JellyfinDbContext> provider,
        ILoggerFactory loggerFactory,
        ILocalizationManager localizationManager)
    {
        _provider = provider;
        _localizationManager = localizationManager;
        _logger = loggerFactory.CreateLogger<MigrateRatingLevels>();
    }

    /// <inheritdoc/>
    public void Perform()
    {
        _logger.LogInformation("Recalculating parental rating levels based on rating string.");
        using var context = _provider.CreateDbContext();
        using var transaction = context.Database.BeginTransaction();
        var ratings = context.BaseItems.AsNoTracking().Select(e => e.OfficialRating).Distinct();
        foreach (var rating in ratings)
        {
            if (string.IsNullOrEmpty(rating))
            {
                int? value = null;
                context.BaseItems
                    .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
                    .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, value));
                context.BaseItems
                    .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
                    .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, value));
            }
            else
            {
                var ratingValue = _localizationManager.GetRatingScore(rating);
                var score = ratingValue?.Score;
                var subScore = ratingValue?.SubScore;
                context.BaseItems
                    .Where(e => e.OfficialRating == rating)
                    .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, score));
                context.BaseItems
                    .Where(e => e.OfficialRating == rating)
                    .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, subScore));
            }
        }

        transaction.Commit();
    }
}