From 9c064b95cc78917185ddc17858808c3d33366614 Mon Sep 17 00:00:00 2001 From: Kevin Boulongne Date: Thu, 17 Oct 2024 13:03:35 +0200 Subject: [PATCH 1/2] feat: Delete Realm when migration is needed --- .../multiplatform_swisstransfer/database/RealmProvider.kt | 3 +++ 1 file changed, 3 insertions(+) diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/RealmProvider.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/RealmProvider.kt index 18ef60e2..71f0dff4 100644 --- a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/RealmProvider.kt +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/RealmProvider.kt @@ -59,18 +59,21 @@ class RealmProvider(private val loadDataInMemory: Boolean = false) { private val realmAppSettingsConfiguration = RealmConfiguration .Builder(schema = setOf(AppSettingsDB::class)) .name("AppSettings") + .deleteRealmIfMigrationNeeded() // TODO: Remove before going to production ! .loadDataInMemoryIfNeeded() .build() private val realmUploadDBConfiguration = RealmConfiguration .Builder(schema = setOf(UploadDB::class, UploadContainerDB::class, UploadFileDB::class)) .name("Uploads") + .deleteRealmIfMigrationNeeded() // TODO: Remove before going to production ! .loadDataInMemoryIfNeeded() .build() private fun realmTransfersConfiguration(userId: Int) = RealmConfiguration .Builder(schema = setOf(TransferDB::class, ContainerDB::class, FileDB::class)) .name(transferRealmName(userId)) + .deleteRealmIfMigrationNeeded() // TODO: Remove before going to production ! .loadDataInMemoryIfNeeded() .build() From fe95d30844530cb9700ebe7a66b71c0df7a6601f Mon Sep 17 00:00:00 2001 From: Vincent TE Date: Thu, 17 Oct 2024 14:36:24 +0200 Subject: [PATCH 2/2] Don't drop DB if we're in memory mode --- .../database/RealmProvider.kt | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/RealmProvider.kt b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/RealmProvider.kt index 71f0dff4..7dbe6ed4 100644 --- a/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/RealmProvider.kt +++ b/STDatabase/src/commonMain/kotlin/com/infomaniak/multiplatform_swisstransfer/database/RealmProvider.kt @@ -59,21 +59,21 @@ class RealmProvider(private val loadDataInMemory: Boolean = false) { private val realmAppSettingsConfiguration = RealmConfiguration .Builder(schema = setOf(AppSettingsDB::class)) .name("AppSettings") - .deleteRealmIfMigrationNeeded() // TODO: Remove before going to production ! + .deleteRealmDataIfNeeded() // TODO: Remove before going to production ! .loadDataInMemoryIfNeeded() .build() private val realmUploadDBConfiguration = RealmConfiguration .Builder(schema = setOf(UploadDB::class, UploadContainerDB::class, UploadFileDB::class)) .name("Uploads") - .deleteRealmIfMigrationNeeded() // TODO: Remove before going to production ! + .deleteRealmDataIfNeeded() // TODO: Remove before going to production ! .loadDataInMemoryIfNeeded() .build() private fun realmTransfersConfiguration(userId: Int) = RealmConfiguration .Builder(schema = setOf(TransferDB::class, ContainerDB::class, FileDB::class)) .name(transferRealmName(userId)) - .deleteRealmIfMigrationNeeded() // TODO: Remove before going to production ! + .deleteRealmDataIfNeeded() // TODO: Remove before going to production ! .loadDataInMemoryIfNeeded() .build() @@ -82,4 +82,8 @@ class RealmProvider(private val loadDataInMemory: Boolean = false) { private fun RealmConfiguration.Builder.loadDataInMemoryIfNeeded(): RealmConfiguration.Builder { return apply { if (loadDataInMemory) inMemory() } } + + private fun RealmConfiguration.Builder.deleteRealmDataIfNeeded(): RealmConfiguration.Builder { + return apply { if (!loadDataInMemory) deleteRealmIfMigrationNeeded() } + } }