This commit is contained in:
2025-07-19 16:42:47 -04:00
parent 6e46775f58
commit e3c3f2c6f0
38 changed files with 556 additions and 367 deletions

View File

@ -1,24 +1,29 @@
import SwiftData
import Foundation
struct WorkoutsMigrationPlan: SchemaMigrationPlan {
static var schemas: [VersionedSchema.Type] = [
SchemaV1.self,
SchemaV2.self
]
static var schemas: [VersionedSchema.Type] = SchemaVersion.schemas
static var stages: [MigrationStage] = [
// Migration from V1 to V2: Add status field to WorkoutLog
MigrationStage.custom(
fromVersion: SchemaV1.self,
toVersion: SchemaV2.self,
toVersion: SchemaV1.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
print("migrating from v1 to v1")
let workouts = try? context.fetch(FetchDescriptor<Workout>())
workouts?.forEach { workout in
if let status = workout.status {
} else {
workout.status = .notStarted
}
// if let endDate = workout.end {
//
// } else {
// workout.end = Date()
// }
workout.end = Date()
}
},
didMigrate: { _ in