Adds AuthTo005 realm migration

This commit is contained in:
ericdecanini 2022-03-03 13:14:49 +01:00
parent d33081c349
commit dffd568e14
5 changed files with 140 additions and 2 deletions

View file

@ -22,6 +22,7 @@ import org.matrix.android.sdk.internal.auth.db.migration.MigrateAuthTo001
import org.matrix.android.sdk.internal.auth.db.migration.MigrateAuthTo002
import org.matrix.android.sdk.internal.auth.db.migration.MigrateAuthTo003
import org.matrix.android.sdk.internal.auth.db.migration.MigrateAuthTo004
import org.matrix.android.sdk.internal.auth.db.migration.MigrateAuthTo005
import timber.log.Timber
import javax.inject.Inject
@ -33,7 +34,7 @@ internal class AuthRealmMigration @Inject constructor() : RealmMigration {
override fun equals(other: Any?) = other is AuthRealmMigration
override fun hashCode() = 4000
val schemaVersion = 4L
val schemaVersion = 5L
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
Timber.d("Migrating Auth Realm from $oldVersion to $newVersion")
@ -42,5 +43,6 @@ internal class AuthRealmMigration @Inject constructor() : RealmMigration {
if (oldVersion < 2) MigrateAuthTo002(realm).perform()
if (oldVersion < 3) MigrateAuthTo003(realm).perform()
if (oldVersion < 4) MigrateAuthTo004(realm).perform()
if (oldVersion < 5) MigrateAuthTo005(realm).perform()
}
}

View file

@ -27,5 +27,5 @@ internal open class SessionParamsEntity(
// Set to false when the token is invalid and the user has been soft logged out
// In case of hard logout, this object is deleted from DB
var isTokenValid: Boolean = true,
var loginType: String,
var loginType: String = "",
) : RealmObject()

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.internal.auth.db.migration
import io.realm.DynamicRealm
import org.matrix.android.sdk.internal.auth.db.SessionParamsEntityFields
import org.matrix.android.sdk.internal.auth.login.LoginType
import org.matrix.android.sdk.internal.util.database.RealmMigrator
import timber.log.Timber
class MigrateAuthTo005(realm: DynamicRealm) : RealmMigrator(realm, 5) {
override fun doMigrate(realm: DynamicRealm) {
Timber.d("Update SessionParamsEntity to add LoginType")
realm.schema.get("SessionParamsEntity")
?.addField(SessionParamsEntityFields.LOGIN_TYPE, String::class.java)
?.transform { it.set(SessionParamsEntityFields.LOGIN_TYPE, LoginType.UNKNOWN.value) }
}
}

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.internal.auth.db.migration
import org.junit.Test
import org.matrix.android.sdk.test.fakes.Fake005MigrationRealm
class MigrateAuthTo005Test {
private val fakeRealm = Fake005MigrationRealm()
private val migrator = MigrateAuthTo005(fakeRealm.instance)
@Test
fun `when doMigrate, then LoginType field added`() {
migrator.doMigrate(fakeRealm.instance)
fakeRealm.verifyLoginTypeAdded()
}
}

View file

@ -0,0 +1,68 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.matrix.android.sdk.test.fakes
import io.mockk.every
import io.mockk.mockk
import io.mockk.slot
import io.mockk.verify
import io.mockk.verifyOrder
import io.realm.DynamicRealm
import io.realm.DynamicRealmObject
import io.realm.RealmObjectSchema
import io.realm.RealmSchema
import org.matrix.android.sdk.internal.auth.db.SessionParamsEntityFields
import org.matrix.android.sdk.internal.auth.login.LoginType
class Fake005MigrationRealm {
val instance: DynamicRealm = mockk()
private val schema: RealmSchema = mockk()
private val objectSchema: RealmObjectSchema = mockk()
init {
every { instance.schema } returns schema
every { schema.get("SessionParamsEntity") } returns objectSchema
every { objectSchema.addField(any(), any()) } returns objectSchema
every { objectSchema.transform(any()) } returns objectSchema
}
fun verifyLoginTypeAdded() {
transformFunctionSlot.clear()
verifyLoginTypeFieldAddedAndTransformed()
verifyTransformationSetsUnknownLoginType()
}
private fun verifyLoginTypeFieldAddedAndTransformed() {
verifyOrder {
objectSchema["SessionParamsEntity"]
objectSchema.addField(SessionParamsEntityFields.LOGIN_TYPE, String::class.java)
objectSchema.transform(capture(transformFunctionSlot))
}
}
private fun verifyTransformationSetsUnknownLoginType() {
val dynamicRealmObject: DynamicRealmObject = mockk()
transformFunctionSlot.captured.invoke(dynamicRealmObject)
verify { dynamicRealmObject.set(SessionParamsEntityFields.LOGIN_TYPE, LoginType.UNKNOWN.value) }
}
companion object {
private val transformFunctionSlot = slot<(DynamicRealmObject) -> Unit>()
}
}