Ensure Realm migration does not crash

Fixes #8962
This commit is contained in:
Benoit Marty 2024-12-20 09:32:41 +01:00
parent 9dfc1f1953
commit 324ea9502e
2 changed files with 22 additions and 14 deletions

View file

@ -17,6 +17,7 @@
package org.matrix.android.sdk.internal.crypto.store.db.migration package org.matrix.android.sdk.internal.crypto.store.db.migration
import io.realm.DynamicRealm import io.realm.DynamicRealm
import org.matrix.android.sdk.internal.extensions.safeRemove
import org.matrix.android.sdk.internal.util.database.RealmMigrator import org.matrix.android.sdk.internal.util.database.RealmMigrator
internal class MigrateCryptoTo024(realm: DynamicRealm) : RealmMigrator(realm, 24) { internal class MigrateCryptoTo024(realm: DynamicRealm) : RealmMigrator(realm, 24) {
@ -32,20 +33,20 @@ internal class MigrateCryptoTo024(realm: DynamicRealm) : RealmMigrator(realm, 24
get("CryptoRoomEntity")?.removeField("outboundSessionInfo") get("CryptoRoomEntity")?.removeField("outboundSessionInfo")
// Warning: order is important, first remove classes that depends on others. // Warning: order is important, first remove classes that depends on others.
remove("UserEntity") safeRemove("UserEntity")
remove("DeviceInfoEntity") safeRemove("DeviceInfoEntity")
remove("CrossSigningInfoEntity") safeRemove("CrossSigningInfoEntity")
remove("KeyInfoEntity") safeRemove("KeyInfoEntity")
remove("TrustLevelEntity") safeRemove("TrustLevelEntity")
remove("KeysBackupDataEntity") safeRemove("KeysBackupDataEntity")
remove("OlmInboundGroupSessionEntity") safeRemove("OlmInboundGroupSessionEntity")
remove("OlmSessionEntity") safeRemove("OlmSessionEntity")
remove("AuditTrailEntity") safeRemove("AuditTrailEntity")
remove("OutgoingKeyRequestEntity") safeRemove("OutgoingKeyRequestEntity")
remove("KeyRequestReplyEntity") safeRemove("KeyRequestReplyEntity")
remove("WithHeldSessionEntity") safeRemove("WithHeldSessionEntity")
remove("SharedSessionEntity") safeRemove("SharedSessionEntity")
remove("OutboundGroupSessionInfoEntity") safeRemove("OutboundGroupSessionInfoEntity")
} }
} }
} }

View file

@ -19,6 +19,7 @@ package org.matrix.android.sdk.internal.extensions
import io.realm.RealmList import io.realm.RealmList
import io.realm.RealmObject import io.realm.RealmObject
import io.realm.RealmObjectSchema import io.realm.RealmObjectSchema
import io.realm.RealmSchema
import org.matrix.android.sdk.internal.database.model.HomeServerCapabilitiesEntityFields import org.matrix.android.sdk.internal.database.model.HomeServerCapabilitiesEntityFields
import org.matrix.android.sdk.internal.util.fatalError import org.matrix.android.sdk.internal.util.fatalError
@ -52,3 +53,9 @@ internal fun RealmObjectSchema?.forceRefreshOfHomeServerCapabilities(): RealmObj
obj.setLong(HomeServerCapabilitiesEntityFields.LAST_UPDATED_TIMESTAMP, 0) obj.setLong(HomeServerCapabilitiesEntityFields.LAST_UPDATED_TIMESTAMP, 0)
} }
} }
internal fun RealmSchema.safeRemove(className: String) {
if (get(className) != null) {
remove(className)
}
}