LocalData/Sources/LocalData/Models/AnyStorageMigration.swift
Matt Bruce ac486a38bc Update Audit, Configuration, Migrations (+4 more)
Summary:
- Sources: Audit, Configuration, Migrations, Models, Protocols (+2 more)

Stats:
- 37 files changed, 256 insertions(+), 22 deletions(-)
2026-01-18 14:53:30 -06:00

32 lines
1.4 KiB
Swift

import Foundation
/// Type-erased wrapper for ``StorageMigration`` for use in catalogs and registrations.
public struct AnyStorageMigration: Sendable {
/// Descriptor for the migration destination key.
public let destinationDescriptor: StorageKeyDescriptor
private let shouldMigrateAction: @Sendable (StorageRouter, MigrationContext) async throws -> Bool
private let migrateAction: @Sendable (StorageRouter, MigrationContext) async throws -> MigrationResult
/// Creates a type-erased migration from a concrete migration.
public init<M: StorageMigration>(_ migration: M) {
self.destinationDescriptor = .from(migration.destinationKey)
self.shouldMigrateAction = { @Sendable router, context in
try await migration.shouldMigrate(using: router, context: context)
}
self.migrateAction = { @Sendable router, context in
try await migration.migrate(using: router, context: context)
}
}
/// Evaluates whether the migration should run for the given context.
public func shouldMigrate(using router: StorageRouter, context: MigrationContext) async throws -> Bool {
try await shouldMigrateAction(router, context)
}
/// Executes the migration and returns its result.
public func migrate(using router: StorageRouter, context: MigrationContext) async throws -> MigrationResult {
try await migrateAction(router, context)
}
}