realm / realm-core

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

Better error message when opening outdated files as read-only #3495

Closed kraenhansen closed 4 years ago

kraenhansen commented 4 years ago

When opening an old Realm-file with a newer version of Realm Core it automatically upgrades the file to the newest version of the file format. If the user opens the file in read-only mode (via Realm JS), the following error is thrown:

Error: Unable to open a realm at path '/Users/kraenhansen/Repositories/realm-studio/src/services/data-importer/tests/csv/asset_file.realm': Unsupported Realm file format version Path:Exception backtrace:
0   realm.node                          0x0000000108540971 _ZN5realm15InvalidDatabaseC2ERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEES9_ + 49
1   realm.node                          0x000000010852fe06 _ZN5realm5Group4openEmRKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEE + 262
2   realm.node                          0x000000010852d843 _ZN5realm5Group4openERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEPKcNS0_8OpenModeE + 99
3   realm.node                          0x000000010852d6b8 _ZN5realm5GroupC2ERKNSt3__112basic_stringIcNS1_11char_traitsIcEENS1_9allocatorIcEEEEPKcNS0_8OpenModeE + 616
4   realm.node                          0x000000010823c281 _ZNSt3__110shared_ptrIN5realm5GroupEE11make_sharedIJRNS_12basic_stringIcNS_11char_traitsIcEENS_9allocatorIcEEEEPcNS2_8OpenModeEEEES3_DpOT_ + 87
5   realm.node                          0x0000000108233530 _ZN5realm5_impl16RealmCoordinator7open_dbEv + 578
6   realm.node                          0x00000001082329ae _ZN5realm5_impl16RealmCoordinator12do_get_realmENS_5Realm6ConfigERNSt3__110shared_ptrIS2_EERNS4_11unique_lockINS4_5mutexEEEb + 58
7   realm.node                          0x00000001082328ed _ZN5realm5_impl16RealmCoordinator9get_realmENS_5Realm6ConfigE + 131
8   realm.node                          0x0000000108221523 _ZN5realm5Realm16get_shared_realmENS0_6ConfigE + 113
9   realm.node                          0x0000000108185e9c _ZN5realm2js10RealmClassINS_4node5TypesEE19create_shared_realmEPN2v87IsolateENS_5Realm6ConfigEbONSt3__13mapINSA_12basic_stringIcNSA_11char_traitsIcEENSA_9allocatorIcEEEENSB_ISH_NS0_9ProtectedINS5_5LocalINS5_5ValueEEEEENSA_4lessISH_EENSF_INSA_4pairIKSH_SM_EEEEEESO_NSF_INSP_ISQ_ST_EEEEEEONSB_ISH_NSI_INSJ_INS5_8FunctionEEEEESO_NSF_INSP_ISQ_S10_EEEEEE + 120
10  realm.node                          0x00000001081844f6 _ZN5realm2js10RealmClassINS_4node5TypesEE11constructorEPN2v87IsolateENS5_5LocalINS5_6ObjectEEERNS0_9ArgumentsIS3_EE + 394
11  realm.node                          0x000000010818381f _ZN5realm4node10ObjectWrapINS_2js10RealmClassINS0_5TypesEEEE9constructERKN3Nan20FunctionCallbackInfoIN2v85ValueEEE + 333
12  realm.node                          0x0000000108184216 _ZN3Nan3impL23FunctionCallbackWrapperERKN2v820FunctionCallbackInfoINS1_5ValueEEE + 166
13  node                                0x000000010024417f _ZN2v88internal25FunctionCallbackArguments4CallEPNS0_15CallHandlerInfoE + 623
14  node                                0x000000010024331b _ZN2v88internal12_GLOBAL__N_119HandleApiCallHelperILb1EEENS0_11MaybeHandleINS0_6ObjectEEEPNS0_7IsolateENS0_6HandleINS0_10HeapObjectEEESA_NS8_INS0_20FunctionTemplateInfoEEENS8_IS4_EENS0_16BuiltinArgumentsE + 427
15  node                                0x0000000100242d37 _ZN2v88internalL26Builtin_Impl_HandleApiCallENS0_16BuiltinArgumentsEPNS0_7IsolateE + 247
16  ???                                 0x000017e6df05be3d 0x0 + 26280351612477
17  ???                                 0x000017e6df00d145 0x0 + 26280351289669.

It is not obvious from the message nor stacktrace that the cause of this is the user opening the file in read-only mode. Instead I propose that if an upgrade is needed and the file was opened as read-only, an error with the following message is thrown:

"Unable to open a realm at path '/Users/kraenhansen/Repositories/realm-studio/src/services/data-importer/tests/csv/asset_file.realm': Unable to upgrade Realm file format when opened in read-only mode"
bmunkholm commented 4 years ago

It could also report the format number of the file. It should not crash but report an error (which requires those calling open to handle the error)

RealmBot commented 4 years ago

➤ Jørgen Edelbo commented:

[~kraen.hansen] would you care to verify that the fix is sufficient?

RealmBot commented 4 years ago

➤ Kræn Hansen commented:

Looking at [the changes on GitHub|https://github.com/realm/realm-core/commit/44f79774acd388f6044ed57a59c01707492b8df0 ( https://github.com/realm/realm-core/commit/44f79774acd388f6044ed57a59c01707492b8df0 )] I believe the chances to the message ("Realm file needs upgrade before opening in RO mode") is sufficient. I'll close when this has released and I've verified it via Realm JS.

RealmBot commented 4 years ago

➤ Kræn Hansen commented:

Now Realm JS (4.0.0-beta.1) throws an error with the message: "The Realm file format must be allowed to be upgraded in order to proceed."

I thinks it's because the original error gets catched by object store and another error is thrown here https://github.com/realm/realm-object-store/blob/2a204063e1e1a366efbdd909fbea9effceb7d3c4/src/shared_realm.cpp#L138-L143 ( https://github.com/realm/realm-object-store/blob/2a204063e1e1a366efbdd909fbea9effceb7d3c4/src/shared_realm.cpp#L138-L143 ).

That message doesn't point to the fact that it was opened as read-only, but I think its fine in regards to the intention behind this issue.