Skip to content

Commit a8fba55

Browse files
committed
Further simplified migration by removing unneeded convenience methods
1 parent 388d0bd commit a8fba55

File tree

4 files changed

+35
-49
lines changed

4 files changed

+35
-49
lines changed

CoreDataMigration-Example/CoreData/CoreDataManager.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class CoreDataManager {
1818
let persistentContainer = NSPersistentContainer(name: "CoreDataMigration_Example")
1919
let description = persistentContainer.persistentStoreDescriptions.first
2020
description?.shouldInferMappingModelAutomatically = false //inferred mapping will be handled else where
21+
description?.shouldMigrateStoreAutomatically = false
2122
description?.type = storeType
2223

2324
return persistentContainer
@@ -75,9 +76,9 @@ class CoreDataManager {
7576
fatalError("persistentContainer was not set up properly")
7677
}
7778

78-
if migrator.requiresMigration(at: storeURL) {
79+
if migrator.requiresMigration(at: storeURL, toVersion: CoreDataMigrationVersion.latest) {
7980
DispatchQueue.global(qos: .userInitiated).async {
80-
self.migrator.migrateStore(at: storeURL)
81+
self.migrator.migrateStore(at: storeURL, toVersion: CoreDataMigrationVersion.latest)
8182

8283
DispatchQueue.main.async {
8384
completion()

CoreDataMigration-Example/CoreData/Migration/CoreDataMigrator.swift

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,7 @@ import CoreData
1010

1111
protocol CoreDataMigratorProtocol {
1212
func requiresMigration(at storeURL: URL, toVersion version: CoreDataMigrationVersion) -> Bool
13-
func migrateStore(at: URL)
14-
}
15-
16-
extension CoreDataMigratorProtocol {
17-
18-
func requiresMigration(at storeURL: URL) -> Bool {
19-
return requiresMigration(at: storeURL, toVersion: CoreDataMigrationVersion.latest)
20-
}
13+
func migrateStore(at storeURL: URL, toVersion version: CoreDataMigrationVersion)
2114
}
2215

2316
/**
@@ -50,15 +43,11 @@ class CoreDataMigrator: CoreDataMigratorProtocol {
5043

5144
// MARK: - Migration
5245

53-
func migrateStore(at storeURL: URL) {
54-
migrateStore(from: storeURL, to: storeURL, targetVersion: CoreDataMigrationVersion.latest)
55-
}
56-
57-
func migrateStore(from sourceURL: URL, to targetURL: URL, targetVersion: CoreDataMigrationVersion) {
58-
forceWALCheckpointingForStore(at: sourceURL)
46+
func migrateStore(at storeURL: URL, toVersion version: CoreDataMigrationVersion) {
47+
forceWALCheckpointingForStore(at: storeURL)
5948

60-
var currentURL = sourceURL
61-
let migrationSteps = self.migrationSteps(forStoreAtURL: sourceURL, toDestinationVersion: targetVersion)
49+
var currentURL = storeURL
50+
let migrationSteps = self.migrationStepsForStore(at: storeURL, toVersion: version)
6251

6352
for migrationStep in migrationSteps {
6453
let manager = NSMigrationManager(sourceModel: migrationStep.sourceModel, destinationModel: migrationStep.destinationModel)
@@ -70,29 +59,29 @@ class CoreDataMigrator: CoreDataMigratorProtocol {
7059
fatalError("failed attempting to migrate from \(migrationStep.sourceModel) to \(migrationStep.destinationModel), error: \(error)")
7160
}
7261

73-
if currentURL != sourceURL {
62+
if currentURL != storeURL {
7463
//Destroy intermediate step's store
7564
NSPersistentStoreCoordinator.destroyStore(at: currentURL)
7665
}
7766

7867
currentURL = destinationURL
7968
}
8069

81-
NSPersistentStoreCoordinator.replaceStore(at: targetURL, withStoreAt: currentURL)
70+
NSPersistentStoreCoordinator.replaceStore(at: storeURL, withStoreAt: currentURL)
8271

83-
if (currentURL != sourceURL) {
72+
if (currentURL != storeURL) {
8473
NSPersistentStoreCoordinator.destroyStore(at: currentURL)
8574
}
8675
}
8776

88-
private func migrationSteps(forStoreAtURL storeURL: URL, toDestinationVersion destinationVersion: CoreDataMigrationVersion) -> [CoreDataMigrationStep] {
77+
private func migrationStepsForStore(at storeURL: URL, toVersion destinationVersion: CoreDataMigrationVersion) -> [CoreDataMigrationStep] {
8978
guard let metadata = NSPersistentStoreCoordinator.metadata(at: storeURL), let sourceVersion = CoreDataMigrationVersion.compatibleVersionForStoreMetadata(metadata) else {
9079
fatalError("unknown store version at URL \(storeURL)")
9180
}
9281

9382
return migrationSteps(fromSourceVersion: sourceVersion, toDestinationVersion: destinationVersion)
9483
}
95-
84+
9685
private func migrationSteps(fromSourceVersion sourceVersion: CoreDataMigrationVersion, toDestinationVersion destinationVersion: CoreDataMigrationVersion) -> [CoreDataMigrationStep] {
9786
var sourceVersion = sourceVersion
9887
var migrationSteps = [CoreDataMigrationStep]()

CoreDataMigration-ExampleTests/Tests/CoreData/Migration/CoreDataMigratorTests.swift

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,14 @@ class CoreDataMigratorTests: XCTestCase {
4343

4444
func test_individualStepMigration_1to2() {
4545
let sourceURL = FileManager.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_1.sqlite")
46-
let targetURL = sourceURL
47-
let targetVersion = CoreDataMigrationVersion.version2
46+
let toVersion = CoreDataMigrationVersion.version2
4847

49-
sut.migrateStore(from: sourceURL, to: targetURL, targetVersion: targetVersion)
48+
sut.migrateStore(at: sourceURL, toVersion: toVersion)
5049

51-
XCTAssertTrue(FileManager.default.fileExists(atPath: targetURL.path))
50+
XCTAssertTrue(FileManager.default.fileExists(atPath: sourceURL.path))
5251

53-
let model = NSManagedObjectModel.managedObjectModel(forResource: targetVersion.rawValue)
54-
let context = NSManagedObjectContext(model: model, storeURL: targetURL)
52+
let model = NSManagedObjectModel.managedObjectModel(forResource: toVersion.rawValue)
53+
let context = NSManagedObjectContext(model: model, storeURL: sourceURL)
5554
let request = NSFetchRequest<NSManagedObject>.init(entityName: "Post")
5655
let sort = NSSortDescriptor(key: "postID", ascending: false)
5756
request.sortDescriptors = [sort]
@@ -73,15 +72,14 @@ class CoreDataMigratorTests: XCTestCase {
7372

7473
func test_individualStepMigration_2to3() {
7574
let sourceURL = FileManager.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_2.sqlite")
76-
let targetURL = sourceURL
77-
let targetVersion = CoreDataMigrationVersion.version3
75+
let toVersion = CoreDataMigrationVersion.version3
7876

79-
sut.migrateStore(from: sourceURL, to: targetURL, targetVersion: targetVersion)
77+
sut.migrateStore(at: sourceURL, toVersion: toVersion)
8078

81-
XCTAssertTrue(FileManager.default.fileExists(atPath: targetURL.path))
79+
XCTAssertTrue(FileManager.default.fileExists(atPath: sourceURL.path))
8280

83-
let model = NSManagedObjectModel.managedObjectModel(forResource: targetVersion.rawValue)
84-
let context = NSManagedObjectContext(model: model, storeURL: targetURL)
81+
let model = NSManagedObjectModel.managedObjectModel(forResource: toVersion.rawValue)
82+
let context = NSManagedObjectContext(model: model, storeURL: sourceURL)
8583

8684
let postRequest = NSFetchRequest<NSManagedObject>.init(entityName: "Post")
8785
let postSort = NSSortDescriptor(key: "postID", ascending: false)
@@ -116,15 +114,14 @@ class CoreDataMigratorTests: XCTestCase {
116114

117115
func test_individualStepMigration_3to4() {
118116
let sourceURL = FileManager.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_3.sqlite")
119-
let targetURL = sourceURL
120-
let targetVersion = CoreDataMigrationVersion.version4
117+
let toVersion = CoreDataMigrationVersion.version4
121118

122-
sut.migrateStore(from: sourceURL, to: targetURL, targetVersion: targetVersion)
119+
sut.migrateStore(at: sourceURL, toVersion: toVersion)
123120

124-
XCTAssertTrue(FileManager.default.fileExists(atPath: targetURL.path))
121+
XCTAssertTrue(FileManager.default.fileExists(atPath: sourceURL.path))
125122

126-
let model = NSManagedObjectModel.managedObjectModel(forResource: targetVersion.rawValue)
127-
let context = NSManagedObjectContext(model: model, storeURL: targetURL)
123+
let model = NSManagedObjectModel.managedObjectModel(forResource: toVersion.rawValue)
124+
let context = NSManagedObjectContext(model: model, storeURL: sourceURL)
128125

129126
let postRequest = NSFetchRequest<NSManagedObject>.init(entityName: "Post")
130127
let postSort = NSSortDescriptor(key: "postID", ascending: false)
@@ -167,15 +164,14 @@ class CoreDataMigratorTests: XCTestCase {
167164

168165
func test_multipleStepMigration_fromVersion1toVersion4() {
169166
let sourceURL = FileManager.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_1.sqlite")
170-
let targetURL = sourceURL
171-
let targetVersion = CoreDataMigrationVersion.version4
167+
let toVersion = CoreDataMigrationVersion.version4
172168

173-
sut.migrateStore(from: sourceURL, to: targetURL, targetVersion: targetVersion)
169+
sut.migrateStore(at: sourceURL, toVersion: toVersion)
174170

175-
XCTAssertTrue(FileManager.default.fileExists(atPath: targetURL.path))
171+
XCTAssertTrue(FileManager.default.fileExists(atPath: sourceURL.path))
176172

177-
let model = NSManagedObjectModel.managedObjectModel(forResource: targetVersion.rawValue)
178-
let context = NSManagedObjectContext(model: model, storeURL: targetURL)
173+
let model = NSManagedObjectModel.managedObjectModel(forResource: toVersion.rawValue)
174+
let context = NSManagedObjectContext(model: model, storeURL: sourceURL)
179175

180176
let postRequest = NSFetchRequest<NSManagedObject>.init(entityName: "Post")
181177
let colorRequest = NSFetchRequest<NSManagedObject>.init(entityName: "Color")
@@ -192,7 +188,7 @@ class CoreDataMigratorTests: XCTestCase {
192188
func test_requiresMigration_fromVersion1ToCurrent_true() {
193189
let storeURL = FileManager.moveFileFromBundleToTmpDirectory(fileName: "CoreDataMigration_Example_1.sqlite")
194190

195-
let requiresMigration = sut.requiresMigration(at: storeURL)
191+
let requiresMigration = sut.requiresMigration(at: storeURL, toVersion: CoreDataMigrationVersion.latest)
196192

197193
XCTAssertTrue(requiresMigration)
198194
}

CoreDataMigration-ExampleTests/Tests/Mocks/MockCoreDataMigrator.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class MockCoreDataMigrator: CoreDataMigratorProtocol {
2626
return requiresMigrationToBeReturned
2727
}
2828

29-
func migrateStore(at: URL) {
29+
func migrateStore(at storeURL: URL, toVersion version: CoreDataMigrationVersion) {
3030
migrateStoreWasCalled = true
3131
}
3232
}

0 commit comments

Comments
 (0)