arkavo-org / arkavo-org/app

Add schema versioning and migration strategy for SwiftData

Open
#152 0 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
Swift
Stars
0
Forks
0
Avg merge
1h 41m
Merged PRs (30d)
1

Description

### Problem
Current SwiftData schema has NO versioning or migration strategy:
- Schema changes break existing user data
- No way to migrate from v1 → v2 safely
- Database corruption after app updates
- No rollback capability

### Proposed Solution

#### 1. Define Schema Versions
```swift
enum SchemaVersion: Int, CaseIterable {
case v1 = 1 // Initial release
case v2 = 2 // Fix Stream.source relationship with .nullify delete rule
case v3 = 3 // Add audit timestamps (createdAt, updatedAt)

static var current: SchemaVersion { .v3 }

var schema: Schema {
switch self {
case .v1: return SchemaV1.schema
case .v2: return SchemaV2.schema
case .v3: return SchemaV3.schema
}
}
}
```

#### 2. Migration Plan
```swift
enum SchemaMigrationPlan: SchemaMigrationPlan {
static var schemas: [VersionedSchema.Type] {
[SchemaV1.self, SchemaV2.self, SchemaV3.self]
}

static var stages: [MigrationStage] {
[migrateV1toV2, migrateV2toV3]
}

static let migrateV1toV2 = MigrationStage.lightweight(
fromVersion: SchemaV1.self,
toVersion: SchemaV2.self
)

static let migrateV2toV3 = MigrationStage.custom(
fromVersion: SchemaV2.self,
toVersion: SchemaV3.self,
willMigrate: { context in
let streams = try context.fetch(FetchDescriptor())
let now = Date()
for stream in streams {
stream.createdAt = now
stream.updatedAt = now
}
try context.save()
},
didMigrate: nil
)
}
```

#### 3. Update PersistenceController
```swift
container = try ModelContainer(
for: currentSchema,
migrationPlan: SchemaMigrationPlan.self,
configurations: [modelConfiguration]
)
```

### Benefits
- ✅ Safe schema evolution
- ✅ Automatic data migration
- ✅ Versioned schemas for rollback
- ✅ Custom migration logic support
- ✅ Audit trail capabilities

### Implementation Checklist
- [ ] Create `SchemaVersion.swift`
- [ ] Create `SchemaV1.swift` (current production)
- [ ] Create `SchemaV2.swift` (with relationship fixes)
- [ ] Create `SchemaV3.swift` (with timestamps)
- [ ] Create `SchemaMigrationPlan.swift`
- [ ] Update `PersistenceController`
- [ ] Add migration tests
- [ ] Document migration process

### Files to Create
- `SchemaVersion.swift`
- `SchemaV1.swift`
- `SchemaV2.swift`
- `SchemaV3.swift`
- `SchemaMigrationPlan.swift`

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with PersistenceController and the existing SwiftData model definitions, then review the proposed SchemaVersion.swift, SchemaV1.swift, SchemaV2.swift, SchemaV3.swift, and SchemaMigrationPlan.swift files. Add migration tests and document the migration process; done means versioned schemas, the listed migration stages, updated container setup, and safe handling of existing data.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
databases, mobile
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.