mirror of
https://github.com/JohnEstropia/CoreStore.git
synced 2026-03-29 22:01:56 +02:00
fix migration for CoreStoreObject relationships
This commit is contained in:
@@ -352,7 +352,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
|
|||||||
)
|
)
|
||||||
func expression(forSource sourceEntity: NSEntityDescription) -> NSExpression {
|
func expression(forSource sourceEntity: NSEntityDescription) -> NSExpression {
|
||||||
|
|
||||||
return NSExpression(format: "FETCH(FUNCTION($\(NSMigrationManagerKey), \"fetchRequestForSourceEntityNamed:predicateString:\" , \"\(sourceEntity.name!)\", \"\(NSPredicate(value: true))\"), $\(NSMigrationManagerKey).\(#keyPath(NSMigrationManager.sourceContext)), \(false))")
|
return NSExpression(format: "FETCH(FUNCTION($\(NSMigrationManagerKey), \"fetchRequestForSourceEntityNamed:predicateString:\" , \"\(sourceEntity.name!)\", \"\(NSPredicate(value: true))\"), FUNCTION($\(NSMigrationManagerKey), \"\(#selector(getter: NSMigrationManager.sourceContext))\"), \(false))")
|
||||||
}
|
}
|
||||||
|
|
||||||
let sourceEntitiesByName = sourceModel.entitiesByName
|
let sourceEntitiesByName = sourceModel.entitiesByName
|
||||||
@@ -427,12 +427,11 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
|
|||||||
let destinationAttribute = destination.attribute
|
let destinationAttribute = destination.attribute
|
||||||
let propertyMapping = NSPropertyMapping()
|
let propertyMapping = NSPropertyMapping()
|
||||||
propertyMapping.name = destinationAttribute.name
|
propertyMapping.name = destinationAttribute.name
|
||||||
propertyMapping.valueExpression = NSExpression(format: "$\(NSMigrationSourceObjectKey).\(sourceAttribute.name)")
|
propertyMapping.valueExpression = NSExpression(format: "FUNCTION($\(NSMigrationSourceObjectKey), \"\(#selector(NSManagedObject.value(forKey:)))\", \"\(sourceAttribute.name)\")")
|
||||||
attributeMappings.append(propertyMapping)
|
attributeMappings.append(propertyMapping)
|
||||||
}
|
}
|
||||||
return attributeMappings
|
return attributeMappings
|
||||||
}
|
}
|
||||||
let entityMappingName = entityMapping.name!
|
|
||||||
entityMapping.relationshipMappings = autoreleasepool { () -> [NSPropertyMapping] in
|
entityMapping.relationshipMappings = autoreleasepool { () -> [NSPropertyMapping] in
|
||||||
|
|
||||||
let sourceRelationships = sourceEntity.cs_resolvedRelationshipRenamingIdentities()
|
let sourceRelationships = sourceEntity.cs_resolvedRelationshipRenamingIdentities()
|
||||||
@@ -442,9 +441,11 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
|
|||||||
|
|
||||||
let sourceRelationship = sourceRelationships[renamingIdentifier]!.relationship
|
let sourceRelationship = sourceRelationships[renamingIdentifier]!.relationship
|
||||||
let destinationRelationship = destination.relationship
|
let destinationRelationship = destination.relationship
|
||||||
|
let sourceRelationshipName = sourceRelationship.name
|
||||||
|
|
||||||
let propertyMapping = NSPropertyMapping()
|
let propertyMapping = NSPropertyMapping()
|
||||||
propertyMapping.name = destinationRelationship.name
|
propertyMapping.name = destinationRelationship.name
|
||||||
propertyMapping.valueExpression = NSExpression(format: "FUNCTION($\(NSMigrationManagerKey), \"\(#selector(NSMigrationManager.destinationInstances(forEntityMappingName:sourceInstances:)))\", \"\(entityMappingName)\", $\(NSMigrationSourceObjectKey).\(sourceRelationship.name))")
|
propertyMapping.valueExpression = NSExpression(format: "FUNCTION($\(NSMigrationManagerKey), \"destinationInstancesForSourceRelationshipNamed:sourceInstances:\", \"\(sourceRelationshipName)\", FUNCTION($\(NSMigrationSourceObjectKey), \"\(#selector(NSManagedObject.value(forKey:)))\", \"\(sourceRelationshipName)\"))")
|
||||||
relationshipMappings.append(propertyMapping)
|
relationshipMappings.append(propertyMapping)
|
||||||
}
|
}
|
||||||
return relationshipMappings
|
return relationshipMappings
|
||||||
@@ -485,23 +486,24 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
|
|||||||
}
|
}
|
||||||
userInfo[CustomEntityMigrationPolicy.UserInfoKey.sourceAttributesByDestinationKey] = sourceAttributesByDestinationKey
|
userInfo[CustomEntityMigrationPolicy.UserInfoKey.sourceAttributesByDestinationKey] = sourceAttributesByDestinationKey
|
||||||
}
|
}
|
||||||
let entityMappingName = entityMapping.name!
|
|
||||||
entityMapping.relationshipMappings = autoreleasepool { () -> [NSPropertyMapping] in
|
entityMapping.relationshipMappings = autoreleasepool { () -> [NSPropertyMapping] in
|
||||||
|
|
||||||
let sourceRelationships = sourceEntity.cs_resolvedRelationshipRenamingIdentities()
|
let sourceRelationships = sourceEntity.cs_resolvedRelationshipRenamingIdentities()
|
||||||
let destinationRelationships = destinationEntity.cs_resolvedRelationshipRenamingIdentities()
|
let destinationRelationships = destinationEntity.cs_resolvedRelationshipRenamingIdentities()
|
||||||
|
let transformedRenamingIdentifiers = Set(destinationRelationships.keys)
|
||||||
|
.intersection(sourceRelationships.keys)
|
||||||
|
|
||||||
var relationshipMappings: [NSPropertyMapping] = []
|
var relationshipMappings: [NSPropertyMapping] = []
|
||||||
for (renamingIdentifier, destination) in destinationRelationships {
|
for renamingIdentifier in transformedRenamingIdentifiers {
|
||||||
|
|
||||||
guard let sourceRelationship = sourceRelationships[renamingIdentifier]?.relationship else {
|
let sourceRelationship = sourceRelationships[renamingIdentifier]!.relationship
|
||||||
|
let destinationRelationship = destinationRelationships[renamingIdentifier]!.relationship
|
||||||
|
let sourceRelationshipName = sourceRelationship.name
|
||||||
|
let destinationRelationshipName = destinationRelationship.name
|
||||||
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
let destinationRelationship = destination.relationship
|
|
||||||
let propertyMapping = NSPropertyMapping()
|
let propertyMapping = NSPropertyMapping()
|
||||||
propertyMapping.name = destinationRelationship.name
|
propertyMapping.name = destinationRelationshipName
|
||||||
propertyMapping.valueExpression = NSExpression(format: "FUNCTION($\(NSMigrationManagerKey), \"\(#selector(NSMigrationManager.destinationInstances(forEntityMappingName:sourceInstances:)))\", \"\(entityMappingName)\", $\(NSMigrationSourceObjectKey).\(sourceRelationship.name))")
|
propertyMapping.valueExpression = NSExpression(format: "FUNCTION($\(NSMigrationManagerKey), \"destinationInstancesForSourceRelationshipNamed:sourceInstances:\", \"\(sourceRelationshipName)\", FUNCTION($\(NSMigrationSourceObjectKey), \"\(#selector(NSManagedObject.value(forKey:)))\", \"\(sourceRelationshipName)\"))")
|
||||||
relationshipMappings.append(propertyMapping)
|
relationshipMappings.append(propertyMapping)
|
||||||
}
|
}
|
||||||
return relationshipMappings
|
return relationshipMappings
|
||||||
@@ -553,11 +555,7 @@ public class CustomSchemaMappingProvider: Hashable, SchemaMappingProvider {
|
|||||||
)
|
)
|
||||||
if let dInstance = destinationObject?.rawObject {
|
if let dInstance = destinationObject?.rawObject {
|
||||||
|
|
||||||
manager.associate(
|
manager.associate(sourceInstance: sInstance, withDestinationInstance: dInstance, for: mapping)
|
||||||
sourceInstance: sInstance,
|
|
||||||
withDestinationInstance: dInstance,
|
|
||||||
for: mapping
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user