aboutsummaryrefslogtreecommitdiff
path: root/Jellyfin.Server/Migrations/MigrationRunner.cs
blob: ca4c79cfd3ad1c7f3cf8545da73ee79f1511ae39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using System;
using System.Linq;
using MediaBrowser.Common.Configuration;
using Microsoft.Extensions.Logging;

namespace Jellyfin.Server.Migrations
{
    /// <summary>
    /// The class that knows which migrations to apply and how to apply them.
    /// </summary>
    public sealed class MigrationRunner
    {
        /// <summary>
        /// The list of known migrations, in order of applicability.
        /// </summary>
        internal static readonly IUpdater[] Migrations =
        {
            new Routines.DisableTranscodingThrottling()
        };

        /// <summary>
        /// Run all needed migrations.
        /// </summary>
        /// <param name="host">CoreAppHost that hosts current version.</param>
        /// <param name="loggerFactory">Factory for making the logger.</param>
        public static void Run(CoreAppHost host, ILoggerFactory loggerFactory)
        {
            var logger = loggerFactory.CreateLogger<MigrationRunner>();
            var migrationOptions = ((IConfigurationManager)host.ServerConfigurationManager).GetConfiguration<MigrationOptions>(MigrationsListStore.StoreKey);

            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;
            }

            var applied = migrationOptions.Applied.ToList();

            for (var i = 0; i < Migrations.Length; i++)
            {
                var updater = Migrations[i];
                if (applied.Contains(updater.Name))
                {
                    logger.LogDebug("Skipping migration '{Name}' since it is already applied", updater.Name);
                    continue;
                }

                logger.LogInformation("Applying migration '{Name}'", updater.Name);
                try
                {
                    updater.Perform(host, logger);
                }
                catch (Exception ex)
                {
                    logger.LogError(ex, "Could not apply migration '{Name}'", updater.Name);
                    throw;
                }

                logger.LogInformation("Migration '{Name}' applied successfully", updater.Name);
                applied.Add(updater.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);
            }
        }
    }
}