diff --git a/CHANGELOG.md b/CHANGELOG.md index 686876a43ac..f684e4bb8e7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ### Enhancements * On Windows devices Device Sync will additionally look up SSL certificates in the Windows Trusted Root Certification Authorities certificate store when establishing a connection. (PR [#7882](https://github.com/realm/realm-core/pull/7882)) * Updated the return type of `LogCategory::get_category_names()` from `std::vector` to `std::vector`. ([PR #7879](https://github.com/realm/realm-core/pull/7879)) +* Added `realm_get_persisted_schema_version` for reading the version of the schema currently stored locally. (PR [#7873](https://github.com/realm/realm-core/pull/7873)) ### Fixed * ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?) @@ -24,7 +25,6 @@ # 14.10.4 Release notes ### Enhancements -* None. ### Fixed * When a public name is defined on a property, calling `realm::Results::sort()` or `realm::Results::distinct()` with the internal name could throw an error like `Cannot sort on key path 'NAME': property 'PersonObject.NAME' does not exist`. ([realm/realm-js#6779](https://github.com/realm/realm-js/issues/6779), since v12.12.0) diff --git a/src/realm.h b/src/realm.h index 68e39b80111..255988f2d2f 100644 --- a/src/realm.h +++ b/src/realm.h @@ -1241,6 +1241,11 @@ RLM_API realm_schema_t* realm_get_schema(const realm_t*); */ RLM_API uint64_t realm_get_schema_version(const realm_t* realm); +/** + * Get the schema version for this realm at the path. + */ +RLM_API uint64_t realm_get_persisted_schema_version(const realm_config_t* config); + /** * Update the schema of an open realm. * diff --git a/src/realm/object-store/c_api/schema.cpp b/src/realm/object-store/c_api/schema.cpp index 389524e5346..4d04a6c31b0 100644 --- a/src/realm/object-store/c_api/schema.cpp +++ b/src/realm/object-store/c_api/schema.cpp @@ -55,6 +55,23 @@ RLM_API uint64_t realm_get_schema_version(const realm_t* realm) return rlm->schema_version(); } +RLM_API uint64_t realm_get_persisted_schema_version(const realm_config_t* config) +{ + auto conf = RealmConfig(); + conf.schema_version = ObjectStore::NotVersioned; + conf.path = config->path; + conf.encryption_key = config->encryption_key; + + if (config->sync_config) { + conf.sync_config = nullptr; + conf.force_sync_history = true; + } + + auto realm = Realm::get_shared_realm(conf); + uint64_t version = ObjectStore::get_schema_version(realm->read_group()); + return version; +} + RLM_API bool realm_schema_validate(const realm_schema_t* schema, uint64_t validation_mode) { return wrap_err([&]() { diff --git a/test/object-store/c_api/c_api.cpp b/test/object-store/c_api/c_api.cpp index 3d94bb112d7..7edad0de6e2 100644 --- a/test/object-store/c_api/c_api.cpp +++ b/test/object-store/c_api/c_api.cpp @@ -1287,6 +1287,9 @@ TEST_CASE("C API - schema", "[c_api]") { realm_config_set_schema_version(config.get(), 0); realm_config_set_schema(config.get(), schema.get()); + // no local schema version yet + REQUIRE(realm_get_persisted_schema_version(config.get()) == (uint64_t)-1); + SECTION("error on open") { { std::ofstream o(test_file_2.path.c_str()); @@ -1304,6 +1307,7 @@ TEST_CASE("C API - schema", "[c_api]") { realm_config_set_data_initialization_function(config.get(), initialize_data, &userdata, nullptr); auto realm = cptr_checked(realm_open(config.get())); CHECK(userdata.num_initializations == 1); + REQUIRE(realm_get_persisted_schema_version(config.get()) == 0); } SECTION("data initialization callback error") { @@ -1323,6 +1327,7 @@ TEST_CASE("C API - schema", "[c_api]") { realm_config_set_migration_function(config.get(), migrate_schema, &userdata, nullptr); auto realm = cptr_checked(realm_open(config.get())); CHECK(userdata.num_migrations == 0); + REQUIRE(realm_get_persisted_schema_version(config.get()) == 0); realm.reset(); auto config2 = cptr(realm_config_new()); @@ -1334,6 +1339,7 @@ TEST_CASE("C API - schema", "[c_api]") { realm_config_set_migration_function(config2.get(), migrate_schema, &userdata, nullptr); auto realm2 = cptr_checked(realm_open(config2.get())); CHECK(userdata.num_migrations == 1); + REQUIRE(realm_get_persisted_schema_version(config2.get()) == 999); } SECTION("migrate schema and delete old table") { @@ -5869,6 +5875,7 @@ TEST_CASE("C API - async_open", "[sync][pbs][c_api]") { realm_config_set_path(config, test_config.path.c_str()); realm_config_set_sync_config(config, sync_config); realm_config_set_schema_version(config, 1); + realm_async_open_task_t* task = realm_open_synchronized(config); REQUIRE(task); Userdata userdata;