MigrationRunner.cs 3.5 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.MigrateUserDb)
  23. };
  24. /// <summary>
  25. /// Run all needed migrations.
  26. /// </summary>
  27. /// <param name="host">CoreAppHost that hosts current version.</param>
  28. /// <param name="loggerFactory">Factory for making the logger.</param>
  29. public static void Run(CoreAppHost host, ILoggerFactory loggerFactory)
  30. {
  31. var logger = loggerFactory.CreateLogger<MigrationRunner>();
  32. var migrations = _migrationTypes
  33. .Select(m => ActivatorUtilities.CreateInstance(host.ServiceProvider, m))
  34. .OfType<IMigrationRoutine>()
  35. .ToArray();
  36. var migrationOptions = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration<MigrationOptions>(MigrationsListStore.StoreKey);
  37. if (!host.ServerConfigurationManager.Configuration.IsStartupWizardCompleted && migrationOptions.Applied.Count == 0)
  38. {
  39. // If startup wizard is not finished, this is a fresh install.
  40. // Don't run any migrations, just mark all of them as applied.
  41. logger.LogInformation("Marking all known migrations as applied because this is a fresh install");
  42. migrationOptions.Applied.AddRange(migrations.Select(m => (m.Id, m.Name)));
  43. host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
  44. return;
  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. }