I've realized that I need to use migration plans, but those required versioned schemas. I think I've updated mine, but I wanted to confirm if this was the proper procedure. To start, none of my models were versioned. I've since wrapped them in a VersionedSchema like this:
enum TagV1: VersionedSchema {
static var versionIdentifier: Schema.Version = .init(1, 0, 0)
static var models: [any PersistentModel.Type] {
[Tag.self]
}
@Model
final class Tag {
var id = UUID()
var name: String = ""
// Relationships
var transactions: [Transaction]? = nil
init(name: String) {
self.name = name
}
}
}
I also created a type alias to point to this.
typealias Tag = TagV1.Tag
This is what my container looks like in my app file.
var sharedModelContainer: ModelContainer = {
let schema = Schema([
Tag.self
])
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
do {
return try ModelContainer(for: schema, configurations: [modelConfiguration])
} catch {
fatalError("Could not create ModelContainer: \(error)")
}
}()
The application builds and run successfully. Does this mean that my models are successfully versioned now? I'm trying to avoid an error I came across in earlier testing. That occurred because none of my models were versioned and I tried to setup a migration plan
Cannot use staged migration with an unknown coordinator model version.