Files
jellyfin-jellyfin-1/Jellyfin.Server/Migrations/Routines/MigrateRatingLevels.cs

67 lines
2.8 KiB
C#
Raw Normal View History

2023-05-11 01:38:54 +02:00
using System;
2025-03-31 05:51:54 +02:00
using System.Linq;
using Jellyfin.Database.Implementations;
2023-05-11 01:38:54 +02:00
using MediaBrowser.Model.Globalization;
2025-03-31 05:51:54 +02:00
using Microsoft.EntityFrameworkCore;
2023-05-11 01:38:54 +02:00
using Microsoft.Extensions.Logging;
namespace Jellyfin.Server.Migrations.Routines
{
/// <summary>
2025-03-31 05:51:54 +02:00
/// Migrate rating levels.
2023-05-11 01:38:54 +02:00
/// </summary>
2025-04-28 03:18:08 +03:00
[JellyfinMigration("2025-04-20T22:00:00", nameof(MigrateRatingLevels), "98724538-EB11-40E3-931A-252C55BDDE7A")]
2025-03-31 05:51:54 +02:00
internal class MigrateRatingLevels : IDatabaseMigrationRoutine
2023-05-11 01:38:54 +02:00
{
private readonly ILogger<MigrateRatingLevels> _logger;
2025-03-31 05:51:54 +02:00
private readonly IDbContextFactory<JellyfinDbContext> _provider;
2023-05-11 01:38:54 +02:00
private readonly ILocalizationManager _localizationManager;
public MigrateRatingLevels(
2025-03-31 05:51:54 +02:00
IDbContextFactory<JellyfinDbContext> provider,
2023-05-11 01:38:54 +02:00
ILoggerFactory loggerFactory,
2023-08-21 15:31:02 +02:00
ILocalizationManager localizationManager)
2023-05-11 01:38:54 +02:00
{
2025-03-31 05:51:54 +02:00
_provider = provider;
2023-05-11 01:38:54 +02:00
_localizationManager = localizationManager;
_logger = loggerFactory.CreateLogger<MigrateRatingLevels>();
}
/// <inheritdoc/>
public void Perform()
{
2025-03-31 05:51:54 +02:00
_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)
2023-05-11 01:38:54 +02:00
{
2025-03-31 05:51:54 +02:00
if (string.IsNullOrEmpty(rating))
2023-05-11 01:38:54 +02:00
{
2025-03-31 05:51:54 +02:00
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));
2023-05-11 01:38:54 +02:00
}
2025-03-31 05:51:54 +02:00
else
2023-05-11 01:38:54 +02:00
{
2025-03-31 05:51:54 +02:00
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));
2023-05-11 01:38:54 +02:00
}
}
2025-03-31 05:51:54 +02:00
transaction.Commit();
2023-05-11 01:38:54 +02:00
}
}
}