realm / realm-kotlin

Kotlin Multiplatform and Android SDK for the Realm Mobile Database: Build Better Apps Faster.
Apache License 2.0
968 stars 61 forks source link

Int value in migration can't be accessed #1815

Closed DmytroTols closed 3 months ago

DmytroTols commented 3 months ago

How frequently does the bug occur?

Always

Description

In my database when i have Int variable in my RealmObject, when i migrate I want to parse different value if some value is already given in old database version.

When i try to access the value or to set the value as Int i get error: java.lang.IllegalArgumentException: Trying to access property 'Dog.legs' as type: 'class kotlin.Int' but actual schema type is 'class kotlin.Long'

If i redeclare it as a Long, everything works good, but why should i declare it as Long since in my database i declare it as Int, here small snippet, will also attach Demo and Log output:

My object:

open class Dog() : RealmObject { @PrimaryKey var type: String = "TEST DOG" var legs: Int = 0 }

When i try doing something like this, access variables as Int:

migrationContext.enumerate("Dog") { oldObject, newObject -> newObject?.set("legs", oldObject.getValue("legs", Int::class)) }

I get error that i declared above, here is stackTrace

Stacktrace & log output

2024-08-05 18:15:21.220 22744-22744 System.out              com.example.projects                 I  OLD VERSION IS 2
2024-08-05 18:15:21.274 22744-22744 AndroidRuntime          com.example.projects                 D  Shutting down VM
2024-08-05 18:15:21.285 22744-22744 AndroidRuntime          com.example.projects                 E  FATAL EXCEPTION: main (Ask Gemini)
                                                                                                    Process: com.example.projects, PID: 22744
                                                                                                    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.projects/com.example.projects.MainActivity}: java.lang.IllegalArgumentException: Trying to access property 'Dog.legs' as type: 'class kotlin.Int' but actual schema type is 'class kotlin.Long'
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2868)
                                                                                                        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2958)
                                                                                                        at android.app.ActivityThread.-wrap12(Unknown Source:0)
                                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1653)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                                        at android.os.Looper.loop(Looper.java:164)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:6756)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:449)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                                                    Caused by: java.lang.IllegalArgumentException: Trying to access property 'Dog.legs' as type: 'class kotlin.Int' but actual schema type is 'class kotlin.Long'
                                                                                                        at io.realm.kotlin.internal.RealmObjectHelper.checkPropertyType(RealmObjectHelper.kt:1230)
                                                                                                        at io.realm.kotlin.internal.RealmObjectHelper.dynamicGet$io_realm_kotlin_library(RealmObjectHelper.kt:881)
                                                                                                        at io.realm.kotlin.internal.RealmObjectHelper.dynamicGet$io_realm_kotlin_library$default(RealmObjectHelper.kt:873)
                                                                                                        at io.realm.kotlin.internal.dynamic.DynamicRealmObjectImpl.getValue(DynamicRealmObjectImpl.kt:41)
                                                                                                        at com.example.projects.database.Migration$migrate$1.invoke(Migration.kt:13)
                                                                                                        at com.example.projects.database.Migration$migrate$1.invoke(Migration.kt:12)
                                                                                                        at io.realm.kotlin.migration.AutomaticSchemaMigration$MigrationContext$DefaultImpls.enumerate(AutomaticSchemaMigration.kt:112)
                                                                                                        at io.realm.kotlin.internal.ConfigurationImpl$migrationCallback$1$1$1.enumerate(ConfigurationImpl.kt:172)
                                                                                                        at com.example.projects.database.Migration.migrate(Migration.kt:12)
                                                                                                        at io.realm.kotlin.internal.ConfigurationImpl.lambda$3$lambda$2(ConfigurationImpl.kt:172)
                                                                                                        at io.realm.kotlin.internal.ConfigurationImpl$$ExternalSyntheticLambda0.migrate(Unknown Source:4)
                                                                                                        at io.realm.kotlin.internal.interop.realmcJNI.realm_open(Native Method)
                                                                                                        at io.realm.kotlin.internal.interop.realmc.realm_open(realmc.java:434)
                                                                                                        at io.realm.kotlin.internal.interop.RealmInterop.realm_open(RealmInterop.kt:238)
                                                                                                        at io.realm.kotlin.internal.ConfigurationImpl$openRealm$2.invoke(ConfigurationImpl.kt:115)
                                                                                                        at io.realm.kotlin.internal.ConfigurationImpl$openRealm$2.invoke(ConfigurationImpl.kt:114)
                                                                                                        at io.realm.kotlin.internal.interop.NativePointerKt.use(NativePointer.kt:53)
                                                                                                        at io.realm.kotlin.internal.ConfigurationImpl.openRealm$suspendImpl(ConfigurationImpl.kt:114)
                                                                                                        at io.realm.kotlin.internal.ConfigurationImpl.openRealm(Unknown Source:0)
                                                                                                        at io.realm.kotlin.internal.RealmImpl$1.invokeSuspend(RealmImpl.kt:133)
                                                                                                        at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
                                                                                                        at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106)
                                                                                                        at kotlinx.coroutines.EventLoopImplBase.processNextEvent(EventLoop.common.kt:280)
                                                                                                        at kotlinx.coroutines.BlockingCoroutine.joinBlocking(Builders.kt:85)
                                                                                                        at kotlinx.coroutines.BuildersKt__BuildersKt.runBlocking(Builders.kt:59)
                                                                                                        at kotlinx.coroutines.BuildersKt.runBlocking(Unknown Source:1)
                                                                                                        at io.realm.kotlin.internal.platform.CoroutineUtilsSharedJvmKt.runBlocking(CoroutineUtilsSharedJvm.kt:22)
                                                                                                        at io.realm.kotlin.internal.platform.CoroutineUtilsSharedJvmKt.runBlocking$default(CoroutineUtilsSharedJvm.kt:21)
                                                                                                        at io.realm.kotlin.internal.RealmImpl.<init>(RealmImpl.kt:115)
                                                                                                        at io.realm.kotlin.internal.RealmImpl.<init>(Unknown Source:0)
                                                                                                        at io.realm.kotlin.internal.RealmImpl$Companion.create$io_realm_kotlin_library(RealmImpl.kt:318)
                                                                                                        at io.realm.kotlin.Realm$Companion.open(Realm.kt:83)
                                                                                                        at com.example.projects.database.Database.init(Database.kt:23)
2024-08-05 18:15:21.285 22744-22744 AndroidRuntime          com.example.projects                 E      at com.example.projects.MainActivity.onCreate(MainActivity.kt:18) (Ask Gemini)
                                                                                                        at android.app.Activity.performCreate(Activity.java:7025)
                                                                                                        at android.app.Activity.performCreate(Activity.java:7016)
                                                                                                        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1217)
                                                                                                        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2815)
                                                                                                        ... 9 more

Can you reproduce the bug?

Always

Reproduction Steps

Initialize database with 1 version, after running app once, redeclare database version and migrate to next version, while migrating access Int variable as Int and not as Long, when migration will run issue will apear

Version

1.15.0

What Atlas App Services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Android 10

Build environment

Android Studio version: ... Android Build Tools version: ... Gradle version: ...

sync-by-unito[bot] commented 3 months ago

➤ PM Bot commented:

Jira ticket: RKOTLIN-1117

DmytroTols commented 3 months ago

Attaching Demo

https://drive.google.com/drive/folders/1ZKx2jlZ-hs3mKGgCtM_MIVHncMiLuDnA?usp=sharing

rorbech commented 3 months ago

Hi @DmytroTols. Realm's internal storage type for integral properties are Long and we don't differentiate between the various sizes in the file (though we only use the necessary bits required to hold the smaller types). The truncation to the smaller types , e.g. Int are done in the model class' accessors, which are not in effect for dynamic objects. So you should just use

newObject?.set("legs", oldObject.getValue("legs", Long::class))
DmytroTols commented 3 months ago

Thank you for quick response