Skip to content

Commit 3f8cbd1

Browse files
committed
Create a parent MatrixRealmMigration and improve the logs.
1 parent ae8f977 commit 3f8cbd1

File tree

6 files changed

+73
-43
lines changed

6 files changed

+73
-43
lines changed

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/db/AuthRealmMigration.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,28 +17,26 @@
1717
package org.matrix.android.sdk.internal.auth.db
1818

1919
import io.realm.DynamicRealm
20-
import io.realm.RealmMigration
2120
import org.matrix.android.sdk.internal.auth.db.migration.MigrateAuthTo001
2221
import org.matrix.android.sdk.internal.auth.db.migration.MigrateAuthTo002
2322
import org.matrix.android.sdk.internal.auth.db.migration.MigrateAuthTo003
2423
import org.matrix.android.sdk.internal.auth.db.migration.MigrateAuthTo004
2524
import org.matrix.android.sdk.internal.auth.db.migration.MigrateAuthTo005
26-
import timber.log.Timber
25+
import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration
2726
import javax.inject.Inject
2827

29-
internal class AuthRealmMigration @Inject constructor() : RealmMigration {
28+
internal class AuthRealmMigration @Inject constructor() : MatrixRealmMigration(
29+
dbName = "Auth",
30+
schemaVersion = 5L,
31+
) {
3032
/**
3133
* Forces all AuthRealmMigration instances to be equal.
3234
* Avoids Realm throwing when multiple instances of the migration are set.
3335
*/
3436
override fun equals(other: Any?) = other is AuthRealmMigration
3537
override fun hashCode() = 4000
3638

37-
val schemaVersion = 5L
38-
39-
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
40-
Timber.d("Migrating Auth Realm from $oldVersion to $newVersion")
41-
39+
override fun doMigrate(realm: DynamicRealm, oldVersion: Long) {
4240
if (oldVersion < 1) MigrateAuthTo001(realm).perform()
4341
if (oldVersion < 2) MigrateAuthTo002(realm).perform()
4442
if (oldVersion < 3) MigrateAuthTo003(realm).perform()

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStoreMigration.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.matrix.android.sdk.internal.crypto.store.db
1818

1919
import io.realm.DynamicRealm
20-
import io.realm.RealmMigration
2120
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo001Legacy
2221
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo002Legacy
2322
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo003RiotX
@@ -35,28 +34,30 @@ import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo
3534
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo015
3635
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo016
3736
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo017
37+
import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration
3838
import org.matrix.android.sdk.internal.util.time.Clock
39-
import timber.log.Timber
4039
import javax.inject.Inject
4140

41+
/**
42+
* Schema version history:
43+
* 0, 1, 2: legacy Riot-Android
44+
* 3: migrate to RiotX schema
45+
* 4, 5, 6, 7, 8, 9: migrations from RiotX (which was previously 1, 2, 3, 4, 5, 6)
46+
*/
4247
internal class RealmCryptoStoreMigration @Inject constructor(
4348
private val clock: Clock,
44-
) : RealmMigration {
49+
) : MatrixRealmMigration(
50+
dbName = "Crypto",
51+
schemaVersion = 17L,
52+
) {
4553
/**
4654
* Forces all RealmCryptoStoreMigration instances to be equal.
4755
* Avoids Realm throwing when multiple instances of the migration are set.
4856
*/
4957
override fun equals(other: Any?) = other is RealmCryptoStoreMigration
5058
override fun hashCode() = 5000
5159

52-
// 0, 1, 2: legacy Riot-Android
53-
// 3: migrate to RiotX schema
54-
// 4, 5, 6, 7, 8, 9: migrations from RiotX (which was previously 1, 2, 3, 4, 5, 6)
55-
val schemaVersion = 17L
56-
57-
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
58-
Timber.d("Migrating Realm Crypto from $oldVersion to $newVersion")
59-
60+
override fun doMigrate(realm: DynamicRealm, oldVersion: Long) {
6061
if (oldVersion < 1) MigrateCryptoTo001Legacy(realm).perform()
6162
if (oldVersion < 2) MigrateCryptoTo002Legacy(realm).perform()
6263
if (oldVersion < 3) MigrateCryptoTo003RiotX(realm).perform()

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.matrix.android.sdk.internal.database
1818

1919
import io.realm.DynamicRealm
20-
import io.realm.RealmMigration
2120
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo001
2221
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo002
2322
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo003
@@ -51,24 +50,23 @@ import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo030
5150
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo031
5251
import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo032
5352
import org.matrix.android.sdk.internal.util.Normalizer
54-
import timber.log.Timber
53+
import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration
5554
import javax.inject.Inject
5655

5756
internal class RealmSessionStoreMigration @Inject constructor(
5857
private val normalizer: Normalizer
59-
) : RealmMigration {
58+
) : MatrixRealmMigration(
59+
dbName = "Session",
60+
schemaVersion = 32L,
61+
) {
6062
/**
6163
* Forces all RealmSessionStoreMigration instances to be equal.
6264
* Avoids Realm throwing when multiple instances of the migration are set.
6365
*/
6466
override fun equals(other: Any?) = other is RealmSessionStoreMigration
6567
override fun hashCode() = 1000
6668

67-
val schemaVersion = 32L
68-
69-
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
70-
Timber.d("Migrating Realm Session from $oldVersion to $newVersion")
71-
69+
override fun doMigrate(realm: DynamicRealm, oldVersion: Long) {
7270
if (oldVersion < 1) MigrateSessionTo001(realm).perform()
7371
if (oldVersion < 2) MigrateSessionTo002(realm).perform()
7472
if (oldVersion < 3) MigrateSessionTo003(realm).perform()

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/raw/GlobalRealmMigration.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,22 @@
1717
package org.matrix.android.sdk.internal.raw
1818

1919
import io.realm.DynamicRealm
20-
import io.realm.RealmMigration
2120
import org.matrix.android.sdk.internal.raw.migration.MigrateGlobalTo001
22-
import timber.log.Timber
21+
import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration
2322
import javax.inject.Inject
2423

25-
internal class GlobalRealmMigration @Inject constructor() : RealmMigration {
24+
internal class GlobalRealmMigration @Inject constructor() : MatrixRealmMigration(
25+
dbName = "Global",
26+
schemaVersion = 1L,
27+
) {
2628
/**
2729
* Forces all GlobalRealmMigration instances to be equal.
2830
* Avoids Realm throwing when multiple instances of the migration are set.
2931
*/
3032
override fun equals(other: Any?) = other is GlobalRealmMigration
3133
override fun hashCode() = 2000
3234

33-
val schemaVersion = 1L
34-
35-
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
36-
Timber.d("Migrating Global Realm from $oldVersion to $newVersion")
37-
35+
override fun doMigrate(realm: DynamicRealm, oldVersion: Long) {
3836
if (oldVersion < 1) MigrateGlobalTo001(realm).perform()
3937
}
4038
}

matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/identity/db/RealmIdentityStoreMigration.kt

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,22 @@
1717
package org.matrix.android.sdk.internal.session.identity.db
1818

1919
import io.realm.DynamicRealm
20-
import io.realm.RealmMigration
2120
import org.matrix.android.sdk.internal.session.identity.db.migration.MigrateIdentityTo001
22-
import timber.log.Timber
21+
import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration
2322
import javax.inject.Inject
2423

25-
internal class RealmIdentityStoreMigration @Inject constructor() : RealmMigration {
24+
internal class RealmIdentityStoreMigration @Inject constructor() : MatrixRealmMigration(
25+
dbName = "Identity",
26+
schemaVersion = 1L,
27+
) {
2628
/**
2729
* Forces all RealmIdentityStoreMigration instances to be equal.
2830
* Avoids Realm throwing when multiple instances of the migration are set.
2931
*/
3032
override fun equals(other: Any?) = other is RealmIdentityStoreMigration
3133
override fun hashCode() = 3000
3234

33-
val schemaVersion = 1L
34-
35-
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
36-
Timber.d("Migrating Realm Identity from $oldVersion to $newVersion")
37-
35+
override fun doMigrate(realm: DynamicRealm, oldVersion: Long) {
3836
if (oldVersion < 1) MigrateIdentityTo001(realm).perform()
3937
}
4038
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright (c) 2022 The Matrix.org Foundation C.I.C.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.matrix.android.sdk.internal.util.database
18+
19+
import io.realm.DynamicRealm
20+
import io.realm.RealmMigration
21+
import timber.log.Timber
22+
import kotlin.system.measureTimeMillis
23+
24+
internal abstract class MatrixRealmMigration(
25+
private val dbName: String,
26+
val schemaVersion: Long,
27+
) : RealmMigration {
28+
final override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
29+
Timber.d("Migrating Realm $dbName from $oldVersion to $newVersion")
30+
val duration = measureTimeMillis {
31+
doMigrate(realm, oldVersion)
32+
}
33+
Timber.d("Migrating Realm $dbName from $oldVersion to $newVersion took $duration ms.")
34+
}
35+
36+
abstract fun doMigrate(realm: DynamicRealm, oldVersion: Long)
37+
}

0 commit comments

Comments
 (0)