| | 1 | | using System; |
| | 2 | | using System.Globalization; |
| | 3 | | using System.Threading; |
| | 4 | | using System.Threading.Tasks; |
| | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | 6 | |
|
| | 7 | | namespace Jellyfin.Server.Migrations.Stages; |
| | 8 | |
|
| | 9 | | internal class CodeMigration(Type migrationType, JellyfinMigrationAttribute metadata) |
| | 10 | | { |
| | 11 | | public Type MigrationType { get; } = migrationType; |
| | 12 | |
|
| | 13 | | public JellyfinMigrationAttribute Metadata { get; } = metadata; |
| | 14 | |
|
| | 15 | | public string BuildCodeMigrationId() |
| | 16 | | { |
| 21378 | 17 | | return Metadata.Order.ToString("yyyyMMddHHmmsss", CultureInfo.InvariantCulture) + "_" + MigrationType.Name!; |
| | 18 | | } |
| | 19 | |
|
| | 20 | | public async Task Perform(IServiceProvider? serviceProvider, CancellationToken cancellationToken) |
| | 21 | | { |
| | 22 | | #pragma warning disable CS0618 // Type or member is obsolete |
| | 23 | | if (typeof(IMigrationRoutine).IsAssignableFrom(MigrationType)) |
| | 24 | | { |
| | 25 | | if (serviceProvider is null) |
| | 26 | | { |
| | 27 | | ((IMigrationRoutine)Activator.CreateInstance(MigrationType)!).Perform(); |
| | 28 | | } |
| | 29 | | else |
| | 30 | | { |
| | 31 | | ((IMigrationRoutine)ActivatorUtilities.CreateInstance(serviceProvider, MigrationType)).Perform(); |
| | 32 | | #pragma warning restore CS0618 // Type or member is obsolete |
| | 33 | | } |
| | 34 | | } |
| | 35 | | else if (typeof(IAsyncMigrationRoutine).IsAssignableFrom(MigrationType)) |
| | 36 | | { |
| | 37 | | if (serviceProvider is null) |
| | 38 | | { |
| | 39 | | await ((IAsyncMigrationRoutine)Activator.CreateInstance(MigrationType)!).PerformAsync(cancellationToken) |
| | 40 | | } |
| | 41 | | else |
| | 42 | | { |
| | 43 | | await ((IAsyncMigrationRoutine)ActivatorUtilities.CreateInstance(serviceProvider, MigrationType)).Perfor |
| | 44 | | } |
| | 45 | | } |
| | 46 | | else |
| | 47 | | { |
| | 48 | | throw new InvalidOperationException($"The type {MigrationType} does not implement either IMigrationRoutine o |
| | 49 | | } |
| | 50 | | } |
| | 51 | | } |