SwiftData custom migration crash

I have an app that is in production that has a single entity called CDShift. This is the class:

@Model
final class CDShift {
    var identifier: UUID = UUID()
    var date: Date = Date()
    ...
}

This is how this model is written in the current version. Now, I’m updating the app and I have to do some modifications, that are:

  • add a new entity, called DayPlan
  • add the relationship between DayPlan and CDShift

What I did is this:

enum SchemaV1: VersionedSchema {

    static var versionIdentifier = Schema.Version(1, 0, 0)
    
    static var models: [any PersistentModel.Type] {
        [CDShift.self]
    }

    @Model
    final class CDShift {
        var identifier: UUID = UUID()
        var date: Date = Date()
    }
}

To encapsulate the current CDShift in a version 1 of the schema. Then I created the version 2:

enum SchemaV2: VersionedSchema {

    static var versionIdentifier = Schema.Version(2, 0, 0)
    
    static var models: [any PersistentModel.Type] {
        [CDShift.self, DayPlan.self]
    }

    @Model
    final class DayPlan {
        var identifier: UUID = UUID()
        var date: Date = Date()
        @Relationship(inverse: CDShift.dayPlan) var shifts: [CDShift]? = []
    }

    @Model
    final class CDShift {
        var identifier: UUID = UUID()
        var date: Date = Date()
        var dayPlan: DayPlan? = nil
    }
}

Finally, I created the migration plan:

enum MigrationPlan: SchemaMigrationPlan {
    static var schemas: [any VersionedSchema.Type] {
        [SchemaV1.self, SchemaV2.self]
    }
    
    static let migrateV1toV2 = MigrationStage.custom(
        fromVersion: SchemaV1.self,
        toVersion: SchemaV2.self) { context in
            // willMigrate, only access to old models
        } didMigrate: { context in
            // didMigrate, only access to new models
            
            let shifts = try context.fetch(FetchDescriptor<SchemaV2.CDShift>())
            
            for shift in shifts {
                let dayPlan = DayPlan(date: shift.date)
                dayPlan.shifts?.append(shift)
                context.insert(dayPlan)
            }
        }


    static var stages: [MigrationStage] {
        print("MigrationPlan | stages called")
        return [migrateV1toV2]
    }
}

Last, but not least, how the model container is created in the App:

struct MyApp: App {

    private let container: ModelContainer

    init() {
        container = ModelContainer.appContainer
    }
}

This is the extension of ModelContainer:

extension ModelContainer {
    
    static var appContainer: ModelContainer {
        let schema = Schema([
            CDShift.self,
            DayPlan.self
        ])
        let modelConfiguration = ModelConfiguration(
            schema: schema,
            isStoredInMemoryOnly: Ecosystem.current.isPreview,
            groupContainer: .identifier(Ecosystem.current.appGroupIdentifier)
        )
        
        AMLogger.verbose("Group container: (modelConfiguration.groupContainer)")
        AMLogger.verbose("Group container identifier: (String(describing: modelConfiguration.groupAppContainerIdentifier))")
        
        do {
//            let container = try ModelContainer(for: schema, configurations: modelConfiguration)
            let container = try ModelContainer(for: schema, migrationPlan: MigrationPlan.self, configurations: modelConfiguration)
            AMLogger.verbose("SwiftData path: (modelConfiguration.url.path)")
            return container
        } catch (let error) {
            fatalError("Could not create ModelContainer: (error)")
        }
    }
}

This has always worked perfectly until the migration. It crashes on the fatalError line, this is the error:

Unable to find a configuration named 'default' in the specified managed object model.

Another thing that is strange is that it seems that the version of the store is never updated to 2, but it keeps staying on 1. I tried also using the lightweight migration, no crash, it seems it recognizes the new entity, but the store version is always 1.

Do you have an idea on how to help in this case?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật