From 324ea9502eef51fbf60491f861fe951054afd957 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Fri, 20 Dec 2024 09:32:41 +0100 Subject: [PATCH] Ensure Realm migration does not crash Fixes #8962 --- .../store/db/migration/MigrateCryptoTo024.kt | 29 ++++++++++--------- .../internal/extensions/RealmExtensions.kt | 7 +++++ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/migration/MigrateCryptoTo024.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/migration/MigrateCryptoTo024.kt index 9a87eb4741..82498a5112 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/migration/MigrateCryptoTo024.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/migration/MigrateCryptoTo024.kt @@ -17,6 +17,7 @@ package org.matrix.android.sdk.internal.crypto.store.db.migration import io.realm.DynamicRealm +import org.matrix.android.sdk.internal.extensions.safeRemove import org.matrix.android.sdk.internal.util.database.RealmMigrator internal class MigrateCryptoTo024(realm: DynamicRealm) : RealmMigrator(realm, 24) { @@ -32,20 +33,20 @@ internal class MigrateCryptoTo024(realm: DynamicRealm) : RealmMigrator(realm, 24 get("CryptoRoomEntity")?.removeField("outboundSessionInfo") // Warning: order is important, first remove classes that depends on others. - remove("UserEntity") - remove("DeviceInfoEntity") - remove("CrossSigningInfoEntity") - remove("KeyInfoEntity") - remove("TrustLevelEntity") - remove("KeysBackupDataEntity") - remove("OlmInboundGroupSessionEntity") - remove("OlmSessionEntity") - remove("AuditTrailEntity") - remove("OutgoingKeyRequestEntity") - remove("KeyRequestReplyEntity") - remove("WithHeldSessionEntity") - remove("SharedSessionEntity") - remove("OutboundGroupSessionInfoEntity") + safeRemove("UserEntity") + safeRemove("DeviceInfoEntity") + safeRemove("CrossSigningInfoEntity") + safeRemove("KeyInfoEntity") + safeRemove("TrustLevelEntity") + safeRemove("KeysBackupDataEntity") + safeRemove("OlmInboundGroupSessionEntity") + safeRemove("OlmSessionEntity") + safeRemove("AuditTrailEntity") + safeRemove("OutgoingKeyRequestEntity") + safeRemove("KeyRequestReplyEntity") + safeRemove("WithHeldSessionEntity") + safeRemove("SharedSessionEntity") + safeRemove("OutboundGroupSessionInfoEntity") } } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/extensions/RealmExtensions.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/extensions/RealmExtensions.kt index c6ea2bc7bd..9932c14177 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/extensions/RealmExtensions.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/extensions/RealmExtensions.kt @@ -19,6 +19,7 @@ package org.matrix.android.sdk.internal.extensions import io.realm.RealmList import io.realm.RealmObject import io.realm.RealmObjectSchema +import io.realm.RealmSchema import org.matrix.android.sdk.internal.database.model.HomeServerCapabilitiesEntityFields import org.matrix.android.sdk.internal.util.fatalError @@ -52,3 +53,9 @@ internal fun RealmObjectSchema?.forceRefreshOfHomeServerCapabilities(): RealmObj obj.setLong(HomeServerCapabilitiesEntityFields.LAST_UPDATED_TIMESTAMP, 0) } } + +internal fun RealmSchema.safeRemove(className: String) { + if (get(className) != null) { + remove(className) + } +}