-
Notifications
You must be signed in to change notification settings - Fork 713
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5134 from vector-im/feature/bma/realmMigrations
Refactor realm migrations
- Loading branch information
Showing
56 changed files
with
2,254 additions
and
1,128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...droid/src/main/java/org/matrix/android/sdk/internal/auth/db/migration/MigrateAuthTo001.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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.PendingSessionEntityFields | ||
import org.matrix.android.sdk.internal.util.database.RealmMigrator | ||
import timber.log.Timber | ||
|
||
class MigrateAuthTo001(realm: DynamicRealm) : RealmMigrator(realm, 1) { | ||
|
||
override fun doMigrate(realm: DynamicRealm) { | ||
Timber.d("Create PendingSessionEntity") | ||
|
||
realm.schema.create("PendingSessionEntity") | ||
.addField(PendingSessionEntityFields.HOME_SERVER_CONNECTION_CONFIG_JSON, String::class.java) | ||
.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) | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
...droid/src/main/java/org/matrix/android/sdk/internal/auth/db/migration/MigrateAuthTo002.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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.util.database.RealmMigrator | ||
import timber.log.Timber | ||
|
||
class MigrateAuthTo002(realm: DynamicRealm) : RealmMigrator(realm, 2) { | ||
|
||
override fun doMigrate(realm: DynamicRealm) { | ||
Timber.d("Add boolean isTokenValid in SessionParamsEntity, with value true") | ||
|
||
realm.schema.get("SessionParamsEntity") | ||
?.addField(SessionParamsEntityFields.IS_TOKEN_VALID, Boolean::class.java) | ||
?.transform { it.set(SessionParamsEntityFields.IS_TOKEN_VALID, true) } | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
...droid/src/main/java/org/matrix/android/sdk/internal/auth/db/migration/MigrateAuthTo003.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* 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.api.auth.data.Credentials | ||
import org.matrix.android.sdk.api.auth.data.sessionId | ||
import org.matrix.android.sdk.internal.auth.db.SessionParamsEntityFields | ||
import org.matrix.android.sdk.internal.di.MoshiProvider | ||
import org.matrix.android.sdk.internal.util.database.RealmMigrator | ||
import timber.log.Timber | ||
|
||
class MigrateAuthTo003(realm: DynamicRealm) : RealmMigrator(realm, 3) { | ||
|
||
override fun doMigrate(realm: DynamicRealm) { | ||
Timber.d("Update SessionParamsEntity primary key, to allow several sessions with the same userId") | ||
|
||
realm.schema.get("SessionParamsEntity") | ||
?.removePrimaryKey() | ||
?.addField(SessionParamsEntityFields.SESSION_ID, String::class.java) | ||
?.setRequired(SessionParamsEntityFields.SESSION_ID, true) | ||
?.transform { | ||
val credentialsJson = it.getString(SessionParamsEntityFields.CREDENTIALS_JSON) | ||
|
||
val credentials = MoshiProvider.providesMoshi() | ||
.adapter(Credentials::class.java) | ||
.fromJson(credentialsJson) | ||
|
||
it.set(SessionParamsEntityFields.SESSION_ID, credentials!!.sessionId()) | ||
} | ||
?.addPrimaryKey(SessionParamsEntityFields.SESSION_ID) | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...droid/src/main/java/org/matrix/android/sdk/internal/auth/db/migration/MigrateAuthTo004.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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 android.net.Uri | ||
import io.realm.DynamicRealm | ||
import org.matrix.android.sdk.api.auth.data.HomeServerConnectionConfig | ||
import org.matrix.android.sdk.internal.auth.db.SessionParamsEntityFields | ||
import org.matrix.android.sdk.internal.di.MoshiProvider | ||
import org.matrix.android.sdk.internal.util.database.RealmMigrator | ||
import timber.log.Timber | ||
|
||
class MigrateAuthTo004(realm: DynamicRealm) : RealmMigrator(realm, 4) { | ||
|
||
override fun doMigrate(realm: DynamicRealm) { | ||
Timber.d("Update SessionParamsEntity to add HomeServerConnectionConfig.homeServerUriBase value") | ||
|
||
val adapter = MoshiProvider.providesMoshi() | ||
.adapter(HomeServerConnectionConfig::class.java) | ||
|
||
realm.schema.get("SessionParamsEntity") | ||
?.transform { | ||
val homeserverConnectionConfigJson = it.getString(SessionParamsEntityFields.HOME_SERVER_CONNECTION_CONFIG_JSON) | ||
|
||
val homeserverConnectionConfig = adapter | ||
.fromJson(homeserverConnectionConfigJson) | ||
|
||
val homeserverUrl = homeserverConnectionConfig?.homeServerUri?.toString() | ||
// Special case for matrix.org. Old session may use "https://matrix.org", newer one may use | ||
// "https://matrix-client.matrix.org". So fix that here | ||
val alteredHomeserverConnectionConfig = | ||
if (homeserverUrl == "https://matrix.org" || homeserverUrl == "https://matrix-client.matrix.org") { | ||
homeserverConnectionConfig.copy( | ||
homeServerUri = Uri.parse("https://matrix.org"), | ||
homeServerUriBase = Uri.parse("https://matrix-client.matrix.org") | ||
) | ||
} else { | ||
homeserverConnectionConfig | ||
} | ||
it.set(SessionParamsEntityFields.HOME_SERVER_CONNECTION_CONFIG_JSON, adapter.toJson(alteredHomeserverConnectionConfig)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.