From df4a6326f581679686490f20e1d1d42c82f05c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Edelbo?= Date: Fri, 31 May 2024 16:19:15 +0200 Subject: [PATCH] Output InRealmHistory when compacting (#7753) This will ensure that the current history version is written, so that an upgrade will not be required when opening the file. --- CHANGELOG.md | 4 ++++ src/realm/group.cpp | 3 +-- test/test_shared.cpp | 45 +++++++++++++++++++++++++++++++++----------- 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 89700ac466a..afd9b7f18fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,11 @@ ### Fixed * ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?) * Add a missing file from the bid library to the android blueprint. (PR [#7738](https://github.com/realm/realm-core/pull/7738)) +<<<<<<< HEAD +* After compacting, a file upgrade would be triggered. This could cause loss of data if schema mode is SoftResetFile ([#7747](https://github.com/realm/realm-core/issues/7747), since 14.0.0) +======= * Add missing `REALM_APP_SERVICES` flag to the android blueprint. (PR [#7755](https://github.com/realm/realm-core/pull/7755)) +>>>>>>> master ### Breaking changes * None. diff --git a/src/realm/group.cpp b/src/realm/group.cpp index a0f2f5deac5..40fab4497f8 100644 --- a/src/realm/group.cpp +++ b/src/realm/group.cpp @@ -951,8 +951,7 @@ auto Group::DefaultTableWriter::write_history(_impl::OutputStream& out) -> Histo m_group->m_top.get_ref(), version, history_type, history_schema_version); REALM_ASSERT(history_type != Replication::hist_None); - if (!m_should_write_history || - (history_type != Replication::hist_SyncClient && history_type != Replication::hist_SyncServer)) { + if (!m_should_write_history || history_type == Replication::hist_None) { return info; // Only sync history should be preserved when writing to a new file } info.type = history_type; diff --git a/test/test_shared.cpp b/test/test_shared.cpp index da4c350ae06..78ede3b4a0c 100644 --- a/test/test_shared.cpp +++ b/test/test_shared.cpp @@ -495,25 +495,48 @@ TEST(Shared_CompactingOnTheFly) TEST(Shared_ReadAfterCompact) { SHARED_GROUP_TEST_PATH(path); - DBRef sg = get_test_db(path); { + DBRef sg = DB::create(make_in_realm_history(), path); WriteTransaction wt(sg); auto table = wt.add_table("table"); table->add_column(type_Int, "col"); table->create_object().set_all(1); wt.commit(); + sg->compact(); } - sg->compact(); - auto rt = sg->start_read(); - auto table = rt->get_table("table"); - for (int i = 2; i < 4; ++i) { - WriteTransaction wt(sg); - wt.get_table("table")->create_object().set_all(i); - wt.commit(); - } + { + DBOptions options; + options.allow_file_format_upgrade = false; + DBRef sg = DB::create(make_in_realm_history(), path, options); + auto rt = sg->start_read(); + auto table = rt->get_table("table"); + for (int i = 2; i < 4; ++i) { + WriteTransaction wt(sg); + wt.get_table("table")->create_object().set_all(i); + wt.commit(); + } - CHECK_EQUAL(table->size(), 1); - CHECK_EQUAL(table->get_object(0).get("col"), 1); + CHECK_EQUAL(table->size(), 1); + auto obj = table->get_object(0); + CHECK_EQUAL(obj.get("col"), 1); + + struct Parser : _impl::NoOpTransactionLogParser { + bool create_object(ObjKey) + { + nb_objects++; + return true; + } + bool modify_object(ColKey, ObjKey) + { + return true; + } + void parse_complete() {} + int nb_objects = 0; + } parser; + + rt->advance_read(&parser); + CHECK_EQUAL(parser.nb_objects, 2); + } } TEST(Shared_ReadOverRead)