| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.IO; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Reflection; |
| | 6 | | using System.Threading; |
| | 7 | | using System.Threading.Tasks; |
| | 8 | | using Emby.Server.Implementations.Serialization; |
| | 9 | | using Jellyfin.Database.Implementations; |
| | 10 | | using Jellyfin.Server.Migrations.Stages; |
| | 11 | | using MediaBrowser.Common.Configuration; |
| | 12 | | using MediaBrowser.Model.Configuration; |
| | 13 | | using Microsoft.EntityFrameworkCore; |
| | 14 | | using Microsoft.EntityFrameworkCore.Infrastructure; |
| | 15 | | using Microsoft.EntityFrameworkCore.Migrations; |
| | 16 | | using Microsoft.Extensions.DependencyInjection; |
| | 17 | | using Microsoft.Extensions.Logging; |
| | 18 | |
|
| | 19 | | namespace Jellyfin.Server.Migrations; |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Handles Migration of the Jellyfin data structure. |
| | 23 | | /// </summary> |
| | 24 | | internal class JellyfinMigrationService |
| | 25 | | { |
| | 26 | | private readonly IDbContextFactory<JellyfinDbContext> _dbContextFactory; |
| | 27 | | private readonly ILoggerFactory _loggerFactory; |
| | 28 | |
|
| | 29 | | /// <summary> |
| | 30 | | /// Initializes a new instance of the <see cref="JellyfinMigrationService"/> class. |
| | 31 | | /// </summary> |
| | 32 | | /// <param name="dbContextFactory">Provides access to the jellyfin database.</param> |
| | 33 | | /// <param name="loggerFactory">The logger factory.</param> |
| | 34 | | public JellyfinMigrationService(IDbContextFactory<JellyfinDbContext> dbContextFactory, ILoggerFactory loggerFactory) |
| | 35 | | { |
| 63 | 36 | | _dbContextFactory = dbContextFactory; |
| 63 | 37 | | _loggerFactory = loggerFactory; |
| | 38 | | #pragma warning disable CS0618 // Type or member is obsolete |
| 63 | 39 | | Migrations = [.. typeof(IMigrationRoutine).Assembly.GetTypes().Where(e => typeof(IMigrationRoutine).IsAssignable |
| 63 | 40 | | .Select(e => (Type: e, Metadata: e.GetCustomAttribute<JellyfinMigrationAttribute>())) |
| 63 | 41 | | .Where(e => e.Metadata != null) |
| 63 | 42 | | .GroupBy(e => e.Metadata!.Stage) |
| 63 | 43 | | .Select(f => |
| 63 | 44 | | { |
| 63 | 45 | | var stage = new MigrationStage(f.Key); |
| 63 | 46 | | foreach (var item in f) |
| 63 | 47 | | { |
| 63 | 48 | | stage.Add(new(item.Type, item.Metadata!)); |
| 63 | 49 | | } |
| 63 | 50 | |
|
| 63 | 51 | | return stage; |
| 63 | 52 | | })]; |
| | 53 | | #pragma warning restore CS0618 // Type or member is obsolete |
| 63 | 54 | | } |
| | 55 | |
|
| | 56 | | private interface IInternalMigration |
| | 57 | | { |
| | 58 | | Task PerformAsync(ILogger logger); |
| | 59 | | } |
| | 60 | |
|
| | 61 | | private HashSet<MigrationStage> Migrations { get; set; } |
| | 62 | |
|
| | 63 | | public async Task CheckFirstTimeRunOrMigration(IApplicationPaths appPaths) |
| | 64 | | { |
| | 65 | | var logger = _loggerFactory.CreateLogger<JellyfinMigrationService>(); |
| | 66 | | logger.LogInformation("Initialise Migration service."); |
| | 67 | | var xmlSerializer = new MyXmlSerializer(); |
| | 68 | | var serverConfig = File.Exists(appPaths.SystemConfigurationFilePath) |
| | 69 | | ? (ServerConfiguration)xmlSerializer.DeserializeFromFile(typeof(ServerConfiguration), appPaths.SystemConfigu |
| | 70 | | : new ServerConfiguration(); |
| | 71 | | if (!serverConfig.IsStartupWizardCompleted) |
| | 72 | | { |
| | 73 | | logger.LogInformation("System initialisation detected. Seed data."); |
| | 74 | | var flatApplyMigrations = Migrations.SelectMany(e => e.Where(f => !f.Metadata.RunMigrationOnSetup)).ToArray( |
| | 75 | |
|
| | 76 | | var dbContext = await _dbContextFactory.CreateDbContextAsync().ConfigureAwait(false); |
| | 77 | | await using (dbContext.ConfigureAwait(false)) |
| | 78 | | { |
| | 79 | | var historyRepository = dbContext.GetService<IHistoryRepository>(); |
| | 80 | |
|
| | 81 | | await historyRepository.CreateIfNotExistsAsync().ConfigureAwait(false); |
| | 82 | | var appliedMigrations = await dbContext.Database.GetAppliedMigrationsAsync().ConfigureAwait(false); |
| | 83 | | var startupScripts = flatApplyMigrations |
| | 84 | | .Where(e => !appliedMigrations.Any(f => f != e.BuildCodeMigrationId())) |
| | 85 | | .Select(e => (Migration: e.Metadata, Script: historyRepository.GetInsertScript(new HistoryRow(e.Buil |
| | 86 | | .ToArray(); |
| | 87 | | foreach (var item in startupScripts) |
| | 88 | | { |
| | 89 | | logger.LogInformation("Seed migration {Key}-{Name}.", item.Migration.Key, item.Migration.Name); |
| | 90 | | await dbContext.Database.ExecuteSqlRawAsync(item.Script).ConfigureAwait(false); |
| | 91 | | } |
| | 92 | | } |
| | 93 | |
|
| | 94 | | logger.LogInformation("Migration system initialisation completed."); |
| | 95 | | } |
| | 96 | | else |
| | 97 | | { |
| | 98 | | // migrate any existing migration.xml files |
| | 99 | | var migrationConfigPath = Path.Join(appPaths.ConfigurationDirectoryPath, "migrations.xml"); |
| | 100 | | var migrationOptions = File.Exists(migrationConfigPath) |
| | 101 | | ? (MigrationOptions)xmlSerializer.DeserializeFromFile(typeof(MigrationOptions), migrationConfigPath)! |
| | 102 | | : null; |
| | 103 | | if (migrationOptions != null && migrationOptions.Applied.Count > 0) |
| | 104 | | { |
| | 105 | | logger.LogInformation("Old migration style migration.xml detected. Migrate now."); |
| | 106 | | var dbContext = await _dbContextFactory.CreateDbContextAsync().ConfigureAwait(false); |
| | 107 | | await using (dbContext.ConfigureAwait(false)) |
| | 108 | | { |
| | 109 | | var historyRepository = dbContext.GetService<IHistoryRepository>(); |
| | 110 | | var appliedMigrations = await dbContext.Database.GetAppliedMigrationsAsync().ConfigureAwait(false); |
| | 111 | | var oldMigrations = Migrations |
| | 112 | | .SelectMany(e => e.Where(e => e.Metadata.Key is not null)) // only consider migrations that have |
| | 113 | | .Where(e => migrationOptions.Applied.Any(f => f.Id.Equals(e.Metadata.Key!.Value))) |
| | 114 | | .Where(e => !appliedMigrations.Contains(e.BuildCodeMigrationId())) |
| | 115 | | .ToArray(); |
| | 116 | | var startupScripts = oldMigrations.Select(e => (Migration: e.Metadata, Script: historyRepository.Get |
| | 117 | | foreach (var item in startupScripts) |
| | 118 | | { |
| | 119 | | logger.LogInformation("Migrate migration {Key}-{Name}.", item.Migration.Key, item.Migration.Name |
| | 120 | | await dbContext.Database.ExecuteSqlRawAsync(item.Script).ConfigureAwait(false); |
| | 121 | | } |
| | 122 | |
|
| | 123 | | logger.LogInformation("Rename old migration.xml to migration.xml.backup"); |
| | 124 | | File.Move(migrationConfigPath, Path.ChangeExtension(migrationConfigPath, ".xml.backup"), true); |
| | 125 | | } |
| | 126 | | } |
| | 127 | | } |
| | 128 | | } |
| | 129 | |
|
| | 130 | | public async Task MigrateStepAsync(JellyfinMigrationStageTypes stage, IServiceProvider? serviceProvider) |
| | 131 | | { |
| | 132 | | var logger = _loggerFactory.CreateLogger<JellyfinMigrationService>(); |
| | 133 | | logger.LogInformation("Migrate stage {Stage}.", stage); |
| | 134 | | ICollection<CodeMigration> migrationStage = (Migrations.FirstOrDefault(e => e.Stage == stage) as ICollection<Cod |
| | 135 | |
|
| | 136 | | var dbContext = await _dbContextFactory.CreateDbContextAsync().ConfigureAwait(false); |
| | 137 | | await using (dbContext.ConfigureAwait(false)) |
| | 138 | | { |
| | 139 | | var historyRepository = dbContext.GetService<IHistoryRepository>(); |
| | 140 | | var migrationsAssembly = dbContext.GetService<IMigrationsAssembly>(); |
| | 141 | | var appliedMigrations = await historyRepository.GetAppliedMigrationsAsync().ConfigureAwait(false); |
| | 142 | | var pendingCodeMigrations = migrationStage |
| | 143 | | .Where(e => appliedMigrations.All(f => f.MigrationId != e.BuildCodeMigrationId())) |
| | 144 | | .Select(e => (Key: e.BuildCodeMigrationId(), Migration: new InternalCodeMigration(e, serviceProvider, db |
| | 145 | | .ToArray(); |
| | 146 | |
|
| | 147 | | (string Key, InternalDatabaseMigration Migration)[] pendingDatabaseMigrations = []; |
| | 148 | | if (stage is JellyfinMigrationStageTypes.CoreInitialisaition) |
| | 149 | | { |
| | 150 | | pendingDatabaseMigrations = migrationsAssembly.Migrations.Where(f => appliedMigrations.All(e => e.Migrat |
| | 151 | | .Select(e => (Key: e.Key, Migration: new InternalDatabaseMigration(e, dbContext))) |
| | 152 | | .ToArray(); |
| | 153 | | } |
| | 154 | |
|
| | 155 | | (string Key, IInternalMigration Migration)[] pendingMigrations = [.. pendingCodeMigrations, .. pendingDataba |
| | 156 | | logger.LogInformation("There are {Pending} migrations for stage {Stage}.", pendingCodeMigrations.Length, sta |
| | 157 | | var migrations = pendingMigrations.OrderBy(e => e.Key).ToArray(); |
| | 158 | | foreach (var item in migrations) |
| | 159 | | { |
| | 160 | | try |
| | 161 | | { |
| | 162 | | logger.LogInformation("Perform migration {Name}", item.Key); |
| | 163 | | await item.Migration.PerformAsync(_loggerFactory.CreateLogger(item.GetType().Name)).ConfigureAwait(f |
| | 164 | | logger.LogInformation("Migration {Name} was successfully applied", item.Key); |
| | 165 | | } |
| | 166 | | catch (Exception ex) |
| | 167 | | { |
| | 168 | | logger.LogCritical(ex, "Migration {Name} failed", item.Key); |
| | 169 | | throw; |
| | 170 | | } |
| | 171 | | } |
| | 172 | | } |
| | 173 | | } |
| | 174 | |
|
| | 175 | | private static string GetJellyfinVersion() |
| | 176 | | { |
| 546 | 177 | | return Assembly.GetEntryAssembly()!.GetName().Version!.ToString(); |
| | 178 | | } |
| | 179 | |
|
| | 180 | | private class InternalCodeMigration : IInternalMigration |
| | 181 | | { |
| | 182 | | private readonly CodeMigration _codeMigration; |
| | 183 | | private readonly IServiceProvider? _serviceProvider; |
| | 184 | | private JellyfinDbContext _dbContext; |
| | 185 | |
|
| | 186 | | public InternalCodeMigration(CodeMigration codeMigration, IServiceProvider? serviceProvider, JellyfinDbContext d |
| | 187 | | { |
| 105 | 188 | | _codeMigration = codeMigration; |
| 105 | 189 | | _serviceProvider = serviceProvider; |
| 105 | 190 | | _dbContext = dbContext; |
| 105 | 191 | | } |
| | 192 | |
|
| | 193 | | public async Task PerformAsync(ILogger logger) |
| | 194 | | { |
| | 195 | | await _codeMigration.Perform(_serviceProvider, CancellationToken.None).ConfigureAwait(false); |
| | 196 | |
|
| | 197 | | var historyRepository = _dbContext.GetService<IHistoryRepository>(); |
| | 198 | | var createScript = historyRepository.GetInsertScript(new HistoryRow(_codeMigration.BuildCodeMigrationId(), G |
| | 199 | | await _dbContext.Database.ExecuteSqlRawAsync(createScript).ConfigureAwait(false); |
| | 200 | | } |
| | 201 | | } |
| | 202 | |
|
| | 203 | | private class InternalDatabaseMigration : IInternalMigration |
| | 204 | | { |
| | 205 | | private readonly JellyfinDbContext _jellyfinDbContext; |
| | 206 | | private KeyValuePair<string, TypeInfo> _databaseMigrationInfo; |
| | 207 | |
|
| | 208 | | public InternalDatabaseMigration(KeyValuePair<string, TypeInfo> databaseMigrationInfo, JellyfinDbContext jellyfi |
| | 209 | | { |
| 651 | 210 | | _databaseMigrationInfo = databaseMigrationInfo; |
| 651 | 211 | | _jellyfinDbContext = jellyfinDbContext; |
| 651 | 212 | | } |
| | 213 | |
|
| | 214 | | public async Task PerformAsync(ILogger logger) |
| | 215 | | { |
| | 216 | | var migrator = _jellyfinDbContext.GetService<IMigrator>(); |
| | 217 | | await migrator.MigrateAsync(_databaseMigrationInfo.Key).ConfigureAwait(false); |
| | 218 | | } |
| | 219 | | } |
| | 220 | | } |