2
0

MigrationRunner.cs 3.7 KB

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