CoreStore
CoreStore copied to clipboard
My migration test are failing
Hi,
I am trying to test my migration chain, but I keep getting an error.
This is my test:
class MigrationTests: XCTestCase {
private let testStore = SQLiteStore(fileName: "migrationTestStore",
localStorageOptions: .allowSynchronousLightweightMigration)
override func setUp() {
try! FileManager.default.removeItem(at: testStore.fileURL)
}
func testMigrationChain() {
do {
let v1_0_0 = DataStack(V1_0_0.schema)
try v1_0_0.addStorageAndWait(testStore)
try v1_0_0.addV1_0_0TestData()
let v1_0_1 = DataStack(V1_0_0.schema,
V1_0_1.schema,
migrationChain: [
V1_0_0.schema.modelVersion: V1_0_1.schema.modelVersion,
])
try v1_0_1.addStorageAndWait(testStore)
} catch {
XCTFail("An error occured: " + error.localizedDescription)
}
}
}
And this is the error I'm getting:
⚠️ [CoreStore: Error] DataStack.swift:364 addStorageAndWait(_:)
↪︎ Failed to add 'CoreStore.SQLiteStore' to the stack.
(CoreStore.CoreStoreError) .asynchronousMigrationRequired (
.errorDomain = "com.corestore.error";
.errorCode = 4;
.localStoreURL = "file:///Users/private/Library/Developer/CoreSimulator/Devices/A1DCD685-A1C6-4836-A5E5-10576E3609E2/data/Containers/Data/Application/74D16729-CCE2-44E6-B53A-B12674CD4FFC/Library/Application%20Support/de.sschmaljohann.match-your-friends/migrationTestStore";
.NSError = (
.domain = "NSCocoaErrorDomain";
.code = 134130;
.userInfo = 3 key-value(s) [
"reason" = "Can't find model for source store";
"URL" = "file:///Users/private/Library/Developer/CoreSimulator/Devices/A1DCD685-A1C6-4836-A5E5-10576E3609E2/data/Containers/Data/Application/74D16729-CCE2-44E6-B53A-B12674CD4FFC/Library/Application%20Support/de.sschmaljohann.match-your-friends/migrationTestStore";
"metadata" = {
NSPersistenceFrameworkVersion = 1048;
NSStoreModelVersionHashes = {
BasePlayer = {length = 32, bytes = 0x8d25d780 c64ca18b 2f7a402c 9f48d2b6 ... 97e0befa 35fc2cc7 };
DeletedPlayer = {length = 32, bytes = 0x7a982c1e a821b082 8de4b4b4 b8d3dc1b ... fe050156 ce877d44 };
Discipline = {length = 32, bytes = 0x945936fc 4815248d 030a31ee 7a81865b ... 2858f029 b2c2bf7e };
Match = {length = 32, bytes = 0x403ffea7 d06188c8 303d9bf9 916b134b ... e5866ef4 1b63cab5 };
Player = {length = 32, bytes = 0x3f3b489a 0500c94d 751be9ef 45fc3dc0 ... d3a18ae3 c5aa6e50 };
};
NSStoreModelVersionHashesDigest = "KP36cwhVbFU8fri/7OK25vWeis9Z9IY9ikmTgcWeODfYLxQmaL+uBd5oN38KhUdV3NewP0TTc0kDRjyV6VRxuw==";
NSStoreModelVersionHashesVersion = 3;
NSStoreModelVersionIdentifiers = (
);
NSStoreType = SQLite;
NSStoreUUID = "A4FF91FD-D749-4815-B5A8-2E5336CA0F6F";
"_NSAutoVacuumLevel" = 2;
};
];
);
)
Is there anything obvious I'm doing wrong?
Ok, I solved the issue(s) I had.
First I needed to do the migration asynchronously, so instead of try v1_0_1.addStorageAndWait(testStore), I needed to do this:
let expectation = XCTestExpectation(description: "Asyncronous adding of storeage")
v1_0_1.addStorage(testStore) { result in
switch result {
case let .failure(error):
XCTFail("An error occured: " + error.localizedDescription)
default:
break
}
expectation.fulfill()
}
wait(for: [expectation], timeout: 15)
Secondly, I realised, I needed to run this part of the test on the main queue, so the final working test was this:
class MigrationTests: XCTestCase {
private let testStore = SQLiteStore(fileName: "migrationTestStore",
localStorageOptions: .allowSynchronousLightweightMigration)
override func setUp() {
try? FileManager.default.removeItem(at: testStore.fileURL)
}
func testMigrationFrom1_0_0() {
testMigration(from: V1_0_0.self)
}
private func testMigration(from: TestableVersion.Type) {
do {
let start = DataStack(from.schema)
try start.addStorageAndWait(testStore)
try from.addTestData(to: start)
let end = DataStack(CurrentVersion.schema,
from.schema,
migrationChain: CurrentVersion.migrationChain)
let expectation = XCTestExpectation(description: "Asyncronous adding of storeage")
var progress: Progress?
DispatchQueue.main.async {
progress = end.addStorage(self.testStore) { result in
switch result {
case let .failure(error):
XCTFail("An error occured: " + error.localizedDescription)
default:
break
}
expectation.fulfill()
}
}
if progress != nil {
wait(for: [expectation], timeout: 15)
} else {
print("no migration needed from \(from.schema.modelVersion) to \(CurrentVersion.schema.modelVersion)")
}
} catch {
XCTFail("An error occured: " + error.localizedDescription)
}
}
}