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

Add get local schema version to the C-API #7873

Merged
merged 6 commits into from
Jul 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<const char*>` to `std::vector<std::string_view>`. ([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
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
Expand All @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions src/realm.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
17 changes: 17 additions & 0 deletions src/realm/object-store/c_api/schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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([&]() {
Expand Down
7 changes: 7 additions & 0 deletions test/object-store/c_api/c_api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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") {
Expand All @@ -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());
Expand All @@ -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") {
Expand Down Expand Up @@ -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;
Expand Down
Loading