Skip to content

Commit

Permalink
Merge pull request #273 from Chm/master
Browse files Browse the repository at this point in the history
fixes for relationship migrations
  • Loading branch information
DanielBroad authored Mar 24, 2017
2 parents baa4998 + 1788148 commit df380d2
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions Incremental Store/EncryptedStore.m
Original file line number Diff line number Diff line change
Expand Up @@ -716,11 +716,12 @@ - (id)newValueForRelationship:(NSRelationshipDescription *)relationship

if (![relationship isToMany]) {
// to-one relationship, foreign key exists in source entity table

BOOL shouldFetchSourceEntityType = [self entityNeedsEntityTypeColumn:sourceEntity];

NSString *string = [NSString stringWithFormat:
@"SELECT %@%@ FROM %@ WHERE __objectID=?",
[self foreignKeyColumnForRelationship:relationship],
shouldFetchDestinationEntityType ? @", __entityType" : @"",
shouldFetchSourceEntityType ? @", __entityType" : @"",
[self tableNameForEntity:sourceEntity]];
statement = [self preparedStatementForQuery:string];
sqlite3_bind_int64(statement, 1, key);
Expand Down Expand Up @@ -1459,18 +1460,18 @@ - (BOOL)migrateFromModel:(NSManagedObjectModel *)fromModel toModel:(NSManagedObj
case NSAddEntityMappingType: {
NSString *destRootEntityName = [self rootForEntity:destinationEntity].name;
NSEntityDescription *destRootEntity = destinationEntities[destRootEntityName];
if ([updatedRootEntities containsObject:destRootEntity]) {
return;
}
[updatedRootEntities addObject:destRootEntity];
if (![updatedRootEntities containsObject:destRootEntity]) {
[updatedRootEntities addObject:destRootEntity];

NSString *srcRootEntityName = destRootEntity.name;
NSEntityDescription *srcRootEntity = [sourceEntities objectForKey:srcRootEntityName];
NSString *srcRootEntityName = destRootEntity.name;
NSEntityDescription *srcRootEntity = [sourceEntities objectForKey:srcRootEntityName];

success &= [self alterTableForSourceEntity:srcRootEntity
destinationEntity:destinationEntity
withMapping:nil
error:error];
}

success &= [self alterTableForSourceEntity:srcRootEntity
destinationEntity:destinationEntity
withMapping:nil
error:error];
[destinationEntity.directRelationshipsByName enumerateKeysAndObjectsUsingBlock:^(NSString * _Nonnull key, NSRelationshipDescription * _Nonnull obj, BOOL * _Nonnull relationshipStop) {
NSString *tableName = [self tableNameForRelationship:obj];
BOOL hasTable;
Expand All @@ -1496,15 +1497,15 @@ - (BOOL)migrateFromModel:(NSManagedObjectModel *)fromModel toModel:(NSManagedObj

NSString *destRootEntityName = srcRootEntity.name;
NSEntityDescription *destRootEntity = [destinationEntities objectForKey:destRootEntityName];
if ([updatedRootEntities containsObject:destRootEntity]) {
return;
if (![updatedRootEntities containsObject:destRootEntity]) {
[updatedRootEntities addObject:destRootEntity];

success &= [self alterTableForSourceEntity:srcRootEntity
destinationEntity:destRootEntity
withMapping:nil
error:error];
}
[updatedRootEntities addObject:destRootEntity];

success &= [self alterTableForSourceEntity:srcRootEntity
destinationEntity:destRootEntity
withMapping:nil
error:error];
// TODO: should we remove many-to-many relationship tables here?
} break;

Expand All @@ -1514,15 +1515,15 @@ - (BOOL)migrateFromModel:(NSManagedObjectModel *)fromModel toModel:(NSManagedObj
NSString *destRootEntityName = [self rootForEntity:destinationEntity].name;
NSEntityDescription *srcRootEntity = sourceEntities[srcRootEntityName];
NSEntityDescription *destRootEntity = destinationEntities[destRootEntityName];
if ([updatedRootEntities containsObject:destRootEntity]) {
return;
if (![updatedRootEntities containsObject:destRootEntity]) {
[updatedRootEntities addObject:destRootEntity];

success &= [self alterTableForSourceEntity:srcRootEntity
destinationEntity:destRootEntity
withMapping:nil
error:error];
}
[updatedRootEntities addObject:destRootEntity];

success &= [self alterTableForSourceEntity:srcRootEntity
destinationEntity:destRootEntity
withMapping:nil
error:error];
if (success) {
success &= [self alterRelationshipForSourceEntity:sourceEntity
destinationEntity:destinationEntity
Expand Down

0 comments on commit df380d2

Please sign in to comment.