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