18
18
package com.infomaniak.mail.data.cache
19
19
20
20
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
21
24
import io.realm.kotlin.migration.AutomaticSchemaMigration
22
25
import io.realm.kotlin.migration.AutomaticSchemaMigration.MigrationContext
23
26
@@ -29,6 +32,7 @@ val USER_INFO_MIGRATION = AutomaticSchemaMigration { migrationContext ->
29
32
val MAILBOX_INFO_MIGRATION = AutomaticSchemaMigration { migrationContext ->
30
33
SentryDebug .addMigrationBreadcrumb(migrationContext)
31
34
migrationContext.deleteRealmFromFirstMigration()
35
+ migrationContext.keepDefaultValuesAfterSixthMigration()
32
36
}
33
37
34
38
val MAILBOX_CONTENT_MIGRATION = AutomaticSchemaMigration { migrationContext ->
@@ -40,3 +44,32 @@ val MAILBOX_CONTENT_MIGRATION = AutomaticSchemaMigration { migrationContext ->
40
44
private fun MigrationContext.deleteRealmFromFirstMigration () {
41
45
if (oldRealm.schemaVersion() < 1L ) newRealm.deleteAll()
42
46
}
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