Skip to content

Commit 098fa22

Browse files
fix: Add missing handcrafted migration to fix Realm bug
1 parent 2f2d4ec commit 098fa22

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

app/src/main/java/com/infomaniak/mail/data/cache/RealmMigrations.kt

+33
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
package com.infomaniak.mail.data.cache
1919

2020
import com.infomaniak.mail.utils.SentryDebug
21+
import io.realm.kotlin.dynamic.DynamicMutableRealmObject
22+
import io.realm.kotlin.dynamic.DynamicRealmObject
23+
import io.realm.kotlin.dynamic.getValue
2124
import io.realm.kotlin.migration.AutomaticSchemaMigration
2225
import io.realm.kotlin.migration.AutomaticSchemaMigration.MigrationContext
2326

@@ -29,6 +32,7 @@ val USER_INFO_MIGRATION = AutomaticSchemaMigration { migrationContext ->
2932
val MAILBOX_INFO_MIGRATION = AutomaticSchemaMigration { migrationContext ->
3033
SentryDebug.addMigrationBreadcrumb(migrationContext)
3134
migrationContext.deleteRealmFromFirstMigration()
35+
migrationContext.keepDefaultValuesAfterSixthMigration()
3236
}
3337

3438
val MAILBOX_CONTENT_MIGRATION = AutomaticSchemaMigration { migrationContext ->
@@ -40,3 +44,32 @@ val MAILBOX_CONTENT_MIGRATION = AutomaticSchemaMigration { migrationContext ->
4044
private fun MigrationContext.deleteRealmFromFirstMigration() {
4145
if (oldRealm.schemaVersion() < 1L) newRealm.deleteAll()
4246
}
47+
48+
/**
49+
* Migrate from version #6
50+
*
51+
* This whole migration needs to be done because of this issue :
52+
* https://github.com/realm/realm-swift/issues/1793
53+
*
54+
* Yes the issue is on the Realm-Swift repository, but all Realm projects are impacted.
55+
*
56+
* Documentation to handle manual migrations :
57+
* https://www.mongodb.com/docs/atlas/device-sdks/sdk/kotlin/realm-database/schemas/change-an-object-model/
58+
*/
59+
private fun MigrationContext.keepDefaultValuesAfterSixthMigration() {
60+
if (oldRealm.schemaVersion() <= 6L) {
61+
enumerate(className = "Mailbox") { oldObject: DynamicRealmObject, newObject: DynamicMutableRealmObject? ->
62+
newObject?.apply {
63+
64+
// Add property with default value
65+
set(propertyName = "_isValidInLdap", value = true)
66+
67+
// Rename property without losing its previous value
68+
set(propertyName = "_isLocked", value = oldObject.getValue<Boolean>(fieldName = "isLocked"))
69+
70+
// Rename property without losing its previous value
71+
set(propertyName = "hasValidPassword", value = oldObject.getValue<Boolean>(fieldName = "isPasswordValid"))
72+
}
73+
}
74+
}
75+
}

0 commit comments

Comments
 (0)