jellyfin-server/Jellyfin.Server/Migrations/MigrationRunner.cs

64 lines
2.2 KiB
C#
Raw Normal View History

2020-03-05 17:09:33 +00:00
using System;
using System.Linq;
using MediaBrowser.Common.Configuration;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Server.Migrations
{
/// <summary>
/// The class that knows how migrate between different Jellyfin versions.
/// </summary>
2020-03-05 17:09:33 +00:00
public sealed class MigrationRunner
{
2020-03-05 17:09:33 +00:00
/// <summary>
/// The list of known migrations, in order of applicability.
/// </summary>
internal static readonly IUpdater[] Migrations =
{
new Routines.DisableTranscodingThrottling(),
new Routines.DisableZealousLogging()
};
/// <summary>
/// Run all needed migrations.
/// </summary>
/// <param name="host">CoreAppHost that hosts current version.</param>
2020-03-05 17:09:33 +00:00
/// <param name="loggerFactory">Factory for making the logger.</param>
public static void Run(CoreAppHost host, ILoggerFactory loggerFactory)
{
2020-03-05 17:09:33 +00:00
var logger = loggerFactory.CreateLogger<MigrationRunner>();
var migrationOptions = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration<MigrationOptions>(MigrationsListStore.StoreKey);
2020-03-05 17:09:33 +00:00
var applied = migrationOptions.Applied.ToList();
2020-03-05 17:09:33 +00:00
for (var i = 0; i < Migrations.Length; i++)
{
2020-03-05 17:09:33 +00:00
var updater = Migrations[i];
if (applied.Contains(updater.Name))
{
2020-03-05 17:09:33 +00:00
logger.LogDebug("Skipping migration {0} as it is already applied", updater.Name);
continue;
}
2020-03-05 17:09:33 +00:00
try
{
2020-03-05 17:09:33 +00:00
updater.Perform(host, logger);
}
catch (Exception ex)
{
logger.LogError(ex, "Cannot apply migration {0}", updater.Name);
continue;
}
2020-03-05 17:09:33 +00:00
applied.Add(updater.Name);
}
2020-03-05 17:09:33 +00:00
if (applied.Count > migrationOptions.Applied.Length)
{
logger.LogInformation("Some migrations were run, saving the state");
migrationOptions.Applied = applied.ToArray();
host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
2020-03-05 17:09:33 +00:00
}
}
}
}