Skip to content

fix: Drop realm on migration error in production #1761

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

Merged
merged 1 commit into from
Apr 15, 2025
Merged
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
10 changes: 8 additions & 2 deletions MailCore/Utils/Model/Realm/RealmAccessible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import Foundation
import InfomaniakCoreDB
import InfomaniakDI
import OSLog
import Realm
import RealmSwift

Expand All @@ -41,15 +42,20 @@ extension MailCoreRealmAccessible {
let realm = try Realm(configuration: realmConfiguration)
realm.refresh()
return realm
} catch let error as RLMError
where error.code == .fail && (error.userInfo[RLMErrorCodeNameKey] as? String) == "InvalidSchemaVersion" {
} catch let error as RLMError where error.code == .fail || error.code == .schemaMismatch {
// If scheme version is invalid simply delete the current realm and retry with a new one
// (we still report the error just in case because it shouldn't happen in production)
Logging.reportRealmOpeningError(error, realmConfiguration: realmConfiguration, afterRetry: !canRetry)

@InjectService var realmManager: RealmManageable
realmManager.deleteFiles(for: realmConfiguration)

#if DEBUG
Logger.general.error("Realm files will be deleted, you can resume the app with the debugger")
// This will force the execution to breakpoint, to give a chance to the dev for debugging
raise(SIGINT)
#endif

return getRealm(canRetry: false)
} catch {
Logging.reportRealmOpeningError(error, realmConfiguration: realmConfiguration, afterRetry: !canRetry)
Expand Down
Loading