MigrationRunner.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Linq;
  3. using MediaBrowser.Common.Configuration;
  4. using Microsoft.Extensions.Logging;
  5. namespace Jellyfin.Server.Migrations
  6. {
  7. /// <summary>
  8. /// The class that knows which migrations to apply and how to apply them.
  9. /// </summary>
  10. public sealed class MigrationRunner
  11. {
  12. /// <summary>
  13. /// The list of known migrations, in order of applicability.
  14. /// </summary>
  15. internal static readonly IUpdater[] Migrations =
  16. {
  17. new Routines.DisableTranscodingThrottling()
  18. };
  19. /// <summary>
  20. /// Run all needed migrations.
  21. /// </summary>
  22. /// <param name="host">CoreAppHost that hosts current version.</param>
  23. /// <param name="loggerFactory">Factory for making the logger.</param>
  24. public static void Run(CoreAppHost host, ILoggerFactory loggerFactory)
  25. {
  26. var logger = loggerFactory.CreateLogger<MigrationRunner>();
  27. var migrationOptions = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration<MigrationOptions>(MigrationsListStore.StoreKey);
  28. if (!host.ServerConfigurationManager.Configuration.IsStartupWizardCompleted && migrationOptions.Applied.Length == 0)
  29. {
  30. // If startup wizard is not finished, this is a fresh install.
  31. // Don't run any migrations, just mark all of them as applied.
  32. logger.LogInformation("Marking all known migrations as applied because this is fresh install");
  33. migrationOptions.Applied = Migrations.Select(m => m.Name).ToArray();
  34. host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
  35. return;
  36. }
  37. var applied = migrationOptions.Applied.ToList();
  38. for (var i = 0; i < Migrations.Length; i++)
  39. {
  40. var updater = Migrations[i];
  41. if (applied.Contains(updater.Name))
  42. {
  43. logger.LogDebug("Skipping migration '{Name}' since it is already applied", updater.Name);
  44. continue;
  45. }
  46. logger.LogInformation("Applying migration '{Name}'", updater.Name);
  47. try
  48. {
  49. updater.Perform(host, logger);
  50. }
  51. catch (Exception ex)
  52. {
  53. logger.LogError(ex, "Could not apply migration '{Name}'", updater.Name);
  54. throw;
  55. }
  56. logger.LogInformation("Migration '{Name}' applied successfully", updater.Name);
  57. applied.Add(updater.Name);
  58. }
  59. if (applied.Count > migrationOptions.Applied.Length)
  60. {
  61. logger.LogInformation("Some migrations were run, saving the state");
  62. migrationOptions.Applied = applied.ToArray();
  63. host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
  64. }
  65. }
  66. }
  67. }