realm / realm-core

Core database component for the Realm Mobile Database SDKs
https://realm.io
Apache License 2.0
1.02k stars 165 forks source link

SDK crashes when migrating Primary Key from String to UUID #8045

Closed koneksa-sunny closed 1 week ago

koneksa-sunny commented 2 weeks ago

SDK and version

SDK : Swift Version: 10.33.0

Observations

Crash log / stacktrace

realm-core/src/realm/object-store/shared_realm.cpp:474: [realm-core-12.13.0] Assertion failed: additive || (required_changes = ObjectStore::schema_from_group(read_group()).compare(schema)).empty()
0   RealmPackage                        0x000000010b074390 _ZN5realm4utilL18terminate_internalERNSt3__118basic_stringstreamIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE + 28
1   RealmPackage                        0x000000010b074370 _ZN5realm4util9terminateEPKcS2_lOSt16initializer_listINS0_9PrintableEE + 204
2   RealmPackage                        0x000000010abf7184 _ZN5realm5Realm13update_schemaENS_6SchemaEyNSt3__18functionIFvNS2_10shared_ptrIS0_EES5_RS1_EEENS3_IFvS5_EEEb + 1680
3   RealmPackage                        0x000000010a637b18 +[RLMRealm realmWithConfiguration:queue:error:] + 2528
4   RealmPackage                        0x000000010a6db4f4 $sSo8RLMRealmC13configuration5queueABSo0A13ConfigurationC_So012OS_dispatch_C0CSgtKcfCTO + 144
5   RealmPackage                        0x000000010a791a30 $s10RealmSwift0A0V13configuration5queueA2C13ConfigurationV_So012OS_dispatch_D0CSgtKcfC + 112
6   XXXXXXX                            0x0000000102f468e4 $XXXXXXXXX + 156
7   XXXXXXX                            0x0000000102f469e0 $sIegh_IeyBh_TR + 48
8   libdispatch.dylib                   0x0000000103d64ec0 _dispatch_call_block_and_release + 24
9   libdispatch.dylib                   0x0000000103d667b8 _dispatch_client_callout + 16
10  libdispatch.dylib                   0x0000000103d6eaac _dispatch_lane_serial_drain + 912
11  libdispatch.dylib                   0x0000000103d6f7b0 _dispatch_lane_invoke + 420
12  libdispatch.dylib                   0x0000000103d7c1f0 _dispatch_root_queue_drain_deferred_wlh + 324
13  libdispatch.dylib                   0x0000000103d7b75c _dispatch_workloop_worker_thread + 732
14  libsystem_pthread.dylib             0x00000001038df814 _pthread_wqthread + 284
15  libsystem_pthread.dylib             0x00000001038de5d4 start_wqthread

Steps & Code to Reproduce

I changed my primary key type from String to UUID OLD:

public class MyClass: Object {
  @Persisted(primaryKey: true) public var id: String?

NEW:

public class MyClass: Object {
  @Persisted(primaryKey: true) public var id: UUID?

configuration:

      schemaVersion: 3,
      migrationBlock: { migration, oldSchemaVersion in
/// THIS IS NEVER CALLED
        if oldSchemaVersion == 1 {
        } else if oldSchemaVersion == 2 {
          migration.enumerateObjects(ofType: MyClass.className()) { oldObject, newObject in
            newObject!["id"] = UUID(uuidString: oldObject!["id"]! as! String)
          }
        }
      }
sync-by-unito[bot] commented 2 weeks ago

➤ PM Bot commented:

Jira ticket: RCORE-2265

koneksa-sunny commented 2 weeks ago

Note that if I change some other non-primary-key field in that class from String to UUID, I don't get the exception, but the migrationBlock is still not called and my app crashes for other reasons since it's expecting that field to be populated.

jedelbo commented 2 weeks ago

@koneksa-sunny which SchemaMode are you using when opening the realm?

jedelbo commented 2 weeks ago

I found an error that would produce the issue that you report. It is fixed by #8046. But it would require that no migration function is supplied as the codeline with the assertion is only hit if there is no migration function (or if the SchemaMode is additive changes only). So in order to fix your issue, I would look for ways that the migration function could be missing in your application code.

koneksa-sunny commented 1 week ago

@jedelbo where is SchemaMode configured or set?