From 42d1f41939bb9fae6db9148bbe814d6d0dec9a7d Mon Sep 17 00:00:00 2001 From: John Estropia Date: Fri, 11 Jan 2019 19:48:35 +0900 Subject: [PATCH] Provide more context when failing to search for mapping models #299 --- Sources/CSDataStack.swift | 2 +- Sources/DataStack.swift | 2 +- Sources/InferredSchemaMappingProvider.swift | 12 +++++++++--- Sources/XcodeDataModelSchema.swift | 2 +- 4 files changed, 12 insertions(+), 6 deletions(-) diff --git a/Sources/CSDataStack.swift b/Sources/CSDataStack.swift index 925dea5..a8a8f4d 100644 --- a/Sources/CSDataStack.swift +++ b/Sources/CSDataStack.swift @@ -50,7 +50,7 @@ public final class CSDataStack: NSObject, CoreStoreObjectiveCType { Initializes a `CSDataStack` from the model with the specified `modelName` in the specified `bundle`. - parameter xcodeModelName: the name of the (.xcdatamodeld) model file. If not specified, the application name (CFBundleName) will be used if it exists, or "CoreData" if it the bundle name was not set. - - parameter bundle: an optional bundle to load models from. If not specified, the main bundle will be used. + - parameter bundle: an optional bundle to load .xcdatamodeld models from. If not specified, the main bundle will be used. - parameter versionChain: the version strings that indicate the sequence of model versions to be used as the order for progressive migrations. If not specified, will default to a non-migrating data stack. */ @objc diff --git a/Sources/DataStack.swift b/Sources/DataStack.swift index 78f274d..6188dfe 100644 --- a/Sources/DataStack.swift +++ b/Sources/DataStack.swift @@ -43,7 +43,7 @@ public final class DataStack: Equatable { Convenience initializer for `DataStack` that creates a `SchemaHistory` from the model with the specified `modelName` in the specified `bundle`. - parameter xcodeModelName: the name of the (.xcdatamodeld) model file. If not specified, the application name (CFBundleName) will be used if it exists, or "CoreData" if it the bundle name was not set (e.g. in Unit Tests). - - parameter bundle: an optional bundle to load models from. If not specified, the main bundle will be used. + - parameter bundle: an optional bundle to load .xcdatamodeld models from. If not specified, the main bundle will be used. - parameter migrationChain: the `MigrationChain` that indicates the sequence of model versions to be used as the order for progressive migrations. If not specified, will default to a non-migrating data stack. */ public convenience init(xcodeModelName: XcodeDataModelFileName = DataStack.applicationName, bundle: Bundle = Bundle.main, migrationChain: MigrationChain = nil) { diff --git a/Sources/InferredSchemaMappingProvider.swift b/Sources/InferredSchemaMappingProvider.swift index 0fc6639..db14b59 100644 --- a/Sources/InferredSchemaMappingProvider.swift +++ b/Sources/InferredSchemaMappingProvider.swift @@ -31,6 +31,7 @@ import Foundation /** A `SchemaMappingProvider` that tries to infer model migration between two `DynamicSchema` versions by searching all `xcmappingmodel`s from `Bundle.allBundles` or by relying on lightweight migration if possible. Throws an error if lightweight migration is impossible for the two `DynamicSchema`. This mapping is automatically used as a fallback mapping provider, even if no mapping providers are explicitly declared in the `StorageInterface`. + - Note: For security reasons, `InferredSchemaMappingProvider` will not search `Bundle.allFrameworks` by default. If the `xcmappingmodel`s are bundled within a framework, use `XcodeSchemaMappingProvider` instead and provide `Bundle(for: ` to its initializer. */ public final class InferredSchemaMappingProvider: Hashable, SchemaMappingProvider { @@ -53,7 +54,7 @@ public final class InferredSchemaMappingProvider: Hashable, SchemaMappingProvide // MARK: SchemaMappingProvider public func cs_createMappingModel(from sourceSchema: DynamicSchema, to destinationSchema: DynamicSchema, storage: LocalStorage) throws -> (mappingModel: NSMappingModel, migrationType: MigrationType) { - + let sourceModel = sourceSchema.rawModel() let destinationModel = destinationSchema.rawModel() @@ -85,8 +86,13 @@ public final class InferredSchemaMappingProvider: Hashable, SchemaMappingProvide ) } catch { - - throw CoreStoreError(error) + + let coreStoreError = CoreStoreError(error) + CoreStore.log( + coreStoreError, + "\(cs_typeName(self)) failed to find migration mappings from version model \"\(sourceSchema.modelVersion)\" to \"\(destinationSchema.modelVersion)\" in the \(cs_typeName(storage)) at URL \"\(storage.fileURL)\". The local storage may be corrupt or the \(cs_typeName(storage)) initializer may be missing the correct \(cs_typeName(SchemaMappingProvider.self))" + ) + throw coreStoreError } } } diff --git a/Sources/XcodeDataModelSchema.swift b/Sources/XcodeDataModelSchema.swift index 2bcd90b..e17bbb7 100644 --- a/Sources/XcodeDataModelSchema.swift +++ b/Sources/XcodeDataModelSchema.swift @@ -42,7 +42,7 @@ public final class XcodeDataModelSchema: DynamicSchema { /** Creates a `XcodeDataModelSchema` for each of the models declared in the specified (.xcdatamodeld) model file. - parameter modelName: the name of the (.xcdatamodeld) model file. If not specified, the application name (CFBundleName) will be used if it exists, or "CoreData" if it the bundle name was not set. - - parameter bundle: an optional bundle to load models from. If not specified, the main bundle will be used. + - parameter bundle: an optional bundle to load .xcdatamodeld models from. If not specified, the main bundle will be used. - parameter migrationChain: the `MigrationChain` that indicates the sequence of model versions to be used as the order for progressive migrations. If not specified, will default to a non-migrating data stack. - returns: a tuple containing all `XcodeDataModelSchema` for the models declared in the specified .xcdatamodeld file, and the current model version string declared or inferred from the file. */