-
Notifications
You must be signed in to change notification settings - Fork 424
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In case Pending Deletion flag is nil, populate it with 'false' value (#…
…2997) Task/Issue URL: https://app.asana.com/0/856498667320406/1207582863974542/f Tech Design URL: CC: Description: Check if DB sate is unexpected and fix it if needed. Steps to test this PR: See tests to validate how state is fixed. Ensure Bookmarks are still working after the check, and if migration passes.
- Loading branch information
Showing
5 changed files
with
236 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
// | ||
// BookmarksStateRepair.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import Foundation | ||
import CoreData | ||
import Bookmarks | ||
import Persistence | ||
|
||
public class BookmarksStateRepair { | ||
|
||
enum Constants { | ||
static let pendingDeletionRepaired = "stateRepair_pendingDeletionRepaired" | ||
} | ||
|
||
public enum RepairStatus: Equatable { | ||
case alreadyPerformed | ||
case noBrokenData | ||
case dataRepaired | ||
case repairError(Error) | ||
|
||
public static func == (lhs: BookmarksStateRepair.RepairStatus, rhs: BookmarksStateRepair.RepairStatus) -> Bool { | ||
switch (lhs, rhs) { | ||
case (.alreadyPerformed, .alreadyPerformed), (.noBrokenData, .noBrokenData), (.dataRepaired, .dataRepaired), (.repairError, .repairError): | ||
return true | ||
default: | ||
return false | ||
} | ||
} | ||
} | ||
|
||
let keyValueStore: KeyValueStoring | ||
|
||
public init(keyValueStore: KeyValueStoring) { | ||
self.keyValueStore = keyValueStore | ||
} | ||
|
||
public func validateAndRepairPendingDeletionState(in context: NSManagedObjectContext) -> RepairStatus { | ||
|
||
guard keyValueStore.object(forKey: Constants.pendingDeletionRepaired) == nil else { | ||
return .alreadyPerformed | ||
} | ||
|
||
do { | ||
let fr = BookmarkEntity.fetchRequest() | ||
fr.predicate = NSPredicate(format: "%K == nil", #keyPath(BookmarkEntity.isPendingDeletion)) | ||
|
||
let result = try context.fetch(fr) | ||
|
||
if !result.isEmpty { | ||
for obj in result { | ||
obj.setValue(false, forKey: #keyPath(BookmarkEntity.isPendingDeletion)) | ||
} | ||
|
||
try context.save() | ||
|
||
keyValueStore.set(true, forKey: Constants.pendingDeletionRepaired) | ||
return .dataRepaired | ||
} else { | ||
keyValueStore.set(true, forKey: Constants.pendingDeletionRepaired) | ||
return .noBrokenData | ||
} | ||
} catch { | ||
return .repairError(error) | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
// | ||
// BookmarkStateRepairTests.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2024 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
import XCTest | ||
import CoreData | ||
import Bookmarks | ||
import TestUtils | ||
@testable import Core | ||
@testable import DuckDuckGo | ||
|
||
class BookmarkStateRepairTests: XCTestCase { | ||
|
||
let dbStack = MockBookmarksDatabase.make(prepareFolderStructure: false) | ||
|
||
let mockKeyValueStore = MockKeyValueStore() | ||
|
||
override func setUp() async throws { | ||
try await super.setUp() | ||
|
||
let containerLocation = MockBookmarksDatabase.tempDBDir() | ||
try FileManager.default.createDirectory(at: containerLocation, withIntermediateDirectories: true) | ||
} | ||
|
||
override func tearDown() async throws { | ||
try await super.tearDown() | ||
|
||
try dbStack.tearDown(deleteStores: true) | ||
} | ||
|
||
func testWhenThereIsNoIssueThenThereAreNoChanges() { | ||
prepareValidStructure() | ||
|
||
let testContext = dbStack.makeContext(concurrencyType: .privateQueueConcurrencyType) | ||
testContext.performAndWait { | ||
|
||
let repair = BookmarksStateRepair(keyValueStore: mockKeyValueStore) | ||
|
||
XCTAssertEqual(repair.validateAndRepairPendingDeletionState(in: testContext), .noBrokenData) | ||
XCTAssertEqual(repair.validateAndRepairPendingDeletionState(in: testContext), .alreadyPerformed) | ||
} | ||
} | ||
|
||
func testWhenPendingDeletionIsNilThenItIsFixed() { | ||
prepareBrokenStructure() | ||
|
||
let testContext = dbStack.makeContext(concurrencyType: .privateQueueConcurrencyType) | ||
testContext.performAndWait { | ||
|
||
let repair = BookmarksStateRepair(keyValueStore: mockKeyValueStore) | ||
|
||
XCTAssertEqual(repair.validateAndRepairPendingDeletionState(in: testContext), .dataRepaired) | ||
|
||
mockKeyValueStore.removeObject(forKey: BookmarksStateRepair.Constants.pendingDeletionRepaired) | ||
XCTAssertEqual(repair.validateAndRepairPendingDeletionState(in: testContext), .noBrokenData) | ||
XCTAssertEqual(repair.validateAndRepairPendingDeletionState(in: testContext), .alreadyPerformed) | ||
} | ||
} | ||
|
||
private func prepareStructure(_ block: (NSManagedObjectContext) -> Void) { | ||
let context = dbStack.makeContext(concurrencyType: .privateQueueConcurrencyType) | ||
|
||
context.performAndWait { | ||
BookmarkUtils.prepareFoldersStructure(in: context) | ||
block(context) | ||
do { | ||
try context.save() | ||
} catch { | ||
XCTFail("Could not save context") | ||
} | ||
} | ||
} | ||
|
||
private func prepareValidStructure() { | ||
prepareStructure { context in | ||
guard let root = BookmarkUtils.fetchRootFolder(context) else { | ||
XCTFail("Root missing") | ||
return | ||
} | ||
|
||
let bookmarkA = BookmarkEntity.makeBookmark(title: "A", url: "A", parent: root, context: context) | ||
let bookmarkB = BookmarkEntity.makeBookmark(title: "B", url: "B", parent: root, context: context) | ||
let bookmarkC = BookmarkEntity.makeBookmark(title: "C", url: "C", parent: root, context: context) | ||
} | ||
} | ||
|
||
private func prepareBrokenStructure() { | ||
prepareStructure { context in | ||
guard let root = BookmarkUtils.fetchRootFolder(context) else { | ||
XCTFail("Root missing") | ||
return | ||
} | ||
|
||
let bookmarkA = BookmarkEntity.makeBookmark(title: "A", url: "A", parent: root, context: context) | ||
let bookmarkB = BookmarkEntity.makeBookmark(title: "B", url: "B", parent: root, context: context) | ||
let bookmarkC = BookmarkEntity.makeBookmark(title: "C", url: "C", parent: root, context: context) | ||
|
||
bookmarkA.setValue(nil, forKey: #keyPath(BookmarkEntity.isPendingDeletion)) | ||
bookmarkB.setValue(nil, forKey: #keyPath(BookmarkEntity.isPendingDeletion)) | ||
} | ||
} | ||
|
||
} |