MigrationRunner.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. };
  21. /// <summary>
  22. /// Run all needed migrations.
  23. /// </summary>
  24. /// <param name="host">CoreAppHost that hosts current version.</param>
  25. /// <param name="loggerFactory">Factory for making the logger.</param>
  26. public static void Run(CoreAppHost host, ILoggerFactory loggerFactory)
  27. {
  28. var logger = loggerFactory.CreateLogger<MigrationRunner>();
  29. var migrations = _migrationTypes
  30. .Select(m => ActivatorUtilities.CreateInstance(host.ServiceProvider, m))
  31. .OfType<IMigrationRoutine>()
  32. .ToArray();
  33. var migrationOptions = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration<MigrationOptions>(MigrationsListStore.StoreKey);
  34. if (!host.ServerConfigurationManager.Configuration.IsStartupWizardCompleted && migrationOptions.Applied.Count == 0)
  35. {
  36. // If startup wizard is not finished, this is a fresh install.
  37. // Don't run any migrations, just mark all of them as applied.
  38. logger.LogInformation("Marking all known migrations as applied because this is a fresh install");
  39. migrationOptions.Applied.AddRange(migrations.Select(m => (m.Id, m.Name)));
  40. host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
  41. return;
  42. }
  43. var appliedMigrationIds = migrationOptions.Applied.Select(m => m.Id).ToHashSet();
  44. for (var i = 0; i < migrations.Length; i++)
  45. {
  46. var migrationRoutine = migrations[i];
  47. if (appliedMigrationIds.Contains(migrationRoutine.Id))
  48. {
  49. logger.LogDebug("Skipping migration '{Name}' since it is already applied", migrationRoutine.Name);
  50. continue;
  51. }
  52. logger.LogInformation("Applying migration '{Name}'", migrationRoutine.Name);
  53. try
  54. {
  55. migrationRoutine.Perform();
  56. }
  57. catch (Exception ex)
  58. {
  59. logger.LogError(ex, "Could not apply migration '{Name}'", migrationRoutine.Name);
  60. throw;
  61. }
  62. // Mark the migration as completed
  63. logger.LogInformation("Migration '{Name}' applied successfully", migrationRoutine.Name);
  64. migrationOptions.Applied.Add((migrationRoutine.Id, migrationRoutine.Name));
  65. host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
  66. logger.LogDebug("Migration '{Name}' marked as applied in configuration.", migrationRoutine.Name);
  67. }
  68. }
  69. }
  70. }