MigrateRatingLevels.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. /// <summary>
  9. /// Migrate rating levels.
  10. /// </summary>
  11. #pragma warning disable CS0618 // Type or member is obsolete
  12. [JellyfinMigration("2025-04-20T22:00:00", nameof(MigrateRatingLevels))]
  13. #pragma warning restore CS0618 // Type or member is obsolete
  14. internal class MigrateRatingLevels : IDatabaseMigrationRoutine
  15. {
  16. private readonly ILogger<MigrateRatingLevels> _logger;
  17. private readonly IDbContextFactory<JellyfinDbContext> _provider;
  18. private readonly ILocalizationManager _localizationManager;
  19. public MigrateRatingLevels(
  20. IDbContextFactory<JellyfinDbContext> provider,
  21. ILoggerFactory loggerFactory,
  22. ILocalizationManager localizationManager)
  23. {
  24. _provider = provider;
  25. _localizationManager = localizationManager;
  26. _logger = loggerFactory.CreateLogger<MigrateRatingLevels>();
  27. }
  28. /// <inheritdoc/>
  29. public void Perform()
  30. {
  31. _logger.LogInformation("Recalculating parental rating levels based on rating string.");
  32. using var context = _provider.CreateDbContext();
  33. using var transaction = context.Database.BeginTransaction();
  34. var ratings = context.BaseItems.AsNoTracking().Select(e => e.OfficialRating).Distinct();
  35. foreach (var rating in ratings)
  36. {
  37. if (string.IsNullOrEmpty(rating))
  38. {
  39. int? value = null;
  40. context.BaseItems
  41. .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
  42. .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, value));
  43. context.BaseItems
  44. .Where(e => e.OfficialRating == null || e.OfficialRating == string.Empty)
  45. .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, value));
  46. }
  47. else
  48. {
  49. var ratingValue = _localizationManager.GetRatingScore(rating);
  50. var score = ratingValue?.Score;
  51. var subScore = ratingValue?.SubScore;
  52. context.BaseItems
  53. .Where(e => e.OfficialRating == rating)
  54. .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingValue, score));
  55. context.BaseItems
  56. .Where(e => e.OfficialRating == rating)
  57. .ExecuteUpdate(f => f.SetProperty(e => e.InheritedParentalRatingSubValue, subScore));
  58. }
  59. }
  60. transaction.Commit();
  61. }
  62. }