Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More DB migration log #6538

Merged
merged 3 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/6538.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Log durations of DB migration and migration steps.
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,26 @@
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.
*/
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) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

newVersion is never used, also I think it will be always equal to schemaVersion if it is necessary.

if (oldVersion < 1) MigrateAuthTo001(realm).perform()
if (oldVersion < 2) MigrateAuthTo002(realm).perform()
if (oldVersion < 3) MigrateAuthTo003(realm).perform()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -35,28 +34,30 @@ 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.
*/
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,24 +50,23 @@ 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.
*/
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,22 @@
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.
*/
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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,22 @@
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.
*/
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()
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ package org.matrix.android.sdk.internal.util.database
import io.realm.DynamicRealm
import io.realm.RealmObjectSchema
import timber.log.Timber
import kotlin.system.measureTimeMillis

internal abstract class RealmMigrator(
private val realm: DynamicRealm,
private val targetSchemaVersion: Int
) {
fun perform() {
Timber.d("Migrate ${realm.configuration.realmFileName} to $targetSchemaVersion")
doMigrate(realm)
val duration = measureTimeMillis {
doMigrate(realm)
}
Timber.d("Migrate ${realm.configuration.realmFileName} to $targetSchemaVersion took $duration ms.")
}

abstract fun doMigrate(realm: DynamicRealm)
Expand Down