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

Add proper migration tests #577

Merged
merged 9 commits into from
Nov 27, 2023
Merged
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
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