Skip to content

Commit

Permalink
fix(DriveMigration): Add drive migration to avoid crash when switchin…
Browse files Browse the repository at this point in the history
…g users
  • Loading branch information
FabianDevel committed Feb 24, 2025
1 parent 2393ad0 commit fde2c48
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,10 @@ object DriveInfosController {

private const val DB_NAME = "DrivesInfos.realm"

private val realmConfiguration = RealmConfiguration.Builder().name(DB_NAME)
.deleteRealmIfMigrationNeeded()
private val realmConfiguration = RealmConfiguration.Builder()
.name(DB_NAME)
.schemaVersion(DriveMigration.DB_VERSION) // Must be bumped when the schema changes
.migration(DriveMigration())
.modules(RealmModules.DriveFilesModule())
.build()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Infomaniak kDrive - Android
* Copyright (C) 2025 Infomaniak Network SA
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.infomaniak.drive.data.cache

import io.realm.DynamicRealm
import io.realm.RealmMigration

class DriveMigration : RealmMigration {

override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
var oldVersionTemp = oldVersion

// DynamicRealm exposes an editable schema
val schema = realm.schema

// Migrated to version 1
if (oldVersionTemp == 0L) {

val driveQuotaSchema = schema.create("DriveQuota").apply {
addField("current", Int::class.java)
addField("max", Int::class.java)
isEmbedded = true
}

val driveQuotasSchema = schema.create("DriveQuotas").apply {
addRealmObjectField("dropbox", driveQuotaSchema)
addRealmObjectField("sharedLink", driveQuotaSchema)
isEmbedded = true
}

schema["Drive"]?.apply {
if (hasField("_packFunctionality")) removeField("_packFunctionality")
addRealmObjectField("_quotas", driveQuotasSchema)
}

oldVersionTemp++
}
}

companion object {
const val DB_VERSION = 1L // Must be bumped when the schema changes
}

}

0 comments on commit fde2c48

Please sign in to comment.