| | | 1 | | using System; |
| | | 2 | | using System.Globalization; |
| | | 3 | | using System.Threading; |
| | | 4 | | using System.Threading.Tasks; |
| | | 5 | | using Jellyfin.Server.ServerSetupApp; |
| | | 6 | | using Microsoft.Extensions.DependencyInjection; |
| | | 7 | | using Microsoft.Extensions.DependencyInjection.Extensions; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | |
| | | 10 | | namespace Jellyfin.Server.Migrations.Stages; |
| | | 11 | | |
| | | 12 | | internal class CodeMigration(Type migrationType, JellyfinMigrationAttribute metadata, JellyfinMigrationBackupAttribute? |
| | | 13 | | { |
| | | 14 | | public Type MigrationType { get; } = migrationType; |
| | | 15 | | |
| | | 16 | | public JellyfinMigrationAttribute Metadata { get; } = metadata; |
| | | 17 | | |
| | | 18 | | public JellyfinMigrationBackupAttribute? BackupRequirements { get; set; } = migrationBackupAttribute; |
| | | 19 | | |
| | | 20 | | public string BuildCodeMigrationId() |
| | | 21 | | { |
| | 28518 | 22 | | return Metadata.Order.ToString("yyyyMMddHHmmsss", CultureInfo.InvariantCulture) + "_" + Metadata.Name!; |
| | | 23 | | } |
| | | 24 | | |
| | | 25 | | private IServiceCollection MigrationServices(IServiceProvider serviceProvider, IStartupLogger logger) |
| | | 26 | | { |
| | 105 | 27 | | var childServiceCollection = new ServiceCollection() |
| | 105 | 28 | | .AddSingleton(serviceProvider) |
| | 105 | 29 | | .AddSingleton(logger) |
| | 105 | 30 | | .AddSingleton(typeof(IStartupLogger<>), typeof(NestedStartupLogger<>)) |
| | 105 | 31 | | .AddSingleton<StartupLogTopic>(logger.Topic!); |
| | | 32 | | |
| | 119070 | 33 | | foreach (ServiceDescriptor service in serviceProvider.GetRequiredService<IServiceCollection>()) |
| | | 34 | | { |
| | 59430 | 35 | | if (service.Lifetime == ServiceLifetime.Singleton && !service.ServiceType.IsGenericTypeDefinition) |
| | | 36 | | { |
| | 37275 | 37 | | childServiceCollection.AddSingleton(service.ServiceType, _ => serviceProvider.GetService(service.Service |
| | 37275 | 38 | | continue; |
| | | 39 | | } |
| | | 40 | | |
| | 22155 | 41 | | childServiceCollection.Add(service); |
| | | 42 | | } |
| | | 43 | | |
| | 105 | 44 | | return childServiceCollection; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | public async Task Perform(IServiceProvider? serviceProvider, IStartupLogger logger, CancellationToken cancellationTo |
| | | 48 | | { |
| | | 49 | | #pragma warning disable CS0618 // Type or member is obsolete |
| | | 50 | | if (typeof(IMigrationRoutine).IsAssignableFrom(MigrationType)) |
| | | 51 | | { |
| | | 52 | | if (serviceProvider is null) |
| | | 53 | | { |
| | | 54 | | ((IMigrationRoutine)Activator.CreateInstance(MigrationType)!).Perform(); |
| | | 55 | | } |
| | | 56 | | else |
| | | 57 | | { |
| | | 58 | | using var migrationServices = MigrationServices(serviceProvider, logger).BuildServiceProvider(); |
| | | 59 | | ((IMigrationRoutine)ActivatorUtilities.CreateInstance(migrationServices, MigrationType)).Perform(); |
| | | 60 | | #pragma warning restore CS0618 // Type or member is obsolete |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | else if (typeof(IAsyncMigrationRoutine).IsAssignableFrom(MigrationType)) |
| | | 64 | | { |
| | | 65 | | if (serviceProvider is null) |
| | | 66 | | { |
| | | 67 | | await ((IAsyncMigrationRoutine)Activator.CreateInstance(MigrationType)!).PerformAsync(cancellationToken) |
| | | 68 | | } |
| | | 69 | | else |
| | | 70 | | { |
| | | 71 | | using var migrationServices = MigrationServices(serviceProvider, logger).BuildServiceProvider(); |
| | | 72 | | await ((IAsyncMigrationRoutine)ActivatorUtilities.CreateInstance(migrationServices, MigrationType)).Perf |
| | | 73 | | } |
| | | 74 | | } |
| | | 75 | | else |
| | | 76 | | { |
| | | 77 | | throw new InvalidOperationException($"The type {MigrationType} does not implement either IMigrationRoutine o |
| | | 78 | | } |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | private class NestedStartupLogger<TCategory> : StartupLogger<TCategory> |
| | | 82 | | { |
| | 0 | 83 | | public NestedStartupLogger(ILogger logger, StartupLogTopic topic) : base(logger, topic) |
| | | 84 | | { |
| | 0 | 85 | | } |
| | | 86 | | } |
| | | 87 | | } |