Skip to content

Commit

Permalink
Add proper migration tests (#577)
Browse files Browse the repository at this point in the history
Task/Issue URL: https://app.asana.com/0/414709148257752/1206032628720063/f
https://app.asana.com/0/414709148257752/1206020815014024/f
iOS PR: duckduckgo/iOS#2196
macOS PR: duckduckgo/macos-browser#1889
What kind of version bump will this require?: Patch

Description:

Fix possible crash when migrating from older DB versions.
  • Loading branch information
bwaresiak authored Nov 27, 2023
1 parent 8202035 commit 543e1d7
Show file tree
Hide file tree
Showing 14 changed files with 554 additions and 67 deletions.
18 changes: 18 additions & 0 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ let package = Package(
.process("BookmarksModel.xcdatamodeld")
]
),
.executableTarget(name: "BookmarksTestDBBuilder",
dependencies: [
"Bookmarks",
"Persistence"
],
path: "Sources/BookmarksTestDBBuilder"
),
.target(
name: "BookmarksTestsUtils",
dependencies: [
Expand Down Expand Up @@ -234,6 +241,17 @@ let package = Package(
dependencies: [
"Bookmarks",
"BookmarksTestsUtils"
],
resources: [
.copy("Resources/Bookmarks_V1.sqlite"),
.copy("Resources/Bookmarks_V1.sqlite-shm"),
.copy("Resources/Bookmarks_V1.sqlite-wal"),
.copy("Resources/Bookmarks_V2.sqlite"),
.copy("Resources/Bookmarks_V2.sqlite-shm"),
.copy("Resources/Bookmarks_V2.sqlite-wal"),
.copy("Resources/Bookmarks_V3.sqlite"),
.copy("Resources/Bookmarks_V3.sqlite-shm"),
.copy("Resources/Bookmarks_V3.sqlite-wal")
]),
.testTarget(
name: "BrowserServicesKitTests",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,27 +28,32 @@ public class BookmarkFormFactorFavoritesMigration {
public static func getFavoritesOrderFromPreV4Model(dbContainerLocation: URL,
dbFileURL: URL,
errorEvents: EventMapping<MigrationErrors>? = nil) -> [String]? {
var oldFavoritesOrder: [String]?

let metadata = try? NSPersistentStoreCoordinator.metadataForPersistentStore(ofType: NSSQLiteStoreType,
at: dbFileURL)
if let metadata, let version = metadata["NSStoreModelVersionHashesVersion"] as? Int, version < 4 {
// Before migrating to latest scheme version, read order of favorites from DB
guard let metadata = try? NSPersistentStoreCoordinator.metadataForPersistentStore(ofType: NSSQLiteStoreType, at: dbFileURL),
let latestModel = CoreDataDatabase.loadModel(from: bundle, named: "BookmarksModel"),
!latestModel.isConfiguration(withName: nil, compatibleWithStoreMetadata: metadata)
else {
return nil
}

let v3BookmarksModel = NSManagedObjectModel.mergedModel(from: [Bookmarks.bundle], forStoreMetadata: metadata)!
// Before migrating to latest scheme version, read order of favorites from DB

let oldDB = CoreDataDatabase(name: "Bookmarks",
containerLocation: dbContainerLocation,
model: v3BookmarksModel)
oldDB.loadStore { context, error in
guard let context = context else {
errorEvents?.fire(.couldNotLoadDatabase, error: error)
return
}
let oldBookmarksModel = NSManagedObjectModel.mergedModel(from: [Bookmarks.bundle], forStoreMetadata: metadata)!
let oldDB = CoreDataDatabase(name: dbFileURL.deletingPathExtension().lastPathComponent,
containerLocation: dbContainerLocation,
model: oldBookmarksModel)

let favs = BookmarkUtils.fetchLegacyFavoritesFolder(context)
oldFavoritesOrder = favs?.favoritesArray.compactMap { $0.uuid }
var oldFavoritesOrder: [String]?

oldDB.loadStore { context, error in
guard let context = context else {
errorEvents?.fire(.couldNotLoadDatabase, error: error)
return
}

let favs = BookmarkUtils.fetchLegacyFavoritesFolder(context)
let orderedFavorites = favs?.favorites?.array as? [BookmarkEntity] ?? []
oldFavoritesOrder = orderedFavorites.compactMap { $0.uuid }
}
return oldFavoritesOrder
}
Expand Down
Loading

0 comments on commit 543e1d7

Please sign in to comment.