-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add post migration to trigger sync resources - WPB-14794 (#2240)
Co-authored-by: François Benaiteau <netbe@users.noreply.github.com>
- Loading branch information
1 parent
a2bf418
commit 7a7574d
Showing
20 changed files
with
640 additions
and
36 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
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
412 changes: 412 additions & 0 deletions
412
wire-ios-data-model/Resources/zmessaging.xcdatamodeld/zmessaging2.120.0.xcdatamodel/contents
Large diffs are not rendered by default.
Oops, something went wrong.
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
33 changes: 33 additions & 0 deletions
33
...ta-model/Source/ManagedObjectContext/Migration/119-120/ForceSyncResourcesPostAction.swift
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,33 @@ | ||
// | ||
// Wire | ||
// Copyright (C) 2024 Wire Swiss GmbH | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see http://www.gnu.org/licenses/. | ||
// | ||
|
||
import Foundation | ||
|
||
final class ForceSyncResourcesPostAction: CoreDataMigrationAction { | ||
|
||
override func execute(in context: NSManagedObjectContext) { | ||
do { | ||
try context.setMigrationNeedsSyncResources() | ||
} catch { | ||
WireLogger.localStorage.error( | ||
"Failed to trigger syncResources for federation migration issue: \(error.localizedDescription)", | ||
attributes: .safePublic | ||
) | ||
} | ||
} | ||
} |
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
53 changes: 53 additions & 0 deletions
53
wire-ios-data-model/Source/ManagedObjectContext/NSManagedObjectContext+SyncResources.swift
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,53 @@ | ||
// | ||
// Wire | ||
// Copyright (C) 2024 Wire Swiss GmbH | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see http://www.gnu.org/licenses/. | ||
// | ||
|
||
import Foundation | ||
|
||
// This contains some methods to pass the information through the persistence store metadata | ||
// that we need a sync resources after | ||
public extension NSManagedObjectContext { | ||
|
||
private var migrationsNeedToSyncResourcesKey: String { | ||
"migrationsNeedToSyncResources" | ||
} | ||
|
||
internal enum migrationsNeedToSyncResourcesError: Error { | ||
case couldNotPersistMetadata | ||
} | ||
|
||
/// use to trigger sync resources after some CoreData migrations | ||
func setMigrationNeedsSyncResources() throws { | ||
setPersistentStoreMetadata(1, key: migrationsNeedToSyncResourcesKey) | ||
if !makeMetadataPersistent() { | ||
throw MigrationNeedsSlowSyncError.couldNotPersistMetadata | ||
} | ||
} | ||
|
||
/// checks if we need a syncResources after migrations | ||
func readMigrationNeedsSyncResourcesFlag() -> Bool { | ||
let value = (persistentStoreMetadata(forKey: migrationsNeedToSyncResourcesKey) as? Int) ?? 0 | ||
return value == 1 | ||
} | ||
|
||
/// Reset migration needs syncResources flag | ||
func resetMigrationNeedsSyncResoucesFlagIfNeeded() { | ||
if readMigrationNeedsSyncResourcesFlag() { | ||
setPersistentStoreMetadata(Int?.none, key: migrationsNeedToSyncResourcesKey) | ||
} | ||
} | ||
} |
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
Binary file not shown.
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
58 changes: 58 additions & 0 deletions
58
...a-model/Tests/Source/ManagedObjectContext/DatabaseMigrationTests+ForceSyncResources.swift
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,58 @@ | ||
// | ||
// Wire | ||
// Copyright (C) 2024 Wire Swiss GmbH | ||
// | ||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see http://www.gnu.org/licenses/. | ||
// | ||
|
||
import Foundation | ||
import XCTest | ||
@testable import WireDataModel | ||
|
||
final class DatabaseMigrationTests_ForceSyncResources: XCTestCase { | ||
|
||
private let bundle = Bundle(for: ZMManagedObject.self) | ||
private let teamId = UUID() | ||
private let tmpStoreURL = | ||
URL(fileURLWithPath: "\(NSTemporaryDirectory())DatabaseMigrationTests_ForceSyncResources/") | ||
private let helper = DatabaseMigrationHelper() | ||
|
||
override func setUpWithError() throws { | ||
try super.setUpWithError() | ||
try FileManager.default.createDirectory(at: tmpStoreURL, withIntermediateDirectories: true) | ||
} | ||
|
||
override func tearDownWithError() throws { | ||
try? FileManager.default.removeItem(at: tmpStoreURL) | ||
try super.tearDownWithError() | ||
} | ||
|
||
func testThatItPerformsMigrationFrom119Version_ToCurrentModelVersion() throws { | ||
let initialVersion = "2.119.0" | ||
|
||
try helper.migrateStoreToCurrentVersion( | ||
sourceVersion: initialVersion, | ||
preMigrationAction: { _ in | ||
// nothing | ||
}, | ||
postMigrationAction: { context in | ||
try context.performAndWait { | ||
// verify | ||
XCTAssertTrue(context.readMigrationNeedsSyncResourcesFlag()) | ||
} | ||
}, | ||
for: self | ||
) | ||
} | ||
} |
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
Oops, something went wrong.