jellyfin/Jellyfin.Server/Migrations/MigrationRunner.cs

74 lines
3.0 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>
2020-03-07 19:18:45 +00:00
/// The class that knows which migrations to apply and how to apply them.
/// </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>
2020-03-06 20:51:50 +00:00
internal static readonly IMigrationRoutine[] Migrations =
{
new Routines.DisableTranscodingThrottling(),
new Routines.CreateUserLoggingConfigFile()
};
/// <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-06 16:01:07 +00:00
if (!host.ServerConfigurationManager.Configuration.IsStartupWizardCompleted && migrationOptions.Applied.Length == 0)
{
// 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 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-06 20:51:50 +00:00
var migrationRoutine = Migrations[i];
if (applied.Contains(migrationRoutine.Name))
{
2020-03-06 20:51:50 +00:00
logger.LogDebug("Skipping migration {Name} as it is already applied", migrationRoutine.Name);
continue;
}
2020-03-06 20:51:50 +00:00
logger.LogInformation("Applying migration {Name}", migrationRoutine.Name);
2020-03-05 17:09:33 +00:00
try
{
2020-03-06 20:51:50 +00:00
migrationRoutine.Perform(host, logger);
2020-03-05 17:09:33 +00:00
}
catch (Exception ex)
{
logger.LogError(ex, "Could not apply migration {Name}", migrationRoutine.Name);
throw;
}
// Mark the migration as completed
2020-03-06 20:51:50 +00:00
logger.LogInformation("Migration {Name} applied successfully", migrationRoutine.Name);
applied.Add(migrationRoutine.Name);
2020-03-05 17:09:33 +00:00
migrationOptions.Applied = applied.ToArray();
host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
2020-03-05 17:09:33 +00:00
}
}
}
}