Skip to content

Commit

Permalink
Add ability to open a synced realm without sync configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
leemaguire committed Jul 23, 2024
1 parent 01152ac commit 755d9fe
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 0 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
NEXT_RELEASE Release notes (YYYY-MM-DD)
=============================================================

### Fixed
* None

### Enhancements
* Add `realm::db_config::enable_forced_sync_history()` which allows you to open a synced Realm
even if a sync configuration is not supplied.

### Compatibility
* Fileformat: Generates files with format v24. Reads and automatically upgrade from fileformat v10.

### Internals
* None

2.2.0 Release notes (2024-07-22)
=============================================================

Expand Down
2 changes: 2 additions & 0 deletions include/cpprealm/internal/bridge/realm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,8 @@ namespace realm::internal::bridge {
void set_schema_version(uint64_t version);
void set_encryption_key(const std::array<char, 64>&);
void should_compact_on_launch(std::function<bool(uint64_t total_bytes, uint64_t unused_bytes)>&& fn);
/// Open the Realm using the sync history mode even if a sync configuration is not supplied.
void enable_forced_sync_history();
std::optional<schema> get_schema();

template<typename T>
Expand Down
4 changes: 4 additions & 0 deletions src/cpprealm/internal/bridge/realm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ namespace realm::internal::bridge {
get_config()->should_compact_on_launch_function = std::move(fn);
}

void realm::config::enable_forced_sync_history() {
get_config()->force_sync_history = true;
}

enum ::realm::client_reset_mode realm::config::get_client_reset_mode() const {
return static_cast<enum ::realm::client_reset_mode>(get_config()->sync_config->client_resync_mode);
}
Expand Down
75 changes: 75 additions & 0 deletions tests/sync/flexible_sync_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,81 @@ TEST_CASE("flexible_sync", "[sync]") {
auto flx_sync_config2 = user.flexible_sync_configuration();
REQUIRE_THROWS(db(flx_sync_config2));
}

SECTION("open synced realm without sync config") {
realm_path path;
auto user = app.login(realm::App::credentials::anonymous()).get();

{
auto flx_sync_config = user.flexible_sync_configuration();
flx_sync_config.set_path(path);
auto synced_realm = db(flx_sync_config);

auto update_success = synced_realm.subscriptions().update([](realm::mutable_sync_subscription_set &subs) {
subs.clear();
}).get();
CHECK(update_success == true);
CHECK(synced_realm.subscriptions().size() == 0);

update_success = synced_realm.subscriptions().update([](realm::mutable_sync_subscription_set &subs) {
subs.add<AllTypesObject>("foo-strings", [](auto &obj) {
return obj.str_col == "foo";
});
subs.add<AllTypesObjectLink>("foo-link");
})
.get();
CHECK(update_success == true);
CHECK(synced_realm.subscriptions().size() == 2);

synced_realm.get_sync_session()->wait_for_upload_completion().get();
synced_realm.get_sync_session()->wait_for_download_completion().get();

synced_realm.write([&synced_realm]() {
AllTypesObject o;
o._id = 1;
o.str_col = "foo";
synced_realm.add(std::move(o));
});

synced_realm.get_sync_session()->wait_for_upload_completion().get();
synced_realm.get_sync_session()->wait_for_download_completion().get();

synced_realm.refresh();
auto objs = synced_realm.objects<AllTypesObject>();
CHECK(objs.size() == 1);
}

{
auto local_config = realm::db_config();
local_config.set_path(path);
local_config.enable_forced_sync_history();
auto local_realm = realm::open<AllTypesObject, AllTypesObjectLink, AllTypesObjectEmbedded>(local_config);
auto s = local_realm.objects<AllTypesObject>().size();
CHECK(s == 1);
local_realm.write([&local_realm]() {
AllTypesObject o;
o._id = 2;
o.str_col = "foo";
local_realm.add(std::move(o));
});
}

{
auto flx_sync_config = user.flexible_sync_configuration();
flx_sync_config.set_path(path);
auto synced_realm = db(flx_sync_config);
auto subs_size = synced_realm.subscriptions().size();
CHECK(subs_size == 2);

synced_realm.get_sync_session()->wait_for_upload_completion().get();
synced_realm.get_sync_session()->wait_for_download_completion().get();

synced_realm.refresh();
auto objs = synced_realm.objects<AllTypesObject>();
CHECK(objs.size() == 2);
}
}

}

template<typename T, typename Func>
Expand Down

0 comments on commit 755d9fe

Please sign in to comment.