Skip to content

Commit

Permalink
Merge 21.7.2 hotfix into 21.8 (#20221)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokagio authored Mar 1, 2023
2 parents 68f6127 + 5faf182 commit a02f146
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 35 deletions.
69 changes: 35 additions & 34 deletions WordPress/Classes/Utility/ContextManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,41 @@ public class ContextManager: NSObject, CoreDataStack, CoreDataStackSwift {
save(context, .asynchronously)
}
}

static func migrateDataModelsIfNecessary(storeURL: URL, objectModel: NSManagedObjectModel) throws {
guard FileManager.default.fileExists(atPath: storeURL.path) else {
DDLogInfo("No store exists at \(storeURL). Skipping migration.")
return
}

guard let metadata = try? NSPersistentStoreCoordinator.metadataForPersistentStore(ofType: NSSQLiteStoreType, at: storeURL),
!objectModel.isConfiguration(withName: nil, compatibleWithStoreMetadata: metadata)
else {
return
}

DDLogWarn("Migration required for persistent store.")

guard let modelFileURL = Bundle.main.url(forResource: "WordPress", withExtension: "momd") else {
fatalError("Can't find WordPress.momd")
}

guard let versionInfo = NSDictionary(contentsOf: modelFileURL.appendingPathComponent("VersionInfo.plist")) else {
fatalError("Can't get the object model's version info")
}

guard let modelNames = (versionInfo["NSManagedObjectModel_VersionHashes"] as? [String: AnyObject])?.keys else {
fatalError("Can't parse the model versions")
}

let sortedModelNames = modelNames.sorted { $0.compare($1, options: .numeric) == .orderedAscending }
try CoreDataIterativeMigrator.iterativeMigrate(
sourceStore: storeURL,
storeType: NSSQLiteStoreType,
to: objectModel,
using: sortedModelNames
)
}
}

// MARK: - Private methods
Expand Down Expand Up @@ -236,40 +271,6 @@ private extension ContextManager {
return persistentContainer
}

static func migrateDataModelsIfNecessary(storeURL: URL, objectModel: NSManagedObjectModel) throws {
guard FileManager.default.fileExists(atPath: storeURL.path) else {
DDLogInfo("No store exists at \(storeURL). Skipping migration.")
return
}

guard let metadata = try? NSPersistentStoreCoordinator.metadataForPersistentStore(ofType: NSSQLiteStoreType, at: storeURL),
objectModel.isConfiguration(withName: nil, compatibleWithStoreMetadata: metadata)
else {
return
}

DDLogWarn("Migration required for persistent store.")

guard let modelFileURL = Bundle.main.url(forResource: "WordPress", withExtension: "momd") else {
fatalError("Can't find WordPress.momd")
}

guard let versionInfo = NSDictionary(contentsOf: modelFileURL.appendingPathComponent("VersionInfo.plist")) else {
fatalError("Can't get the object model's version info")
}

guard let modelNames = (versionInfo["NSManagedObjectModel_VersionHashes"] as? [String: AnyObject])?.keys else {
fatalError("Can't parse the model versions")
}

let sortedModelNames = modelNames.sorted { $0.compare($1, options: .numeric) == .orderedAscending }
try CoreDataIterativeMigrator.iterativeMigrate(
sourceStore: storeURL,
storeType: NSSQLiteStoreType,
to: objectModel,
using: sortedModelNames
)
}
}

extension ContextManager {
Expand Down
15 changes: 14 additions & 1 deletion WordPress/Classes/Utility/CoreDataHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -250,14 +250,19 @@ extension CoreDataStack {
throw ContextManager.ContextManagerError.missingDatabase
}

try? migrateDatabaseIfNecessary(at: databaseLocation)

mainContext.reset()
try storeCoordinator.remove(store)
let databaseReplaced = replaceDatabase(from: databaseLocation, to: currentDatabaseLocation)

do {
let options = [NSMigratePersistentStoresAutomaticallyOption: true,
NSInferMappingModelAutomaticallyOption: true]
try storeCoordinator.addPersistentStore(ofType: NSSQLiteStoreType,
configurationName: nil,
at: currentDatabaseLocation)
at: currentDatabaseLocation,
options: options)

if databaseReplaced {
// The database was replaced successfully and the store added with no errors so we
Expand Down Expand Up @@ -327,4 +332,12 @@ extension CoreDataStack {
_ = try? FileManager.default.replaceItemAt(locationShm, withItemAt: shmBackup)
_ = try? FileManager.default.replaceItemAt(locationWal, withItemAt: walBackup)
}

private func migrateDatabaseIfNecessary(at databaseLocation: URL) throws {
guard let modelFileURL = Bundle.main.url(forResource: "WordPress", withExtension: "momd"),
let objectModel = NSManagedObjectModel(contentsOf: modelFileURL) else {
return
}
try ContextManager.migrateDataModelsIfNecessary(storeURL: databaseLocation, objectModel: objectModel)
}
}

0 comments on commit a02f146

Please sign in to comment.