This commit is contained in:
2025-07-17 07:04:38 -04:00
parent 2d0e327334
commit f63bb0ba41
25 changed files with 592 additions and 92 deletions

View File

@ -92,8 +92,7 @@ struct DataLoader {
// 4. Load Exercises
let exerciseData = try loadJSON(forResource: "exercises", type: [ExerciseData].self)
for data in exerciseData {
let exercise = Exercise(name: data.name, setup: data.setup, descr: data.descr,
sets: data.sets, reps: data.reps, weight: data.weight)
let exercise = Exercise(name: data.name, descr: data.descr)
// Set exercise type
if let type = exerciseTypes[data.type] {

View File

@ -0,0 +1,16 @@
import SwiftData
enum SchemaV2: VersionedSchema {
static var versionIdentifier: Schema.Version = .init(1, 0, 1)
static var models: [any PersistentModel.Type] = [
Exercise.self,
ExerciseType.self,
Muscle.self,
MuscleGroup.self,
Split.self,
SplitExerciseAssignment.self,
Workout.self,
WorkoutLog.self
]
}

View File

@ -0,0 +1,16 @@
import SwiftData
enum SchemaV3: VersionedSchema {
static var versionIdentifier: Schema.Version = .init(1, 0, 2)
static var models: [any PersistentModel.Type] = [
Exercise.self,
ExerciseType.self,
Muscle.self,
MuscleGroup.self,
Split.self,
SplitExerciseAssignment.self,
Workout.self,
WorkoutLog.self
]
}

View File

@ -2,6 +2,8 @@ import SwiftData
enum SchemaVersion: Int {
case v1
case v2
case v3
static var current: SchemaVersion { .v1 }
static var current: SchemaVersion { .v3 }
}

View File

@ -8,9 +8,10 @@ final class WorkoutsContainer {
)
static func create() -> ModelContainer {
let schema = Schema(versionedSchema: SchemaV1.self)
// Using the current models directly without migration plan to avoid reference errors
let schema = Schema(SchemaV2.models)
let configuration = ModelConfiguration(cloudKitDatabase: .automatic)
let container = try! ModelContainer(for: schema, migrationPlan: WorkoutsMigrationPlan.self, configurations: [configuration])
let container = try! ModelContainer(for: schema, configurations: configuration)
return container
}
@ -19,7 +20,7 @@ final class WorkoutsContainer {
let configuration = ModelConfiguration(isStoredInMemoryOnly: true)
do {
let schema = Schema(SchemaV1.models)
let schema = Schema(SchemaV2.models)
let container = try ModelContainer(for: schema, configurations: configuration)
let context = ModelContext(container)

View File

@ -2,10 +2,28 @@ import SwiftData
struct WorkoutsMigrationPlan: SchemaMigrationPlan {
static var schemas: [VersionedSchema.Type] = [
SchemaV1.self
SchemaV1.self,
SchemaV2.self
]
static var stages: [MigrationStage] = [
// Add migration stages here in the future
// 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
}
)
]
}