Migrations.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using System.Collections.Generic;
  3. using MediaBrowser.Common.Configuration;
  4. using MediaBrowser.Model.Configuration;
  5. using Microsoft.Extensions.Logging;
  6. namespace Jellyfin.Server
  7. {
  8. /// <summary>
  9. /// The class that knows how migrate between different Jellyfin versions.
  10. /// </summary>
  11. internal static class Migrations
  12. {
  13. private static readonly IUpdater[] _migrations =
  14. {
  15. new Pre10_5()
  16. };
  17. /// <summary>
  18. /// Interface that descibes a migration routine.
  19. /// </summary>
  20. private interface IUpdater
  21. {
  22. /// <summary>
  23. /// Gets maximum version this Updater applies to.
  24. /// If current version is greater or equal to it, skip the updater.
  25. /// </summary>
  26. public abstract Version Maximum { get; }
  27. /// <summary>
  28. /// Execute the migration from version "from".
  29. /// </summary>
  30. /// <param name="host">Host that hosts current version.</param>
  31. /// <param name="logger">Host logger.</param>
  32. /// <param name="from">Version to migrate from.</param>
  33. /// <returns>Whether configuration was changed.</returns>
  34. public abstract bool Perform(CoreAppHost host, ILogger logger, Version from);
  35. }
  36. /// <summary>
  37. /// Run all needed migrations.
  38. /// </summary>
  39. /// <param name="host">CoreAppHost that hosts current version.</param>
  40. /// <param name="logger">AppHost logger.</param>
  41. /// <returns>Whether anything was changed.</returns>
  42. public static bool Run(CoreAppHost host, ILogger logger)
  43. {
  44. bool updated = false;
  45. var version = host.ServerConfigurationManager.CommonConfiguration.PreviousVersion;
  46. for (var i = 0; i < _migrations.Length; i++)
  47. {
  48. var updater = _migrations[i];
  49. if (version.CompareTo(updater.Maximum) >= 0)
  50. {
  51. logger.LogDebug("Skipping updater {0} as current version {1} >= its maximum applicable version {2}", updater, version, updater.Maximum);
  52. continue;
  53. }
  54. if (updater.Perform(host, logger, version))
  55. {
  56. updated = true;
  57. }
  58. version = updater.Maximum;
  59. }
  60. return updated;
  61. }
  62. private class Pre10_5 : IUpdater
  63. {
  64. public Version Maximum { get => Version.Parse("10.5.0"); }
  65. public bool Perform(CoreAppHost host, ILogger logger, Version from)
  66. {
  67. // Set EnableThrottling to false as it wasn't used before, and in 10.5.0 it may introduce issues
  68. var encoding = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration<EncodingOptions>("encoding");
  69. if (encoding.EnableThrottling)
  70. {
  71. logger.LogInformation("Disabling transcoding throttling during migration");
  72. encoding.EnableThrottling = false;
  73. host.ServerConfigurationManager.SaveConfiguration("encoding", encoding);
  74. return true;
  75. }
  76. return false;
  77. }
  78. }
  79. }
  80. }