2
0

MigrateRatingLevels.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. using System;
  2. using System.Linq;
  3. using Jellyfin.Database.Implementations;
  4. using MediaBrowser.Model.Globalization;
  5. using Microsoft.EntityFrameworkCore;
  6. using Microsoft.Extensions.Logging;
  7. namespace Jellyfin.Server.Migrations.Routines
  8. {
  9. /// <summary>
  10. /// Migrate rating levels.
  11. /// </summary>
  12. [JellyfinMigration("2025-04-20T22:00:00", nameof(MigrateRatingLevels), "98724538-EB11-40E3-931A-252C55BDDE7A")]
  13. internal class MigrateRatingLevels : IDatabaseMigrationRoutine
  14. {
  15. private readonly ILogger<MigrateRatingLevels> _logger;
  16. private readonly IDbContextFactory<JellyfinDbContext> _provider;
  17. private readonly ILocalizationManager _localizationManager;
  18. public MigrateRatingLevels(
  19. IDbContextFactory<JellyfinDbContext> provider,
  20. ILoggerFactory loggerFactory,
  21. ILocalizationManager localizationManager)
  22. {
  23. _provider = provider;
  24. _localizationManager = localizationManager;
  25. _logger = loggerFactory.CreateLogger<MigrateRatingLevels>();
  26. }
  27. /// <inheritdoc/>
  28. public void Perform()
  29. {
  30. _logger.LogInformation("Recalculating parental rating levels based on rating string.");
  31. using var context = _provider.CreateDbContext();
  32. using var transaction = context.Database.BeginTransaction();
  33. var ratings = context.BaseItems.AsNoTracking().Select(e => e.OfficialRating).Distinct();
  34. foreach (var rating in ratings)
  35. {
  36. if (string.IsNullOrEmpty(rating))
  37. {
  38. int? value = null;
  39. context.BaseItems
  40. .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
  41. .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, value));
  42. context.BaseItems
  43. .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
  44. .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, value));
  45. }
  46. else
  47. {
  48. var ratingValue = _localizationManager.GetRatingScore(rating);
  49. var score = ratingValue?.Score;
  50. var subScore = ratingValue?.SubScore;
  51. context.BaseItems
  52. .Where(e => e.OfficialRating == rating)
  53. .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, score));
  54. context.BaseItems
  55. .Where(e => e.OfficialRating == rating)
  56. .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, subScore));
  57. }
  58. }
  59. transaction.Commit();
  60. }
  61. }
  62. }