I am migrating my two SwiftData fashions to a V2 VersionedSchema
.
On this migration, I do the next modifications:
- Change two discipline varieties
- Change a
Relationship
deleteRule
- Add a discipline with a Codable enum for kind
Fashions
V1
@Mannequin
class DM1 {
var a: Date
var b: Date
var c: Int
@Relationship(deleteRule: .cascade) var d = [DM2]()
}
@Mannequin
class DM2 {
var e: String
var f: Date
var g: Int
var h: String
var i: Bool
}
V2
@Mannequin
class DM1 {
var a: Date
var b: Date
var c: TimeInterval? // modified
var j: EnumJ // added
@Relationship(deleteRule: .noAction) var d = [DM2]() // modified
}
@Mannequin
class DM2 {
var e: String
var f: Date
var g: TimeInterval // modified
var h: String
var i: Bool
}
Migrations
static let migrateV1toV2 = MigrationStage.customized(
fromVersion: SchemaV1.self,
toVersion: SchemaV2.self,
willMigrate: { context in
let previousDM1 = attempt context.fetch(FetchDescriptor<SchemaV1.DM1>())
let previousDM2 = attempt context.fetch(FetchDescriptor<SchemaV1.DM2>())
for dm2 in previousDm2 {
let newDm2 = SchemaV2.DM2(
e: dm2.e,
f: dm2.f,
g: TimeInterval(dm2.g),
h: dm2.h,
i: dm2.i
)
context.insert(newDm2)
}
for dm1 in previousDm1 {
let newDm1 = SchemaV2.DM1(
a: dm1.a,
b: dm1.b,
j: .aValue,
c: TimeInterval(dm1.c)
)
context.insert(newDm1)
}
attempt context.save()
}, didMigrate: nil)
EnumJ
enum EnumJ: Codable {
case aValue
// Just one case is right here for now
// This enum is for future options
}
Configuration
container = attempt ModelContainer(
for: DM1.self, DM2.self,
migrationPlan: MigrationPlan.self
)
Drawback
When operating the app on a simulator with the database V1, the app crashes with the next error:
Deadly error: Anticipated solely Arrays for Relationships - EnumJ
After clearing the app knowledge, eradicating the necessity for a migration, the app works high quality.
I simply cannot get the migration to work.