MigrationRunner.cs 3.5 KB

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