Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve custom schema mapping provider #308

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CoreStore.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,9 @@
B5FEC18F1C9166E600532541 /* NSPersistentStore+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5FEC18D1C9166E200532541 /* NSPersistentStore+Setup.swift */; };
B5FEC1901C9166E700532541 /* NSPersistentStore+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5FEC18D1C9166E200532541 /* NSPersistentStore+Setup.swift */; };
B5FEC1911C9166E700532541 /* NSPersistentStore+Setup.swift in Sources */ = {isa = PBXBuildFile; fileRef = B5FEC18D1C9166E200532541 /* NSPersistentStore+Setup.swift */; };
E6B8DCE522229E340007BBA9 /* MigrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6B8DCE422229E330007BBA9 /* MigrationTests.swift */; };
E6B8DCE622229E340007BBA9 /* MigrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6B8DCE422229E330007BBA9 /* MigrationTests.swift */; };
E6B8DCE722229E340007BBA9 /* MigrationTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E6B8DCE422229E330007BBA9 /* MigrationTests.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -968,6 +971,7 @@
B5FE4DA61C84FB4400FA6A91 /* InMemoryStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = InMemoryStore.swift; sourceTree = "<group>"; };
B5FE4DAB1C85D44E00FA6A91 /* SQLiteStore.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SQLiteStore.swift; sourceTree = "<group>"; };
B5FEC18D1C9166E200532541 /* NSPersistentStore+Setup.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "NSPersistentStore+Setup.swift"; sourceTree = "<group>"; };
E6B8DCE422229E330007BBA9 /* MigrationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MigrationTests.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -1101,6 +1105,7 @@
B5220E0B1D0D0D19009BC71E /* ImportTests.swift */,
B525576B1CFAF18F00E51965 /* IntoTests.swift */,
B5220E0F1D0DA6AB009BC71E /* ListObserverTests.swift */,
E6B8DCE422229E330007BBA9 /* MigrationTests.swift */,
B5DC47C51C93D22900FA3BF3 /* MigrationChainTests.swift */,
B5220E071D0C5F8D009BC71E /* ObjectObserverTests.swift */,
B52557771D02826E00E51965 /* OrderByTests.swift */,
Expand Down Expand Up @@ -2039,6 +2044,7 @@
B5220E0C1D0D0D19009BC71E /* ImportTests.swift in Sources */,
B5D339B41E925C2B00C880DE /* DynamicModelTests.swift in Sources */,
B5D372841A39CD6900F583D9 /* Model.xcdatamodeld in Sources */,
E6B8DCE522229E340007BBA9 /* MigrationTests.swift in Sources */,
B52557881D02DE8100E51965 /* FetchTests.swift in Sources */,
B5489F501CF603D5008B4978 /* FromTests.swift in Sources */,
B52557781D02826E00E51965 /* OrderByTests.swift in Sources */,
Expand Down Expand Up @@ -2234,6 +2240,7 @@
B5220E0D1D0D0D19009BC71E /* ImportTests.swift in Sources */,
B5D339B51E925C2B00C880DE /* DynamicModelTests.swift in Sources */,
B525576D1CFAF18F00E51965 /* IntoTests.swift in Sources */,
E6B8DCE622229E340007BBA9 /* MigrationTests.swift in Sources */,
B580857B1CDF808D004C2EEB /* SetupTests.swift in Sources */,
B52557891D02DE8100E51965 /* FetchTests.swift in Sources */,
B5489F511CF603D5008B4978 /* FromTests.swift in Sources */,
Expand Down Expand Up @@ -2429,6 +2436,7 @@
B525576E1CFAF18F00E51965 /* IntoTests.swift in Sources */,
B5D339B61E925C2B00C880DE /* DynamicModelTests.swift in Sources */,
B580857C1CDF808F004C2EEB /* SetupTests.swift in Sources */,
E6B8DCE722229E340007BBA9 /* MigrationTests.swift in Sources */,
B525578A1D02DE8100E51965 /* FetchTests.swift in Sources */,
B5220E281D1308E5009BC71E /* SectionByTests.swift in Sources */,
B5489F521CF603D5008B4978 /* FromTests.swift in Sources */,
Expand Down
235 changes: 235 additions & 0 deletions CoreStoreTests/MigrationTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
//
// MigrationChainTests.swift
// CoreStore
//
// Copyright © 2018 John Rommel Estropia
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//

import XCTest

@testable
import CoreStore


// MARK: - MigrationTests

final class MigrationTests: BaseTestCase {

func test_ThatEntityDescriptionExtension_CanMapAttributes() {

// Should match attributes by renaming identifier.
do {
let src = NSEntityDescription([NSAttributeDescription("foo")])
let dst = NSEntityDescription([NSAttributeDescription("bar", renamingIdentifier: "foo")])

var map: [NSAttributeDescription: NSAttributeDescription] = [:]
XCTAssertNoThrow(map = try dst.mapAttributes(in: src))
XCTAssertEqual(map.count, 1)
XCTAssertEqual(map.keys.first?.renamingIdentifier, "foo")
XCTAssertEqual(map.values.first?.name, "foo")
}

// Should match attributes by name when matching by renaming identifier fails.
do {
let src = NSEntityDescription([NSAttributeDescription("bar")])
let dst = NSEntityDescription([NSAttributeDescription("bar", renamingIdentifier: "foo")])

var map: [NSAttributeDescription: NSAttributeDescription] = [:]
XCTAssertNoThrow(map = try dst.mapAttributes(in: src))
XCTAssertEqual(map.count, 1)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In my understanding, in this case mapAttributes() should throw. The new dst.bar should be completely unrelated to src.bar (dst.bar should be linked to a src.foo)

XCTAssertEqual(map.keys.first?.renamingIdentifier, "foo")
XCTAssertEqual(map.keys.first?.name, "bar")
XCTAssertEqual(map.values.first?.name, "bar")
}

// Should not throw exception when optional attributes cannot be matched.
do {
let src = NSEntityDescription([NSAttributeDescription("foo")])
let dst = NSEntityDescription([NSAttributeDescription("bar")])

var map: [NSAttributeDescription: NSAttributeDescription] = [:]
XCTAssertNoThrow(map = try dst.mapAttributes(in: src))
XCTAssertEqual(map.count, 0)
}

// Should not throw exception when required attributes with default value cannot be matched.
do {
let src = NSEntityDescription([NSAttributeDescription("foo")])
let dst = NSEntityDescription([NSAttributeDescription("bar", optional: false, defaultValue: "baz")])

var map: [NSAttributeDescription: NSAttributeDescription] = [:]
XCTAssertNoThrow(map = try dst.mapAttributes(in: src))
XCTAssertEqual(map.count, 0)
}

// Should throw exception when required attributes without default value cannot be matched.
do {
let src = NSEntityDescription([NSAttributeDescription("foo")])
let dst = NSEntityDescription([NSAttributeDescription("bar", optional: false)])
XCTAssertThrowsError(try dst.mapAttributes(in: src))
}
}

func test_ThatCustomSchemaMappingProvider_CanDeleteAndInsertEntitiesWithCustomEntityMapping() {
class Foo: CoreStoreObject {
var name = Value.Optional<String>("name")
}

class Bar: CoreStoreObject {
var nickname = Value.Optional<String>("nickname", renamingIdentifier: "name")
}

let src: CoreStoreSchema = CoreStoreSchema(modelVersion: "1", entities: [Entity<Foo>("Foo")])
let dst: CoreStoreSchema = CoreStoreSchema(modelVersion: "2", entities: [Entity<Bar>("Bar")])

let migration: CustomSchemaMappingProvider = CustomSchemaMappingProvider(from: "1", to: "2", entityMappings: [
.deleteEntity(sourceEntity: "Foo"),
.insertEntity(destinationEntity: "Bar")
])

/// Create the source store and data set.
withExtendedLifetime(DataStack(src), { stack in
try! stack.addStorageAndWait(SQLiteStore())
try! stack.perform(synchronous: { $0.create(Into<Foo>()).name.value = "Willy" })
})

let expectation: XCTestExpectation = self.expectation(description: "migration-did-complete")

withExtendedLifetime(DataStack(src, dst, migrationChain: ["1", "2"]), { stack in
_ = stack.addStorage(SQLiteStore(fileURL: SQLiteStore.defaultFileURL, migrationMappingProviders: [migration]), completion: {
switch $0 {
case .success(_):
XCTAssertEqual(stack.modelSchema.rawModel().entities.count, 1)
try! stack.perform(synchronous: { $0.create(Into<Bar>()).nickname.value = "Bobby" })
case .failure(let error):
XCTFail("\(error)")
}
expectation.fulfill()
})
})

self.waitAndCheckExpectations()
}

func test_ThatCustomSchemaMappingProvider_CanCopyEntityWithCustomEntityMapping() {
class Foo: CoreStoreObject {
var name = Value.Required<String>("name", initial: "")
}

// Todo: The way this handles different version locks is flaky… It fails face on the ground in debug, but seems
// todo: to work fine in production, yet it's not clear if it transforms everything as expected.

let src: CoreStoreSchema = CoreStoreSchema(modelVersion: "1", entities: [Entity<Foo>("Foo")])
let dst: CoreStoreSchema = CoreStoreSchema(modelVersion: "2", entities: [Entity<Foo>("Foo")])

XCTAssertEqual(dst.rawModel().entities.first!.versionHash, src.rawModel().entities.first!.versionHash)

let migration: CustomSchemaMappingProvider = CustomSchemaMappingProvider(from: "1", to: "2", entityMappings: [
.copyEntity(sourceEntity: "Foo", destinationEntity: "Foo")
])

/// Create the source store and data set.
withExtendedLifetime(DataStack(src), { stack in
try! stack.addStorageAndWait(SQLiteStore())
try! stack.perform(synchronous: { $0.create(Into<Foo>()).name.value = "Willy" })
})

let expectation: XCTestExpectation = self.expectation(description: "migration-did-complete")

withExtendedLifetime(DataStack(src, dst, migrationChain: ["1", "2"]), { stack in
_ = stack.addStorage(SQLiteStore(fileURL: SQLiteStore.defaultFileURL, migrationMappingProviders: [migration]), completion: {
switch $0 {
case .success(_):
XCTAssertEqual(stack.modelSchema.rawModel().entities.count, 1)
XCTAssertEqual(try! stack.fetchCount(From<Foo>()), 1)
try! stack.perform(synchronous: { $0.create(Into<Foo>()).name.value = "Bobby" })
case .failure(let error):
XCTFail("\(error)")
}
expectation.fulfill()
})
})

self.waitAndCheckExpectations()
}

func test_ThatCustomSchemaMappingProvider_CanTransformEntityWithCustomEntityMapping() {
class Foo: CoreStoreObject {
var name = Value.Required<String>("name", initial: "")
var futile = Value.Required<String>("futile", initial: "")
}

class Bar: CoreStoreObject {
var firstName = Value.Required<String>("firstName", initial: "", renamingIdentifier: "name")
var lastName = Value.Required<String>("lastName", initial: "", renamingIdentifier: "placeholder")
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should never migrate successfully as there is no "placeholder" key in the source entity.

var age = Value.Required<Int>("age", initial: 18)
var gender = Value.Optional<String>("gender")
}

let src: CoreStoreSchema = CoreStoreSchema(modelVersion: "1", entities: [Entity<Foo>("Foo")])
let dst: CoreStoreSchema = CoreStoreSchema(modelVersion: "2", entities: [Entity<Bar>("Bar")])

let migration: CustomSchemaMappingProvider = CustomSchemaMappingProvider(from: "1", to: "2", entityMappings: [
.transformEntity(sourceEntity: "Foo", destinationEntity: "Bar", transformer: CustomSchemaMappingProvider.CustomMapping.inferredTransformation)
])

/// Create the source store and data set.
withExtendedLifetime(DataStack(src), { stack in
try! stack.addStorageAndWait(SQLiteStore())
try! stack.perform(synchronous: { $0.create(Into<Foo>()).name.value = "Willy" })
})

let expectation: XCTestExpectation = self.expectation(description: "migration-did-complete")

withExtendedLifetime(DataStack(src, dst, migrationChain: ["1", "2"]), { stack in
_ = stack.addStorage(SQLiteStore(fileURL: SQLiteStore.defaultFileURL, migrationMappingProviders: [migration]), completion: {
switch $0 {
case .success(_):
XCTAssertEqual(stack.modelSchema.rawModel().entities.count, 1)
XCTAssertEqual(try! stack.fetchCount(From<Bar>()), 1)
try! stack.perform(synchronous: { $0.create(Into<Bar>()).firstName.value = "Bobby" })
case .failure(let error):
XCTFail("\(error)")
}
expectation.fulfill()
})
})

self.waitAndCheckExpectations()
}
}

extension NSEntityDescription {
fileprivate convenience init(_ properties: [NSPropertyDescription]) {
self.init()
self.properties = properties
}
}

extension NSAttributeDescription {
fileprivate convenience init(_ name: String, renamingIdentifier: String? = nil, optional: Bool? = nil, defaultValue: Any? = nil) {
self.init()
self.name = name
self.renamingIdentifier = renamingIdentifier
self.isOptional = optional ?? true
self.defaultValue = defaultValue
}
}
8 changes: 4 additions & 4 deletions CoreStoreTests/SetupTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class SetupTests: BaseTestDataTestCase {
)
try sqliteStore.cs_eraseStorageAndWait(
metadata: metadata,
soureModelHint: stack.schemaHistory.schema(for: metadata)?.rawModel()
sourceModelHint: stack.schemaHistory.schema(for: metadata)?.rawModel()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing out this typo. Personally I think it should be in a separate PR though

)
XCTAssertFalse(fileManager.fileExists(atPath: sqliteStore.fileURL.path))
XCTAssertFalse(fileManager.fileExists(atPath: sqliteStore.fileURL.path.appending("-wal")))
Expand All @@ -244,7 +244,7 @@ class SetupTests: BaseTestDataTestCase {
do {

let metadata = try createStore()
try sqliteStore.cs_eraseStorageAndWait(metadata: metadata, soureModelHint: nil)
try sqliteStore.cs_eraseStorageAndWait(metadata: metadata, sourceModelHint: nil)
XCTAssertFalse(fileManager.fileExists(atPath: sqliteStore.fileURL.path))
XCTAssertFalse(fileManager.fileExists(atPath: sqliteStore.fileURL.path.appending("-wal")))
XCTAssertFalse(fileManager.fileExists(atPath: sqliteStore.fileURL.path.appending("-shm")))
Expand Down Expand Up @@ -358,7 +358,7 @@ class SetupTests: BaseTestDataTestCase {
)
try sqliteStore.cs_eraseStorageAndWait(
metadata: metadata,
soureModelHint: stack.schemaHistory.schema(for: metadata)?.rawModel()
sourceModelHint: stack.schemaHistory.schema(for: metadata)?.rawModel()
)
XCTAssertFalse(fileManager.fileExists(atPath: sqliteStore.fileURL.path))
XCTAssertFalse(fileManager.fileExists(atPath: sqliteStore.fileURL.path.appending("-wal")))
Expand All @@ -371,7 +371,7 @@ class SetupTests: BaseTestDataTestCase {
do {

let metadata = try createStore()
try sqliteStore.cs_eraseStorageAndWait(metadata: metadata, soureModelHint: nil)
try sqliteStore.cs_eraseStorageAndWait(metadata: metadata, sourceModelHint: nil)
XCTAssertFalse(fileManager.fileExists(atPath: sqliteStore.fileURL.path))
XCTAssertFalse(fileManager.fileExists(atPath: sqliteStore.fileURL.path.appending("-wal")))
XCTAssertFalse(fileManager.fileExists(atPath: sqliteStore.fileURL.path.appending("-shm")))
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ public protocol LocalStorage: StorageInterface {
var migrationMappingProviders: [SchemaMappingProvider] { get }
var localStorageOptions: LocalStorageOptions { get }
func dictionary(forOptions: LocalStorageOptions) -> [String: AnyObject]?
func cs_eraseStorageAndWait(metadata: [String: Any], soureModelHint: NSManagedObjectModel?) throws
func cs_eraseStorageAndWait(metadata: [String: Any], sourceModelHint: NSManagedObjectModel?) throws
}
```
If you have custom `NSIncrementalStore` or `NSAtomicStore` subclasses, you can implement this protocol and use it similarly to `SQLiteStore`.
Expand Down Expand Up @@ -463,7 +463,7 @@ if you do so, any model mismatch will be thrown as an error.

In general though, if migrations are expected the asynchronous variant `addStorage(_:completion:)` method is recommended instead:
```swift
let migrationProgress: Progress? = try dataStack.addStorage(
let migrationProgress: Progress? = dataStack.addStorage(
SQLiteStore(
fileName: "MyStore.sqlite",
configuration: "Config2"
Expand Down
4 changes: 2 additions & 2 deletions Sources/CSSQliteStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,11 @@ public final class CSSQLiteStore: NSObject, CSLocalStorage, CoreStoreObjectiveCT
Called by the `CSDataStack` to perform actual deletion of the store file from disk. Do not call directly! The `sourceModel` argument is a hint for the existing store's model version. For `CSSQLiteStore`, this converts the database's WAL journaling mode to DELETE before deleting the file.
*/
@objc
public func cs_eraseStorageAndWait(metadata: NSDictionary, soureModelHint: NSManagedObjectModel?, error: NSErrorPointer) -> Bool {
public func cs_eraseStorageAndWait(metadata: NSDictionary, sourceModelHint: NSManagedObjectModel?, error: NSErrorPointer) -> Bool {

return bridge(error) {

try self.bridgeToSwift.cs_eraseStorageAndWait(metadata: metadata as! [String: Any], soureModelHint: soureModelHint)
try self.bridgeToSwift.cs_eraseStorageAndWait(metadata: metadata as! [String: Any], sourceModelHint: sourceModelHint)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/CSStorageInterface.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,5 @@ public protocol CSLocalStorage: CSStorageInterface {
Called by the `CSDataStack` to perform actual deletion of the store file from disk. Do not call directly! The `sourceModel` argument is a hint for the existing store's model version. Implementers can use the `sourceModel` to perform necessary store operations. (SQLite stores for example, can convert WAL journaling mode to DELETE before deleting)
*/
@objc
func cs_eraseStorageAndWait(metadata: NSDictionary, soureModelHint: NSManagedObjectModel?, error: NSErrorPointer) -> Bool
func cs_eraseStorageAndWait(metadata: NSDictionary, sourceModelHint: NSManagedObjectModel?, error: NSErrorPointer) -> Bool
}
Loading