model versions rollback,then how to migrate ??? #370

Open
opened 2025-12-29 18:26:11 +01:00 by adam · 5 comments
Owner

Originally created by @CoderCMY on GitHub (Jul 20, 2021).

For example,In TestFlight We publish a new app version and we update the "xcdatamodeld" model version that is “v10”, In AppStore the "xcdatamodeld" model version that is “v9”,when a user installed the testflight version first, then he installed the AppStore version again for some reason, as a result stored data in database is gone。So is there anyway to ensure CoreStore migrate succeed in this situation

Originally created by @CoderCMY on GitHub (Jul 20, 2021). For example,In TestFlight We publish a new app version and we update the "xcdatamodeld" model version that is “v10”, In AppStore the "xcdatamodeld" model version that is “v9”,when a user installed the testflight version first, then he installed the AppStore version again for some reason, as a result stored data in database is gone。So is there anyway to ensure CoreStore migrate succeed in this situation
adam added the question label 2025-12-29 18:26:11 +01:00
Author
Owner

@JohnEstropia commented on GitHub (Jul 20, 2021):

If you want to support this then you have to write a migration for V10V9. That's the only way

@JohnEstropia commented on GitHub (Jul 20, 2021): If you want to support this then you have to write a migration for `V10`→`V9`. That's the only way
Author
Owner

@CoderCMY commented on GitHub (Jul 20, 2021):

If you want to support this then you have to write a migration for V10V9. That's the only way

Under normal conditions,our CoreStore Initialization method is below,according to you advice,what's the right code

private func configCoreStore() {
    let dataStack = DataStack(xcodeModelName: "QMNovel", bundle: QMModuleCoreBundle.bundle, migrationChain: ["QMNovel", "QMNovelV2", "QMNovelV3", "QMNovelV4", "QMNovelV5", "QMNovelV6", "QMNovelV7", "QMNovelV8", "QMNovelV9", "QMNovelV10"])
    let systemDirectorySearchPath = FileManager.SearchPathDirectory.applicationSupportDirectory
    var defaultSystemDirectory = FileManager.default.urls(
        for: systemDirectorySearchPath,
        in: .userDomainMask).first!
    defaultSystemDirectory = defaultSystemDirectory.appendingPathComponent(
        Bundle.main.bundleIdentifier ?? "com.CoreStore.DataStack",
        isDirectory: true)
    let sqliteFileURL = defaultSystemDirectory.appendingPathComponent("QMNovel.sqlite")
    _ = dataStack.addStorage(
        SQLiteStore(
            fileURL: sqliteFileURL, // set the target file URL for the sqlite file
            localStorageOptions: .recreateStoreOnModelMismatch // if migration paths cannot be resolved, recreate the sqlite file
        ),
        completion: { [weak self] (result) -> Void in
            switch result {
            case .success(_):
		// 数据库初始化成功
            case .failure(let error):                    
                   // 数据库初始化失败
            }
        }
    )
    CoreStoreDefaults.dataStack = dataStack
}
@CoderCMY commented on GitHub (Jul 20, 2021): > If you want to support this then you have to write a migration for `V10`→`V9`. That's the only way Under normal conditions,our CoreStore Initialization method is below,according to you advice,what's the right code > private func configCoreStore() { let dataStack = DataStack(xcodeModelName: "QMNovel", bundle: QMModuleCoreBundle.bundle, migrationChain: ["QMNovel", "QMNovelV2", "QMNovelV3", "QMNovelV4", "QMNovelV5", "QMNovelV6", "QMNovelV7", "QMNovelV8", "QMNovelV9", "QMNovelV10"]) let systemDirectorySearchPath = FileManager.SearchPathDirectory.applicationSupportDirectory var defaultSystemDirectory = FileManager.default.urls( for: systemDirectorySearchPath, in: .userDomainMask).first! defaultSystemDirectory = defaultSystemDirectory.appendingPathComponent( Bundle.main.bundleIdentifier ?? "com.CoreStore.DataStack", isDirectory: true) let sqliteFileURL = defaultSystemDirectory.appendingPathComponent("QMNovel.sqlite") _ = dataStack.addStorage( SQLiteStore( fileURL: sqliteFileURL, // set the target file URL for the sqlite file localStorageOptions: .recreateStoreOnModelMismatch // if migration paths cannot be resolved, recreate the sqlite file ), completion: { [weak self] (result) -> Void in switch result { case .success(_): // 数据库初始化成功 case .failure(let error): // 数据库初始化失败 } } ) CoreStoreDefaults.dataStack = dataStack } >
Author
Owner

@JohnEstropia commented on GitHub (Jul 21, 2021):

You use recreateStoreOnModelMismatch, which means the database will be reset on a "downgrade".
If you want to keep data intact even if your testers downgrade your app version, you need to create an xcmappingmodel from v10 to v9, similar to how you'd prepare a migration from v9 to v10

@JohnEstropia commented on GitHub (Jul 21, 2021): You use `recreateStoreOnModelMismatch`, which means the database will be reset on a "downgrade". If you want to keep data intact even if your testers downgrade your app version, you need to create an xcmappingmodel from v10 to v9, similar to how you'd prepare a migration from v9 to v10
Author
Owner

@CoderCMY commented on GitHub (Jul 21, 2021):

You use recreateStoreOnModelMismatch, which means the database will be reset on a "downgrade".
If you want to keep data intact even if your testers downgrade your app version, you need to create an xcmappingmodel from v10 to v9, similar to how you'd prepare a migration from v9 to v10

Sorry,I guess you don’t understand my question completely,your solution is  create an xcmappingmodel from v10 to v9,In this way it is necessary I have to upload a new version to AppStore???my situation is like this,For example,we have published a version 5.0 the "xcdatamodeld" model version that is “v9” In AppStore ,A few days later,We publish a new version 5.1 and we update the "xcdatamodeld" model version that is “v10” In TestFlight 。when a tester installed the testflight version 5.1 and used some days, then he installed the AppStore version 5.0 again for some reason。 as a result stored data in database is gone,so my question is how to solve this problem .

@CoderCMY commented on GitHub (Jul 21, 2021): > You use `recreateStoreOnModelMismatch`, which means the database will be reset on a "downgrade". > If you want to keep data intact even if your testers downgrade your app version, you need to create an xcmappingmodel from v10 to v9, similar to how you'd prepare a migration from v9 to v10 Sorry,I guess you don’t understand my question completely,your solution is  create an xcmappingmodel from v10 to v9,In this way it is necessary I have to upload a new version to AppStore???my situation is like this,For example,we have published a version 5.0 the "xcdatamodeld" model version that is “v9” In AppStore ,A few days later,We publish a new version 5.1 and we update the "xcdatamodeld" model version that is “v10” In TestFlight 。when a tester installed the testflight version 5.1 and used some days, then he installed the AppStore version 5.0 again for some reason。 as a result stored data in database is gone,so my question is how to solve this problem .
Author
Owner

@JohnEstropia commented on GitHub (Sep 23, 2021):

It's been a while and I hope you had resolved this issue...

Sorry,I guess you don’t understand my question completely

Yes I did understand the situation, but my answer stands that the only way you can "downgrade" models is if you provide a mapping model from the newer version into the older version. This means you have to "mock" your older app version with a newer version that uses the old DB model but with newer mapping models.

@JohnEstropia commented on GitHub (Sep 23, 2021): It's been a while and I hope you had resolved this issue... > Sorry,I guess you don’t understand my question completely Yes I did understand the situation, but my answer stands that the only way you can "downgrade" models is if you provide a mapping model from the newer version into the older version. This means you have to "mock" your older app version with a newer version that uses the old DB model but with newer mapping models.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore-JohnEstropia#370