Realm migrations are not objects anymore

SchemaVersion is now a val
This commit is contained in:
Benoit Marty 2022-02-02 17:52:26 +01:00
parent 8a4ecf616a
commit eed4bf175f
11 changed files with 62 additions and 33 deletions

View File

@ -46,7 +46,9 @@ internal abstract class AuthModule {
@JvmStatic @JvmStatic
@Provides @Provides
@AuthDatabase @AuthDatabase
fun providesRealmConfiguration(context: Context, realmKeysUtils: RealmKeysUtils): RealmConfiguration { fun providesRealmConfiguration(context: Context,
realmKeysUtils: RealmKeysUtils,
authRealmMigration: AuthRealmMigration): RealmConfiguration {
val old = File(context.filesDir, "matrix-sdk-auth") val old = File(context.filesDir, "matrix-sdk-auth")
if (old.exists()) { if (old.exists()) {
old.renameTo(File(context.filesDir, "matrix-sdk-auth.realm")) old.renameTo(File(context.filesDir, "matrix-sdk-auth.realm"))
@ -58,8 +60,8 @@ internal abstract class AuthModule {
} }
.name("matrix-sdk-auth.realm") .name("matrix-sdk-auth.realm")
.modules(AuthRealmModule()) .modules(AuthRealmModule())
.schemaVersion(AuthRealmMigration.SCHEMA_VERSION) .schemaVersion(authRealmMigration.schemaVersion)
.migration(AuthRealmMigration) .migration(authRealmMigration)
.build() .build()
} }
} }

View File

@ -23,11 +23,17 @@ 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.MigrateAuthTo003
import org.matrix.android.sdk.internal.auth.db.migration.MigrateAuthTo004 import org.matrix.android.sdk.internal.auth.db.migration.MigrateAuthTo004
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
internal object AuthRealmMigration : RealmMigration { internal class AuthRealmMigration @Inject constructor() : RealmMigration {
/**
* 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
// Current schema version val schemaVersion = 4L
const val SCHEMA_VERSION = 4L
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")

View File

@ -112,7 +112,8 @@ internal abstract class CryptoModule {
@SessionScope @SessionScope
fun providesRealmConfiguration(@SessionFilesDirectory directory: File, fun providesRealmConfiguration(@SessionFilesDirectory directory: File,
@UserMd5 userMd5: String, @UserMd5 userMd5: String,
realmKeysUtils: RealmKeysUtils): RealmConfiguration { realmKeysUtils: RealmKeysUtils,
realmCryptoStoreMigration: RealmCryptoStoreMigration): RealmConfiguration {
return RealmConfiguration.Builder() return RealmConfiguration.Builder()
.directory(directory) .directory(directory)
.apply { .apply {
@ -121,8 +122,8 @@ internal abstract class CryptoModule {
.name("crypto_store.realm") .name("crypto_store.realm")
.modules(RealmCryptoStoreModule()) .modules(RealmCryptoStoreModule())
.allowWritesOnUiThread(true) .allowWritesOnUiThread(true)
.schemaVersion(RealmCryptoStoreMigration.CRYPTO_STORE_SCHEMA_VERSION) .schemaVersion(realmCryptoStoreMigration.schemaVersion)
.migration(RealmCryptoStoreMigration) .migration(realmCryptoStoreMigration)
.build() .build()
} }

View File

@ -33,16 +33,23 @@ import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo013 import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo013
import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo014 import org.matrix.android.sdk.internal.crypto.store.db.migration.MigrateCryptoTo014
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
internal object RealmCryptoStoreMigration : RealmMigration { internal class RealmCryptoStoreMigration @Inject constructor() : RealmMigration {
/**
* 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 // 0, 1, 2: legacy Riot-Android
// 3: migrate to RiotX schema // 3: migrate to RiotX schema
// 4, 5, 6, 7, 8, 9: migrations from RiotX (which was previously 1, 2, 3, 4, 5, 6) // 4, 5, 6, 7, 8, 9: migrations from RiotX (which was previously 1, 2, 3, 4, 5, 6)
const val CRYPTO_STORE_SCHEMA_VERSION = 14L val schemaVersion = 14L
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) { override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
Timber.v("Migrating Realm Crypto from $oldVersion to $newVersion") Timber.d("Migrating Realm Crypto from $oldVersion to $newVersion")
if (oldVersion < 1) MigrateCryptoTo001Legacy(realm).perform() if (oldVersion < 1) MigrateCryptoTo001Legacy(realm).perform()
if (oldVersion < 2) MigrateCryptoTo002Legacy(realm).perform() if (oldVersion < 2) MigrateCryptoTo002Legacy(realm).perform()

View File

@ -49,11 +49,6 @@ import javax.inject.Inject
internal class RealmSessionStoreMigration @Inject constructor( internal class RealmSessionStoreMigration @Inject constructor(
private val normalizer: Normalizer private val normalizer: Normalizer
) : RealmMigration { ) : RealmMigration {
companion object {
const val SESSION_STORE_SCHEMA_VERSION = 24L
}
/** /**
* Forces all RealmSessionStoreMigration instances to be equal * Forces all RealmSessionStoreMigration instances to be equal
* Avoids Realm throwing when multiple instances of the migration are set * Avoids Realm throwing when multiple instances of the migration are set
@ -61,8 +56,10 @@ internal class RealmSessionStoreMigration @Inject constructor(
override fun equals(other: Any?) = other is RealmSessionStoreMigration override fun equals(other: Any?) = other is RealmSessionStoreMigration
override fun hashCode() = 1000 override fun hashCode() = 1000
val schemaVersion = 24L
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) { override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
Timber.v("Migrating Realm Session from $oldVersion to $newVersion") Timber.d("Migrating Realm Session from $oldVersion to $newVersion")
if (oldVersion < 1) MigrateSessionTo001(realm).perform() if (oldVersion < 1) MigrateSessionTo001(realm).perform()
if (oldVersion < 2) MigrateSessionTo002(realm).perform() if (oldVersion < 2) MigrateSessionTo002(realm).perform()

View File

@ -71,7 +71,7 @@ internal class SessionRealmConfigurationFactory @Inject constructor(
} }
.allowWritesOnUiThread(true) .allowWritesOnUiThread(true)
.modules(SessionRealmModule()) .modules(SessionRealmModule())
.schemaVersion(RealmSessionStoreMigration.SESSION_STORE_SCHEMA_VERSION) .schemaVersion(realmSessionStoreMigration.schemaVersion)
.migration(realmSessionStoreMigration) .migration(realmSessionStoreMigration)
.build() .build()

View File

@ -42,7 +42,8 @@ import org.matrix.android.sdk.internal.legacy.riot.HomeServerConnectionConfig as
internal class DefaultLegacySessionImporter @Inject constructor( internal class DefaultLegacySessionImporter @Inject constructor(
private val context: Context, private val context: Context,
private val sessionParamsStore: SessionParamsStore, private val sessionParamsStore: SessionParamsStore,
private val realmKeysUtils: RealmKeysUtils private val realmKeysUtils: RealmKeysUtils,
private val realmCryptoStoreMigration: RealmCryptoStoreMigration
) : LegacySessionImporter { ) : LegacySessionImporter {
private val loginStorage = LoginStorage(context) private val loginStorage = LoginStorage(context)
@ -170,8 +171,8 @@ internal class DefaultLegacySessionImporter @Inject constructor(
.directory(File(context.filesDir, userMd5)) .directory(File(context.filesDir, userMd5))
.name("crypto_store.realm") .name("crypto_store.realm")
.modules(RealmCryptoStoreModule()) .modules(RealmCryptoStoreModule())
.schemaVersion(RealmCryptoStoreMigration.CRYPTO_STORE_SCHEMA_VERSION) .schemaVersion(realmCryptoStoreMigration.schemaVersion)
.migration(RealmCryptoStoreMigration) .migration(realmCryptoStoreMigration)
.build() .build()
Timber.d("Migration: copy DB to encrypted DB") Timber.d("Migration: copy DB to encrypted DB")

View File

@ -20,11 +20,17 @@ import io.realm.DynamicRealm
import io.realm.RealmMigration import io.realm.RealmMigration
import org.matrix.android.sdk.internal.raw.migration.MigrateGlobalTo001 import org.matrix.android.sdk.internal.raw.migration.MigrateGlobalTo001
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
internal object GlobalRealmMigration : RealmMigration { internal class GlobalRealmMigration @Inject constructor() : RealmMigration {
/**
* 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
// Current schema version val schemaVersion = 1L
const val SCHEMA_VERSION = 1L
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) { override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
Timber.d("Migrating Global Realm from $oldVersion to $newVersion") Timber.d("Migrating Global Realm from $oldVersion to $newVersion")

View File

@ -51,14 +51,15 @@ internal abstract class RawModule {
@Provides @Provides
@GlobalDatabase @GlobalDatabase
@MatrixScope @MatrixScope
fun providesRealmConfiguration(realmKeysUtils: RealmKeysUtils): RealmConfiguration { fun providesRealmConfiguration(realmKeysUtils: RealmKeysUtils,
globalRealmMigration: GlobalRealmMigration): RealmConfiguration {
return RealmConfiguration.Builder() return RealmConfiguration.Builder()
.apply { .apply {
realmKeysUtils.configureEncryption(this, DB_ALIAS) realmKeysUtils.configureEncryption(this, DB_ALIAS)
} }
.name("matrix-sdk-global.realm") .name("matrix-sdk-global.realm")
.schemaVersion(GlobalRealmMigration.SCHEMA_VERSION) .schemaVersion(globalRealmMigration.schemaVersion)
.migration(GlobalRealmMigration) .migration(globalRealmMigration)
.allowWritesOnUiThread(true) .allowWritesOnUiThread(true)
.modules(GlobalRealmModule()) .modules(GlobalRealmModule())
.build() .build()

View File

@ -60,6 +60,7 @@ internal abstract class IdentityModule {
@IdentityDatabase @IdentityDatabase
@SessionScope @SessionScope
fun providesIdentityRealmConfiguration(realmKeysUtils: RealmKeysUtils, fun providesIdentityRealmConfiguration(realmKeysUtils: RealmKeysUtils,
realmIdentityStoreMigration: RealmIdentityStoreMigration,
@SessionFilesDirectory directory: File, @SessionFilesDirectory directory: File,
@UserMd5 userMd5: String): RealmConfiguration { @UserMd5 userMd5: String): RealmConfiguration {
return RealmConfiguration.Builder() return RealmConfiguration.Builder()
@ -68,8 +69,8 @@ internal abstract class IdentityModule {
.apply { .apply {
realmKeysUtils.configureEncryption(this, SessionModule.getKeyAlias(userMd5)) realmKeysUtils.configureEncryption(this, SessionModule.getKeyAlias(userMd5))
} }
.schemaVersion(RealmIdentityStoreMigration.IDENTITY_STORE_SCHEMA_VERSION) .schemaVersion(realmIdentityStoreMigration.schemaVersion)
.migration(RealmIdentityStoreMigration) .migration(realmIdentityStoreMigration)
.allowWritesOnUiThread(true) .allowWritesOnUiThread(true)
.modules(IdentityRealmModule()) .modules(IdentityRealmModule())
.build() .build()

View File

@ -20,13 +20,20 @@ import io.realm.DynamicRealm
import io.realm.RealmMigration import io.realm.RealmMigration
import org.matrix.android.sdk.internal.session.identity.db.migration.MigrateIdentityTo001 import org.matrix.android.sdk.internal.session.identity.db.migration.MigrateIdentityTo001
import timber.log.Timber import timber.log.Timber
import javax.inject.Inject
internal object RealmIdentityStoreMigration : RealmMigration { internal class RealmIdentityStoreMigration @Inject constructor() : RealmMigration {
/**
* 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
const val IDENTITY_STORE_SCHEMA_VERSION = 1L val schemaVersion = 1L
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) { override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
Timber.v("Migrating Realm Identity from $oldVersion to $newVersion") Timber.d("Migrating Realm Identity from $oldVersion to $newVersion")
if (oldVersion < 1) MigrateIdentityTo001(realm).perform() if (oldVersion < 1) MigrateIdentityTo001(realm).perform()
} }