2
0

MigrationRunner.cs 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.Select(m => (m.Id, m.Name)));
  44. host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
  45. return;
  46. }
  47. var appliedMigrationIds = migrationOptions.Applied.Select(m => m.Id).ToHashSet();
  48. for (var i = 0; i < migrations.Length; i++)
  49. {
  50. var migrationRoutine = migrations[i];
  51. if (appliedMigrationIds.Contains(migrationRoutine.Id))
  52. {
  53. logger.LogDebug("Skipping migration '{Name}' since it is already applied", migrationRoutine.Name);
  54. continue;
  55. }
  56. logger.LogInformation("Applying migration '{Name}'", migrationRoutine.Name);
  57. try
  58. {
  59. migrationRoutine.Perform();
  60. }
  61. catch (Exception ex)
  62. {
  63. logger.LogError(ex, "Could not apply migration '{Name}'", migrationRoutine.Name);
  64. throw;
  65. }
  66. // Mark the migration as completed
  67. logger.LogInformation("Migration '{Name}' applied successfully", migrationRoutine.Name);
  68. migrationOptions.Applied.Add((migrationRoutine.Id, migrationRoutine.Name));
  69. host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
  70. logger.LogDebug("Migration '{Name}' marked as applied in configuration.", migrationRoutine.Name);
  71. }
  72. }
  73. }
  74. }