project-imas / encrypted-core-data

v2.0 - iOS Core Data encrypted SQLite store using SQLCipher
Other
784 stars 236 forks source link

Core Data migration of database at framework level. #238

Open ankitthakur opened 8 years ago

ankitthakur commented 8 years ago

We are having ECD DB at framework level, and at that level the DB migration is not working.

// run migrations
                NSDictionary *options = [self options];
                if ([[options objectForKey:NSMigratePersistentStoresAutomaticallyOption] boolValue] &&
                    [[options objectForKey:NSInferMappingModelAutomaticallyOption] boolValue]) {
                    NSManagedObjectModel *newModel = [[self persistentStoreCoordinator] managedObjectModel];

                    // check that a migration is required first:
                    if ([newModel isConfiguration:nil compatibleWithStoreMetadata:metadata]){
                        return YES;
                    }

                    // load the old model:
                    NSMutableArray *bundles = [NSMutableArray array];
                    [bundles addObject:[NSBundle mainBundle]];
                    NSManagedObjectModel *oldModel = [NSManagedObjectModel mergedModelFromBundles:bundles
                                                                                 forStoreMetadata:metadata];

[bundles addObjectsFromArray:[NSBundle allBundles]];

New implementation for this fix is:

// run migrations
                NSDictionary *options = [self options];
                if ([[options objectForKey:NSMigratePersistentStoresAutomaticallyOption] boolValue] &&
                    [[options objectForKey:NSInferMappingModelAutomaticallyOption] boolValue]) {
                    NSManagedObjectModel *newModel = [[self persistentStoreCoordinator] managedObjectModel];

                    // check that a migration is required first:
                    if ([newModel isConfiguration:nil compatibleWithStoreMetadata:metadata]){
                        return YES;
                    }

                    // load the old model:
                    NSMutableArray *bundles = [NSMutableArray array];
                    [bundles addObjectsFromArray:[NSBundle allBundles]];
                    NSManagedObjectModel *oldModel = [NSManagedObjectModel mergedModelFromBundles:bundles
                                                                                 forStoreMetadata:metadata];
ankitthakur commented 8 years ago

fixed in https://github.com/ankitthakur/encrypted-core-data/commit/fed4306b39dec2216cd54152cff9c0b4a0023f27

ankitthakur commented 7 years ago

The new fork is taken from master, and merged this fix in commit : 02eb4a645256a3bd1a08fc0ba7c0cfcd41600c85

lolgear commented 7 years ago

@ankitthakur check latest master, please. It could be solved by passing configuration with custom bundle.

chadbag commented 6 years ago

This is a good change. There is no downside as the -mergedModelFromBundles:forStoreMetadata: method call returns a model based on the metadata matches from the source bundles, so providing all the models in the app to match against, no matter the bundle, provides a more foolproof way.

ankitthakur commented 4 years ago

@lolgear I have tried that with latest app and latest version, but it is not working. So taking cherry pick - 02eb4a6