MigrateRatingLevels.cs 2.7 KB

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