Summary: - Sources: Audit, Helpers, Migrations, Models, Protocols (+2 more) Stats: - 27 files changed, 303 insertions(+), 12 deletions(-)
45 lines
2.0 KiB
Swift
45 lines
2.0 KiB
Swift
import Foundation
|
|
|
|
/// Conditional migration that runs only when the app version is below a threshold.
|
|
public struct AppVersionConditionalMigration<Value: Codable & Sendable>: ConditionalMigration {
|
|
/// Destination key for the migration.
|
|
public let destinationKey: StorageKey<Value>
|
|
/// Minimum app version required to skip this migration.
|
|
public let minAppVersion: String
|
|
/// Migration to run when the version condition is met.
|
|
public let fallbackMigration: AnyStorageMigration
|
|
|
|
/// Creates a version-gated migration.
|
|
public init(
|
|
destinationKey: StorageKey<Value>,
|
|
minAppVersion: String,
|
|
fallbackMigration: AnyStorageMigration
|
|
) {
|
|
self.destinationKey = destinationKey
|
|
self.minAppVersion = minAppVersion
|
|
self.fallbackMigration = fallbackMigration
|
|
}
|
|
|
|
/// Determines whether the migration should run based on the app version.
|
|
///
|
|
/// - Parameters:
|
|
/// - router: The storage router used to query state.
|
|
/// - context: Migration context containing the app version.
|
|
/// - Returns: `true` when migration should proceed.
|
|
public func shouldMigrate(using router: StorageRouter, context: MigrationContext) async throws -> Bool {
|
|
let isEligible = context.appVersion.compare(minAppVersion, options: .numeric) == .orderedAscending
|
|
guard isEligible else { return false }
|
|
return try await router.shouldAllowMigration(for: destinationKey, context: context)
|
|
}
|
|
|
|
/// Executes the fallback migration when the version condition is met.
|
|
///
|
|
/// - Parameters:
|
|
/// - router: The storage router used to read and write values.
|
|
/// - context: Migration context for conditional checks.
|
|
/// - Returns: A ``MigrationResult`` describing success or failure.
|
|
public func migrate(using router: StorageRouter, context: MigrationContext) async throws -> MigrationResult {
|
|
try await fallbackMigration.migrate(using: router, context: context)
|
|
}
|
|
}
|