MigrateRatingLevels.cs 4.5 KB

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