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

IllegalArgumentException when Deleting Class Not in Schema with AutomaticSchemaMigration #1548

Closed
sirambd opened this issue Oct 16, 2023 · 4 comments
Assignees
Labels
Importance:Workaround More-information-needed More information needed from the reporter. The issue will autoclose if no more information is given. O-Community SDK-Use:Local Waiting-For-Reporter Waiting for more information from the reporter before we can proceed

Comments

@sirambd
Copy link

sirambd commented Oct 16, 2023

Problem

When migrating with AutomaticSchemaMigration, I get an IllegalArgumentException because I want to delete the Signature class, but it's no longer in the schema list because it doesn't make sense to keep it in the schema migration list.
I even tried with enumerates and it didn't work.

Here's an example:

val MAILBOX_CONTENT_MIGRATION = AutomaticSchemaMigration { migrationContext ->
    val oldRealm = migrationContext.oldRealm
    val newRealm = migrationContext.newRealm
    if (oldRealm.schemaVersion() < 9 && newRealm.schemaVersion() >= 9) {
        migrationContext.newRealm.delete("Signature")
    }
}

Exception

java.lang.IllegalArgumentException: Schema does not contain a class named 'Signature'
2023-10-16 19:48:36.541 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.schema.SchemaMetadata$DefaultImpls.getOrThrow(CachedClassKeyMap.kt:40)
2023-10-16 19:48:36.541 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.schema.CachedSchemaMetadata.getOrThrow(CachedClassKeyMap.kt:89)
2023-10-16 19:48:36.542 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.dynamic.DynamicMutableRealmImpl.query(DynamicMutableRealmImpl.kt:65)
2023-10-16 19:48:36.542 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.dynamic.DynamicRealm$DefaultImpls.query$default(DynamicRealm.kt:45)
2023-10-16 19:48:36.542 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.dynamic.DynamicMutableRealmImpl.delete(DynamicMutableRealmImpl.kt:106)
2023-10-16 19:48:36.542 24243-24443 System.err              com.infomaniak.mail                  W  	at com.infomaniak.mail.data.cache.RealmMigrationsKt.MAILBOX_CONTENT_MIGRATION$lambda$2(RealmMigrations.kt:36)
2023-10-16 19:48:36.542 24243-24443 System.err              com.infomaniak.mail                  W  	at com.infomaniak.mail.data.cache.RealmMigrationsKt.$r8$lambda$m8gC0trFnAvmBIhjEaMMi3q8yFo(Unknown Source:0)
2023-10-16 19:48:36.543 24243-24443 System.err              com.infomaniak.mail                  W  	at com.infomaniak.mail.data.cache.RealmMigrationsKt$$ExternalSyntheticLambda2.migrate(Unknown Source:0)
2023-10-16 19:48:36.543 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.ConfigurationImpl.lambda$3$lambda$2(ConfigurationImpl.kt:171)
2023-10-16 19:48:36.543 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.ConfigurationImpl$$ExternalSyntheticLambda0.migrate(Unknown Source:4)
2023-10-16 19:48:36.543 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.interop.realmcJNI.realm_open(Native Method)
2023-10-16 19:48:36.544 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.interop.realmc.realm_open(realmc.java:426)
2023-10-16 19:48:36.544 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.interop.RealmInterop.realm_open(RealmInterop.kt:202)
2023-10-16 19:48:36.544 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.ConfigurationImpl$openRealm$2.invoke(ConfigurationImpl.kt:114)
2023-10-16 19:48:36.544 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.ConfigurationImpl$openRealm$2.invoke(ConfigurationImpl.kt:113)
2023-10-16 19:48:36.544 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.interop.NativePointerKt.use(NativePointer.kt:53)
2023-10-16 19:48:36.544 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.ConfigurationImpl.openRealm$suspendImpl(ConfigurationImpl.kt:113)
2023-10-16 19:48:36.544 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.ConfigurationImpl.openRealm(Unknown Source:0)
2023-10-16 19:48:36.544 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.RealmImpl$1.invokeSuspend(RealmImpl.kt:132)
2023-10-16 19:48:36.545 24243-24443 System.err              com.infomaniak.mail                  W  	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
2023-10-16 19:48:36.545 24243-24443 System.err              com.infomaniak.mail                  W  	at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
2023-10-16 19:48:36.545 24243-24443 System.err              com.infomaniak.mail                  W  	at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:280)
2023-10-16 19:48:36.545 24243-24443 System.err              com.infomaniak.mail                  W  	at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
2023-10-16 19:48:36.545 24243-24443 System.err              com.infomaniak.mail                  W  	at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
2023-10-16 19:48:36.545 24243-24443 System.err              com.infomaniak.mail                  W  	at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source:1)
2023-10-16 19:48:36.545 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.platform.CoroutineUtilsSharedJvmKt.runBlocking(CoroutineUtilsSharedJvm.kt:22)
2023-10-16 19:48:36.546 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.platform.CoroutineUtilsSharedJvmKt.runBlocking$default(CoroutineUtilsSharedJvm.kt:21)
2023-10-16 19:48:36.546 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.RealmImpl.<init>(RealmImpl.kt:114)
2023-10-16 19:48:36.546 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.RealmImpl.<init>(Unknown Source:0)
2023-10-16 19:48:36.546 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.internal.RealmImpl$Companion.create$io_realm_kotlin_library(RealmImpl.kt:293)
2023-10-16 19:48:36.546 24243-24443 System.err              com.infomaniak.mail                  W  	at io.realm.kotlin.Realm$Companion.open(Realm.kt:83)
2023-10-16 19:48:36.546 24243-24443 System.err              com.infomaniak.mail                  W  	at com.infomaniak.mail.data.cache.RealmDatabase.newMailboxContentInstance(RealmDatabase.kt:80)
2023-10-16 19:48:36.547 24243-24443 System.err              com.infomaniak.mail                  W  	at com.infomaniak.mail.data.cache.RealmDatabase.getNewMailboxContentInstance(RealmDatabase.kt:79)
2023-10-16 19:48:36.547 24243-24443 System.err              com.infomaniak.mail                  W  	at com.infomaniak.mail.data.cache.RealmDatabase$MailboxContent$invoke$1.invokeSuspend(RealmDatabase.kt:85)
2023-10-16 19:48:36.547 24243-24443 System.err              com.infomaniak.mail                  W  	at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)

Version
Realm 1.11.1

Solution

I haven't found any other way than to leave the class in the schema migrations, but ideally it would be nice to be able to delete an existing class even if it no longer appears in the current schema migrations.

  • Perhaps the oldRealm could contain a delete method, which would allow deletion based on the previous schema only (not a fan, as it would be confusing).
  • Or a way to temporarily put the schema in the list until the migration is complete.
  • Or just a way to delete without checking the schema during migration.

Alternatives

No response

How important is this improvement for you?

I would like to have it but have a workaround

Feature would mainly be used with

Local Database only

@clementetb
Copy link
Contributor

It is not required to delete the class. AutomaticSchemaMigration automatically migrates the schema.

During an AutomaticSchemaMigration, you only need to move the data from the old classes/properties to the new ones.

@sirambd
Copy link
Author

sirambd commented Oct 17, 2023

@clementetb Even in the case of migrating the data, I'd like to be able to remove it from the list from within the RealmConfiguration.Builder, in itself the migration is fine, there's nothing to worry about.
But it's just that I'd like to remove the class from the list as it's useless, however doing this creates a problem even using enumerates which returns null as it requires the class to be left in the list.

@clementetb
Copy link
Contributor

It is not required to add the old class to the configuration's schema list.

The way that automatic migration works is that you update your schema, pass it to the configuration, and then open the Realm. No need to keep references to the "old schema".

If while opening the Realm a schema variation is detected, then the migration codeblock will be triggered. During the migration you are given two Dynamic Realms, an instance before the schema variation and an instance with the new schema. You can use them to move any data from the old to the new.

@sync-by-unito sync-by-unito bot added More-information-needed More information needed from the reporter. The issue will autoclose if no more information is given. Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Nov 14, 2023
Copy link
Contributor

This issue has been automatically closed because there has been no response to our request for more information from the original author. With only the information that is currently in the issue, we don't have enough information to take action. Please reach out if you have or find the answers we need so that we can investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 18, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Importance:Workaround More-information-needed More information needed from the reporter. The issue will autoclose if no more information is given. O-Community SDK-Use:Local Waiting-For-Reporter Waiting for more information from the reporter before we can proceed
Projects
None yet
Development

No branches or pull requests

2 participants