using System;
using System.Linq;
using MediaBrowser.Common.Configuration;
using Microsoft.Extensions.Logging;
namespace Jellyfin.Server.Migrations
{
///
/// The class that knows how migrate between different Jellyfin versions.
///
public sealed class MigrationRunner
{
///
/// The list of known migrations, in order of applicability.
///
internal static readonly IMigrationRoutine[] Migrations =
{
new Routines.DisableTranscodingThrottling(),
new Routines.CreateUserLoggingConfigFile()
};
///
/// Run all needed migrations.
///
/// CoreAppHost that hosts current version.
/// Factory for making the logger.
public static void Run(CoreAppHost host, ILoggerFactory loggerFactory)
{
var logger = loggerFactory.CreateLogger();
var migrationOptions = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration(MigrationsListStore.StoreKey);
if (!host.ServerConfigurationManager.Configuration.IsStartupWizardCompleted)
{
// If startup wizard is not finished, this is a fresh install.
// Don't run any migrations, just mark all of them as applied.
logger.LogInformation("Marking all known migrations as applied because this is fresh install");
migrationOptions.Applied = Migrations.Select(m => m.Name).ToArray();
host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
return;
}
var applied = migrationOptions.Applied.ToList();
for (var i = 0; i < Migrations.Length; i++)
{
var migrationRoutine = Migrations[i];
if (applied.Contains(migrationRoutine.Name))
{
logger.LogDebug("Skipping migration {Name} as it is already applied", migrationRoutine.Name);
continue;
}
logger.LogInformation("Applying migration {Name}", migrationRoutine.Name);
try
{
migrationRoutine.Perform(host, logger);
}
catch (Exception ex)
{
logger.LogError(ex, "Cannot apply migration {Name}", migrationRoutine.Name);
continue;
}
logger.LogInformation("Migration {Name} applied successfully", migrationRoutine.Name);
applied.Add(migrationRoutine.Name);
}
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);
}
}
}
}