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