diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/db/AuthRealmMigration.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/db/AuthRealmMigration.kt index 0bc7831f5c..c5b8eae3ff 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/db/AuthRealmMigration.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/auth/db/AuthRealmMigration.kt @@ -17,16 +17,18 @@ package org.matrix.android.sdk.internal.auth.db import io.realm.DynamicRealm -import io.realm.RealmMigration 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 org.matrix.android.sdk.internal.util.database.MatrixRealmMigration import javax.inject.Inject -internal class AuthRealmMigration @Inject constructor() : RealmMigration { +internal class AuthRealmMigration @Inject constructor() : MatrixRealmMigration( + dbName = "Auth", + schemaVersion = 5L, +) { /** * Forces all AuthRealmMigration instances to be equal. * Avoids Realm throwing when multiple instances of the migration are set. @@ -34,11 +36,7 @@ internal class AuthRealmMigration @Inject constructor() : RealmMigration { override fun equals(other: Any?) = other is AuthRealmMigration override fun hashCode() = 4000 - val schemaVersion = 5L - - override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) { - Timber.d("Migrating Auth Realm from $oldVersion to $newVersion") - + override fun doMigrate(realm: DynamicRealm, oldVersion: Long) { if (oldVersion < 1) MigrateAuthTo001(realm).perform() if (oldVersion < 2) MigrateAuthTo002(realm).perform() if (oldVersion < 3) MigrateAuthTo003(realm).perform() diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStoreMigration.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStoreMigration.kt index 4ca9d44f98..10f59e6429 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStoreMigration.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/crypto/store/db/RealmCryptoStoreMigration.kt @@ -17,7 +17,6 @@ package org.matrix.android.sdk.internal.crypto.store.db import io.realm.DynamicRealm -import io.realm.RealmMigration import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo001Legacy import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo002Legacy import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo003RiotX @@ -35,13 +34,22 @@ import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo015 import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo016 import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo017 +import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration import org.matrix.android.sdk.internal.util.time.Clock -import timber.log.Timber import javax.inject.Inject +/** + * Schema version history: + * 0, 1, 2: legacy Riot-Android + * 3: migrate to RiotX schema + * 4, 5, 6, 7, 8, 9: migrations from RiotX (which was previously 1, 2, 3, 4, 5, 6) + */ internal class RealmCryptoStoreMigration @Inject constructor( private val clock: Clock, -) : RealmMigration { +) : MatrixRealmMigration( + dbName = "Crypto", + schemaVersion = 17L, +) { /** * Forces all RealmCryptoStoreMigration instances to be equal. * Avoids Realm throwing when multiple instances of the migration are set. @@ -49,14 +57,7 @@ internal class RealmCryptoStoreMigration @Inject constructor( override fun equals(other: Any?) = other is RealmCryptoStoreMigration override fun hashCode() = 5000 - // 0, 1, 2: legacy Riot-Android - // 3: migrate to RiotX schema - // 4, 5, 6, 7, 8, 9: migrations from RiotX (which was previously 1, 2, 3, 4, 5, 6) - val schemaVersion = 17L - - override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) { - Timber.d("Migrating Realm Crypto from $oldVersion to $newVersion") - + override fun doMigrate(realm: DynamicRealm, oldVersion: Long) { if (oldVersion < 1) MigrateCryptoTo001Legacy(realm).perform() if (oldVersion < 2) MigrateCryptoTo002Legacy(realm).perform() if (oldVersion < 3) MigrateCryptoTo003RiotX(realm).perform() diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt index 9be1717f32..b54aec26b2 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/database/RealmSessionStoreMigration.kt @@ -17,7 +17,6 @@ package org.matrix.android.sdk.internal.database import io.realm.DynamicRealm -import io.realm.RealmMigration import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo001 import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo002 import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo003 @@ -51,12 +50,15 @@ import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo030 import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo031 import org.matrix.android.sdk.internal.database.migration.MigrateSessionTo032 import org.matrix.android.sdk.internal.util.Normalizer -import timber.log.Timber +import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration import javax.inject.Inject internal class RealmSessionStoreMigration @Inject constructor( private val normalizer: Normalizer -) : RealmMigration { +) : MatrixRealmMigration( + dbName = "Session", + schemaVersion = 32L, +) { /** * Forces all RealmSessionStoreMigration instances to be equal. * Avoids Realm throwing when multiple instances of the migration are set. @@ -64,11 +66,7 @@ internal class RealmSessionStoreMigration @Inject constructor( override fun equals(other: Any?) = other is RealmSessionStoreMigration override fun hashCode() = 1000 - val schemaVersion = 32L - - override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) { - Timber.d("Migrating Realm Session from $oldVersion to $newVersion") - + override fun doMigrate(realm: DynamicRealm, oldVersion: Long) { if (oldVersion < 1) MigrateSessionTo001(realm).perform() if (oldVersion < 2) MigrateSessionTo002(realm).perform() if (oldVersion < 3) MigrateSessionTo003(realm).perform() diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/raw/GlobalRealmMigration.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/raw/GlobalRealmMigration.kt index a9dfd47b5a..575afa8494 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/raw/GlobalRealmMigration.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/raw/GlobalRealmMigration.kt @@ -17,12 +17,14 @@ package org.matrix.android.sdk.internal.raw import io.realm.DynamicRealm -import io.realm.RealmMigration import org.matrix.android.sdk.internal.raw.migration.MigrateGlobalTo001 -import timber.log.Timber +import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration import javax.inject.Inject -internal class GlobalRealmMigration @Inject constructor() : RealmMigration { +internal class GlobalRealmMigration @Inject constructor() : MatrixRealmMigration( + dbName = "Global", + schemaVersion = 1L, +) { /** * Forces all GlobalRealmMigration instances to be equal. * Avoids Realm throwing when multiple instances of the migration are set. @@ -30,11 +32,7 @@ internal class GlobalRealmMigration @Inject constructor() : RealmMigration { override fun equals(other: Any?) = other is GlobalRealmMigration override fun hashCode() = 2000 - val schemaVersion = 1L - - override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) { - Timber.d("Migrating Global Realm from $oldVersion to $newVersion") - + override fun doMigrate(realm: DynamicRealm, oldVersion: Long) { if (oldVersion < 1) MigrateGlobalTo001(realm).perform() } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/identity/db/RealmIdentityStoreMigration.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/identity/db/RealmIdentityStoreMigration.kt index e731f9f347..4756b41f4c 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/identity/db/RealmIdentityStoreMigration.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/identity/db/RealmIdentityStoreMigration.kt @@ -17,12 +17,14 @@ package org.matrix.android.sdk.internal.session.identity.db import io.realm.DynamicRealm -import io.realm.RealmMigration import org.matrix.android.sdk.internal.session.identity.db.migration.MigrateIdentityTo001 -import timber.log.Timber +import org.matrix.android.sdk.internal.util.database.MatrixRealmMigration import javax.inject.Inject -internal class RealmIdentityStoreMigration @Inject constructor() : RealmMigration { +internal class RealmIdentityStoreMigration @Inject constructor() : MatrixRealmMigration( + dbName = "Identity", + schemaVersion = 1L, +) { /** * Forces all RealmIdentityStoreMigration instances to be equal. * Avoids Realm throwing when multiple instances of the migration are set. @@ -30,11 +32,7 @@ internal class RealmIdentityStoreMigration @Inject constructor() : RealmMigratio override fun equals(other: Any?) = other is RealmIdentityStoreMigration override fun hashCode() = 3000 - val schemaVersion = 1L - - override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) { - Timber.d("Migrating Realm Identity from $oldVersion to $newVersion") - + override fun doMigrate(realm: DynamicRealm, oldVersion: Long) { if (oldVersion < 1) MigrateIdentityTo001(realm).perform() } } diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/util/database/MatrixRealmMigration.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/util/database/MatrixRealmMigration.kt new file mode 100644 index 0000000000..4dff466de2 --- /dev/null +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/util/database/MatrixRealmMigration.kt @@ -0,0 +1,37 @@ +/* + * 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.util.database + +import io.realm.DynamicRealm +import io.realm.RealmMigration +import timber.log.Timber +import kotlin.system.measureTimeMillis + +internal abstract class MatrixRealmMigration( + private val dbName: String, + val schemaVersion: Long, +) : RealmMigration { + final override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) { + Timber.d("Migrating Realm $dbName from $oldVersion to $newVersion") + val duration = measureTimeMillis { + doMigrate(realm, oldVersion) + } + Timber.d("Migrating Realm $dbName from $oldVersion to $newVersion took $duration ms.") + } + + abstract fun doMigrate(realm: DynamicRealm, oldVersion: Long) +}