2020-03-05 10:09:33 -07:00
|
|
|
using System;
|
|
|
|
using System.Linq;
|
|
|
|
using MediaBrowser.Common.Configuration;
|
2020-04-16 20:40:32 -07:00
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2020-03-05 08:21:27 -07:00
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
|
|
namespace Jellyfin.Server.Migrations
|
|
|
|
{
|
|
|
|
/// <summary>
|
2020-03-07 12:18:45 -07:00
|
|
|
/// The class that knows which migrations to apply and how to apply them.
|
2020-03-05 08:21:27 -07:00
|
|
|
/// </summary>
|
2020-03-05 10:09:33 -07:00
|
|
|
public sealed class MigrationRunner
|
2020-03-05 08:21:27 -07:00
|
|
|
{
|
2020-03-05 10:09:33 -07:00
|
|
|
/// <summary>
|
|
|
|
/// The list of known migrations, in order of applicability.
|
|
|
|
/// </summary>
|
2020-04-16 20:40:32 -07:00
|
|
|
private static readonly Type[] _migrationTypes =
|
2020-03-05 08:21:27 -07:00
|
|
|
{
|
2020-04-16 20:40:32 -07:00
|
|
|
typeof(Routines.DisableTranscodingThrottling),
|
2020-05-14 16:30:28 -07:00
|
|
|
typeof(Routines.CreateUserLoggingConfigFile),
|
2020-05-15 11:49:45 -07:00
|
|
|
typeof(Routines.RemoveDuplicateExtras)
|
2020-03-05 08:21:27 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Run all needed migrations.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="host">CoreAppHost that hosts current version.</param>
|
2020-03-05 10:09:33 -07:00
|
|
|
/// <param name="loggerFactory">Factory for making the logger.</param>
|
|
|
|
public static void Run(CoreAppHost host, ILoggerFactory loggerFactory)
|
2020-03-05 08:21:27 -07:00
|
|
|
{
|
2020-03-05 10:09:33 -07:00
|
|
|
var logger = loggerFactory.CreateLogger<MigrationRunner>();
|
2020-04-16 20:40:32 -07:00
|
|
|
var migrations = _migrationTypes
|
|
|
|
.Select(m => ActivatorUtilities.CreateInstance(host.ServiceProvider, m))
|
|
|
|
.OfType<IMigrationRoutine>()
|
|
|
|
.ToArray();
|
2020-03-05 10:37:49 -07:00
|
|
|
var migrationOptions = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration<MigrationOptions>(MigrationsListStore.StoreKey);
|
2020-03-06 03:22:44 -07:00
|
|
|
|
2020-03-08 08:05:31 -07:00
|
|
|
if (!host.ServerConfigurationManager.Configuration.IsStartupWizardCompleted && migrationOptions.Applied.Count == 0)
|
2020-03-06 03:22:44 -07:00
|
|
|
{
|
|
|
|
// If startup wizard is not finished, this is a fresh install.
|
|
|
|
// Don't run any migrations, just mark all of them as applied.
|
2020-03-08 09:40:30 -07:00
|
|
|
logger.LogInformation("Marking all known migrations as applied because this is a fresh install");
|
2020-04-16 20:40:32 -07:00
|
|
|
migrationOptions.Applied.AddRange(migrations.Select(m => (m.Id, m.Name)));
|
2020-03-06 03:22:44 -07:00
|
|
|
host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-03-08 09:40:30 -07:00
|
|
|
var appliedMigrationIds = migrationOptions.Applied.Select(m => m.Id).ToHashSet();
|
|
|
|
|
2020-04-16 20:40:32 -07:00
|
|
|
for (var i = 0; i < migrations.Length; i++)
|
2020-03-05 08:21:27 -07:00
|
|
|
{
|
2020-04-16 20:40:32 -07:00
|
|
|
var migrationRoutine = migrations[i];
|
2020-03-08 09:40:30 -07:00
|
|
|
if (appliedMigrationIds.Contains(migrationRoutine.Id))
|
2020-03-05 08:21:27 -07:00
|
|
|
{
|
2020-03-08 07:02:59 -07:00
|
|
|
logger.LogDebug("Skipping migration '{Name}' since it is already applied", migrationRoutine.Name);
|
2020-03-05 08:21:27 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-03-08 07:02:59 -07:00
|
|
|
logger.LogInformation("Applying migration '{Name}'", migrationRoutine.Name);
|
2020-03-07 19:19:24 -07:00
|
|
|
|
2020-03-05 10:09:33 -07:00
|
|
|
try
|
2020-03-05 08:21:27 -07:00
|
|
|
{
|
2020-04-16 20:40:32 -07:00
|
|
|
migrationRoutine.Perform();
|
2020-03-05 10:09:33 -07:00
|
|
|
}
|
|
|
|
catch (Exception ex)
|
|
|
|
{
|
2020-03-08 07:02:59 -07:00
|
|
|
logger.LogError(ex, "Could not apply migration '{Name}'", migrationRoutine.Name);
|
2020-03-08 07:02:42 -07:00
|
|
|
throw;
|
2020-03-05 08:21:27 -07:00
|
|
|
}
|
|
|
|
|
2020-03-08 07:02:42 -07:00
|
|
|
// Mark the migration as completed
|
2020-03-08 07:02:59 -07:00
|
|
|
logger.LogInformation("Migration '{Name}' applied successfully", migrationRoutine.Name);
|
2020-03-08 09:40:30 -07:00
|
|
|
migrationOptions.Applied.Add((migrationRoutine.Id, migrationRoutine.Name));
|
2020-03-05 10:37:49 -07:00
|
|
|
host.ServerConfigurationManager.SaveConfiguration(MigrationsListStore.StoreKey, migrationOptions);
|
2020-03-08 07:02:59 -07:00
|
|
|
logger.LogDebug("Migration '{Name}' marked as applied in configuration.", migrationRoutine.Name);
|
2020-03-05 10:09:33 -07:00
|
|
|
}
|
2020-03-05 08:21:27 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|