2
0

MigrationRunner.cs 3.8 KB

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