Split code into several methods

This commit is contained in:
Benoit Marty 2020-01-03 14:25:19 +01:00
parent c2e7e33050
commit 160927e7b5

View file

@ -31,51 +31,55 @@ internal object AuthRealmMigration : RealmMigration {
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) { override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
Timber.d("Migrating Auth Realm from $oldVersion to $newVersion") Timber.d("Migrating Auth Realm from $oldVersion to $newVersion")
if (oldVersion <= 0) { if (oldVersion <= 0) migrateTo1(realm)
Timber.d("Step 0 -> 1") if (oldVersion <= 1) migrateTo2(realm)
Timber.d("Create PendingSessionEntity") if (oldVersion <= 2) migrateTo3(realm)
}
realm.schema.create("PendingSessionEntity") private fun migrateTo1(realm: DynamicRealm) {
.addField(PendingSessionEntityFields.HOME_SERVER_CONNECTION_CONFIG_JSON, String::class.java) Timber.d("Step 0 -> 1")
.setRequired(PendingSessionEntityFields.HOME_SERVER_CONNECTION_CONFIG_JSON, true) Timber.d("Create PendingSessionEntity")
.addField(PendingSessionEntityFields.CLIENT_SECRET, String::class.java)
.setRequired(PendingSessionEntityFields.CLIENT_SECRET, true)
.addField(PendingSessionEntityFields.SEND_ATTEMPT, Integer::class.java)
.setRequired(PendingSessionEntityFields.SEND_ATTEMPT, true)
.addField(PendingSessionEntityFields.RESET_PASSWORD_DATA_JSON, String::class.java)
.addField(PendingSessionEntityFields.CURRENT_SESSION, String::class.java)
.addField(PendingSessionEntityFields.IS_REGISTRATION_STARTED, Boolean::class.java)
.addField(PendingSessionEntityFields.CURRENT_THREE_PID_DATA_JSON, String::class.java)
}
if (oldVersion <= 1) { realm.schema.create("PendingSessionEntity")
Timber.d("Step 1 -> 2") .addField(PendingSessionEntityFields.HOME_SERVER_CONNECTION_CONFIG_JSON, String::class.java)
Timber.d("Add boolean isTokenValid in SessionParamsEntity, with value true") .setRequired(PendingSessionEntityFields.HOME_SERVER_CONNECTION_CONFIG_JSON, true)
.addField(PendingSessionEntityFields.CLIENT_SECRET, String::class.java)
.setRequired(PendingSessionEntityFields.CLIENT_SECRET, true)
.addField(PendingSessionEntityFields.SEND_ATTEMPT, Integer::class.java)
.setRequired(PendingSessionEntityFields.SEND_ATTEMPT, true)
.addField(PendingSessionEntityFields.RESET_PASSWORD_DATA_JSON, String::class.java)
.addField(PendingSessionEntityFields.CURRENT_SESSION, String::class.java)
.addField(PendingSessionEntityFields.IS_REGISTRATION_STARTED, Boolean::class.java)
.addField(PendingSessionEntityFields.CURRENT_THREE_PID_DATA_JSON, String::class.java)
}
realm.schema.get("SessionParamsEntity") private fun migrateTo2(realm: DynamicRealm) {
?.addField(SessionParamsEntityFields.IS_TOKEN_VALID, Boolean::class.java) Timber.d("Step 1 -> 2")
?.transform { it.set(SessionParamsEntityFields.IS_TOKEN_VALID, true) } Timber.d("Add boolean isTokenValid in SessionParamsEntity, with value true")
}
if (oldVersion <= 2) { realm.schema.get("SessionParamsEntity")
Timber.d("Step 2 -> 3") ?.addField(SessionParamsEntityFields.IS_TOKEN_VALID, Boolean::class.java)
Timber.d("Update SessionParamsEntity primary key, to allow several sessions with the same userId") ?.transform { it.set(SessionParamsEntityFields.IS_TOKEN_VALID, true) }
}
realm.schema.get("SessionParamsEntity") private fun migrateTo3(realm: DynamicRealm) {
?.removePrimaryKey() Timber.d("Step 2 -> 3")
?.addField(SessionParamsEntityFields.SESSION_ID, String::class.java) Timber.d("Update SessionParamsEntity primary key, to allow several sessions with the same userId")
?.setRequired(SessionParamsEntityFields.SESSION_ID, true)
?.transform {
val userId = it.getString(SessionParamsEntityFields.USER_ID)
val credentialsJson = it.getString(SessionParamsEntityFields.CREDENTIALS_JSON)
val credentials = MoshiProvider.providesMoshi() realm.schema.get("SessionParamsEntity")
.adapter(Credentials::class.java) ?.removePrimaryKey()
.fromJson(credentialsJson) ?.addField(SessionParamsEntityFields.SESSION_ID, String::class.java)
?.setRequired(SessionParamsEntityFields.SESSION_ID, true)
?.transform {
val userId = it.getString(SessionParamsEntityFields.USER_ID)
val credentialsJson = it.getString(SessionParamsEntityFields.CREDENTIALS_JSON)
it.set(SessionParamsEntityFields.SESSION_ID, createSessionId(userId, credentials?.deviceId)) val credentials = MoshiProvider.providesMoshi()
} .adapter(Credentials::class.java)
?.addPrimaryKey(SessionParamsEntityFields.SESSION_ID) .fromJson(credentialsJson)
}
it.set(SessionParamsEntityFields.SESSION_ID, createSessionId(userId, credentials?.deviceId))
}
?.addPrimaryKey(SessionParamsEntityFields.SESSION_ID)
} }
} }