MigrationRunner.cs 3.6 KB

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