Skip to content

Commit

Permalink
suggested fix + basic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicola-cab committed Jul 12, 2024
1 parent f870a65 commit 8830dca
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/realm/object-store/c_api/schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,17 @@ RLM_API uint64_t realm_get_schema_version(const realm_t* realm)

RLM_API uint64_t realm_get_persisted_schema_version(const realm_config_t* config)
{

auto conf = RealmConfig();
conf.schema_mode = SchemaMode::Immutable;
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;
}

return wrap_err([&]() {
auto realm = new shared_realm{Realm::get_shared_realm(conf)};
uint64_t version = ObjectStore::get_schema_version(realm->get()->read_group());
Expand Down
9 changes: 9 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 All @@ -5883,6 +5890,8 @@ TEST_CASE("C API - async_open", "[sync][pbs][c_api]") {
realm_t* realm = realm_from_thread_safe_reference(userdata.realm_ref, nullptr);
realm_release(userdata.realm_ref);

REQUIRE(realm_get_persisted_schema_version(config) == 0);

bool found;
realm_class_info_t class_info;
realm_find_class(realm, "object", &found, &class_info);
Expand Down

0 comments on commit 8830dca

Please sign in to comment.