MigrationRunner.cs 3.8 KB

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