Files
workouts/Workouts/Schema/WorkoutsMigrationPlan.swift
2025-07-25 17:42:25 -04:00

30 lines
1.1 KiB
Swift

import SwiftData
struct WorkoutsMigrationPlan: SchemaMigrationPlan {
static var schemas: [VersionedSchema.Type] = [
SchemaV1.self,
SchemaV2.self
]
static var stages: [MigrationStage] = [
// Migration from V1 to V2: Add status field to WorkoutLog
MigrationStage.custom(
fromVersion: SchemaV1.self,
toVersion: SchemaV2.self,
willMigrate: { context in
// Get all WorkoutLog instances
let workoutLogs = try? context.fetch(FetchDescriptor<WorkoutLog>())
// Update each WorkoutLog with appropriate status based on completed flag
workoutLogs?.forEach { workoutLog in
// If completed is true, set status to .completed, otherwise set to .notStarted
workoutLog.status = workoutLog.completed ? WorkoutStatus.completed : WorkoutStatus.notStarted
}
},
didMigrate: { _ in
// No additional actions needed after migration
}
)
]
}