| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.ExternalAuthentication.Contracts; |
| | | 3 | | using Elsa.ExternalAuthentication.Models; |
| | | 4 | | |
| | | 5 | | namespace Elsa.ExternalAuthentication.Services; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Orchestrates adapter-owned, forward-only settings migrations while preserving |
| | | 9 | | /// the protocol-neutral connection envelope. |
| | | 10 | | /// </summary> |
| | 2 | 11 | | public sealed class AdapterSettingsMigrationService( |
| | 2 | 12 | | IExternalAuthenticationAdapterRegistry adapters, |
| | 2 | 13 | | IEnumerable<IAdapterSettingsMigration> migrations) : IAdapterSettingsMigrationService |
| | | 14 | | { |
| | 2 | 15 | | private readonly IReadOnlyDictionary<(string AdapterType, int FromVersion), IAdapterSettingsMigration> _migrations = |
| | 2 | 16 | | BuildMigrationIndex(migrations); |
| | | 17 | | |
| | | 18 | | public async ValueTask<AdapterSettingsMigrationResult> MigrateAsync( |
| | | 19 | | string adapterType, |
| | | 20 | | int settingsVersion, |
| | | 21 | | JsonElement settings, |
| | | 22 | | CancellationToken cancellationToken = default) |
| | | 23 | | { |
| | 5 | 24 | | cancellationToken.ThrowIfCancellationRequested(); |
| | 5 | 25 | | if (!adapters.TryGet(adapterType, out var adapter)) |
| | 1 | 26 | | throw new InvalidOperationException($"The adapter type '{adapterType}' is not installed or deployment-allowe |
| | | 27 | | |
| | 4 | 28 | | var currentVersion = adapter.Describe().SettingsVersion; |
| | 4 | 29 | | if (settingsVersion <= 0 || settingsVersion > currentVersion) |
| | 1 | 30 | | throw new InvalidOperationException($"Settings version {settingsVersion} is not compatible with adapter '{ad |
| | 3 | 31 | | if (settingsVersion == currentVersion) |
| | 1 | 32 | | return new AdapterSettingsMigrationResult(currentVersion, settings.Clone(), false); |
| | | 33 | | |
| | 2 | 34 | | var migrated = settings.Clone(); |
| | 2 | 35 | | var version = settingsVersion; |
| | 2 | 36 | | var stepCount = 0; |
| | 5 | 37 | | while (version < currentVersion) |
| | | 38 | | { |
| | 3 | 39 | | if (!_migrations.TryGetValue((adapterType, version), out var migration)) |
| | 0 | 40 | | throw new InvalidOperationException($"Adapter '{adapterType}' does not provide a settings migration from |
| | 3 | 41 | | if (migration.ToVersion <= version || migration.ToVersion > currentVersion) |
| | 0 | 42 | | throw new InvalidOperationException($"Adapter '{adapterType}' has an invalid settings migration from ver |
| | 3 | 43 | | if (++stepCount > 64) |
| | 0 | 44 | | throw new InvalidOperationException($"Adapter '{adapterType}' settings migration contains a cycle."); |
| | | 45 | | |
| | 3 | 46 | | migrated = (await migration.MigrateAsync(migrated, cancellationToken)).Clone(); |
| | 3 | 47 | | version = migration.ToVersion; |
| | 3 | 48 | | } |
| | | 49 | | |
| | 2 | 50 | | return new AdapterSettingsMigrationResult(version, migrated, true); |
| | 3 | 51 | | } |
| | | 52 | | |
| | | 53 | | private static IReadOnlyDictionary<(string AdapterType, int FromVersion), IAdapterSettingsMigration> BuildMigrationI |
| | | 54 | | IEnumerable<IAdapterSettingsMigration> migrations) |
| | | 55 | | { |
| | 2 | 56 | | var result = new Dictionary<(string AdapterType, int FromVersion), IAdapterSettingsMigration>(); |
| | 10 | 57 | | foreach (var migration in migrations) |
| | | 58 | | { |
| | 3 | 59 | | if (string.IsNullOrWhiteSpace(migration.AdapterType) || migration.FromVersion <= 0) |
| | 0 | 60 | | throw new InvalidOperationException("Adapter settings migrations must define an adapter type and a posit |
| | 3 | 61 | | if (!result.TryAdd((migration.AdapterType, migration.FromVersion), migration)) |
| | 0 | 62 | | throw new InvalidOperationException($"Adapter '{migration.AdapterType}' registers more than one migratio |
| | | 63 | | } |
| | 2 | 64 | | return result; |
| | | 65 | | } |
| | | 66 | | } |