mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-27 03:49:04 +03:00
Split code into several methods
This commit is contained in:
parent
c2e7e33050
commit
160927e7b5
1 changed files with 43 additions and 39 deletions
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue