MigrationRunner.cs 3.5 KB

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