MigrationRunner.cs 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. using System;
  2. using System.Linq;
  3. using MediaBrowser.Common.Configuration;
  4. using Microsoft.Extensions.Logging;
  5. namespace Jellyfin.Server.Migrations
  6. {
  7. /// <summary>
  8. /// The class that knows which migrations to apply and how to apply them.
  9. /// </summary>
  10. public sealed class MigrationRunner
  11. {
  12. /// <summary>
  13. /// The list of known migrations, in order of applicability.
  14. /// </summary>
  15. internal static readonly IMigrationRoutine[] Migrations =
  16. {
  17. new Routines.DisableTranscodingThrottling(),
  18. new Routines.CreateUserLoggingConfigFile()
  19. };
  20. /// <summary>
  21. /// Run all needed migrations.
  22. /// </summary>
  23. /// <param name="host">CoreAppHost that hosts current version.</param>
  24. /// <param name="loggerFactory">Factory for making the logger.</param>
  25. public static void Run(CoreAppHost host, ILoggerFactory loggerFactory)
  26. {
  27. var logger = loggerFactory.CreateLogger<MigrationRunner>();
  28. var migrationOptions = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration<MigrationOptions>(MigrationsListStore.StoreKey);
  29. if (!host.ServerConfigurationManager.Configuration.IsStartupWizardCompleted && migrationOptions.Applied.Count == 0)
  30. {
  31. // If startup wizard is not finished, this is a fresh install.
  32. // Don't run any migrations, just mark all of them as applied.
  33. logger.LogInformation("Marking all known migrations as applied because this is a fresh install");
  34. migrationOptions.Applied.AddRange(Migrations.Select(m => (m.Id, m.Name)));
  35. host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
  36. return;
  37. }
  38. var appliedMigrationIds = migrationOptions.Applied.Select(m => m.Id).ToHashSet();
  39. for (var i = 0; i < Migrations.Length; i++)
  40. {
  41. var migrationRoutine = Migrations[i];
  42. if (appliedMigrationIds.Contains(migrationRoutine.Id))
  43. {
  44. logger.LogDebug("Skipping migration '{Name}' since it is already applied", migrationRoutine.Name);
  45. continue;
  46. }
  47. logger.LogInformation("Applying migration '{Name}'", migrationRoutine.Name);
  48. try
  49. {
  50. migrationRoutine.Perform(host, logger);
  51. }
  52. catch (Exception ex)
  53. {
  54. logger.LogError(ex, "Could not apply migration '{Name}'", migrationRoutine.Name);
  55. throw;
  56. }
  57. // Mark the migration as completed
  58. logger.LogInformation("Migration '{Name}' applied successfully", migrationRoutine.Name);
  59. migrationOptions.Applied.Add((migrationRoutine.Id, migrationRoutine.Name));
  60. host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
  61. logger.LogDebug("Migration '{Name}' marked as applied in configuration.", migrationRoutine.Name);
  62. }
  63. }
  64. }
  65. }