MigrateRatingLevels.cs 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using System;
  2. using System.Globalization;
  3. using System.IO;
  4. using MediaBrowser.Controller;
  5. using Microsoft.Extensions.Logging;
  6. using SQLitePCL.pretty;
  7. namespace Jellyfin.Server.Migrations.Routines
  8. {
  9. /// <summary>
  10. /// Migrate rating levels to new rating level system.
  11. /// </summary>
  12. internal class MigrateRatingLevels : IMigrationRoutine
  13. {
  14. private const string DbFilename = "library.db";
  15. private readonly ILogger<MigrateRatingLevels> _logger;
  16. private readonly IServerApplicationPaths _paths;
  17. public MigrateRatingLevels(ILogger<MigrateRatingLevels> logger, IServerApplicationPaths paths)
  18. {
  19. _logger = logger;
  20. _paths = paths;
  21. }
  22. /// <inheritdoc/>
  23. public Guid Id => Guid.Parse("{67445D54-B895-4B24-9F4C-35CE0690EA07}");
  24. /// <inheritdoc/>
  25. public string Name => "MigrateRatingLevels";
  26. /// <inheritdoc/>
  27. public bool PerformOnNewInstall => false;
  28. /// <inheritdoc/>
  29. public void Perform()
  30. {
  31. var dataPath = _paths.DataPath;
  32. var dbPath = Path.Combine(dataPath, DbFilename);
  33. using (var connection = SQLite3.Open(
  34. dbPath,
  35. ConnectionFlags.ReadWrite,
  36. null))
  37. {
  38. // Back up the database before deleting any entries
  39. for (int i = 1; ; i++)
  40. {
  41. var bakPath = string.Format(CultureInfo.InvariantCulture, "{0}.bak{1}", dbPath, i);
  42. if (!File.Exists(bakPath))
  43. {
  44. try
  45. {
  46. File.Copy(dbPath, bakPath);
  47. _logger.LogInformation("Library database backed up to {BackupPath}", bakPath);
  48. break;
  49. }
  50. catch (Exception ex)
  51. {
  52. _logger.LogError(ex, "Cannot make a backup of {Library} at path {BackupPath}", DbFilename, bakPath);
  53. throw;
  54. }
  55. }
  56. }
  57. // Migrate parental rating levels to new schema
  58. _logger.LogInformation("Migrating parental rating levels.");
  59. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = NULL WHERE OfficialRating = 'NR'");
  60. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = NULL WHERE InheritedParentalRatingValue = ''");
  61. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = NULL WHERE InheritedParentalRatingValue = 0");
  62. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = 1000 WHERE InheritedParentalRatingValue = 100");
  63. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = 1000 WHERE InheritedParentalRatingValue = 15");
  64. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = 18 WHERE InheritedParentalRatingValue = 10");
  65. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = 18 WHERE InheritedParentalRatingValue = 9");
  66. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = 16 WHERE InheritedParentalRatingValue = 8");
  67. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = 12 WHERE InheritedParentalRatingValue = 7");
  68. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = 12 WHERE InheritedParentalRatingValue = 6");
  69. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = 12 WHERE InheritedParentalRatingValue = 5");
  70. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = 7 WHERE InheritedParentalRatingValue = 4");
  71. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = 6 WHERE InheritedParentalRatingValue = 3");
  72. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = 6 WHERE InheritedParentalRatingValue = 2");
  73. connection.Execute("UPDATE TypedBaseItems SET InheritedParentalRatingValue = 0 WHERE InheritedParentalRatingValue = 1");
  74. }
  75. }
  76. }
  77. }