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

fix an edge case of embedded table migrations #4694

Merged
merged 1 commit into from
May 14, 2021
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
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
* None.
* Fixed an incorrect detection of multiple incoming links in a migration when changing a table to embedded and removing a link to it at the same time. ([#4694](https://github.com/realm/realm-core/issues/4694) since 10.0.0-beta.2)

### Breaking changes
* None.

Expand Down
10 changes: 8 additions & 2 deletions src/realm/object-store/schema.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -322,8 +322,6 @@ std::vector<SchemaChange> Schema::compare(Schema const& target_schema, bool incl
if (include_table_removals)
changes.emplace_back(schema_change::RemoveTable{existing});
}
else if (existing->is_embedded != target->is_embedded)
changes.emplace_back(schema_change::ChangeTableType{target});
});

// Modify columns
Expand All @@ -336,6 +334,14 @@ std::vector<SchemaChange> Schema::compare(Schema const& target_schema, bool incl
}
// nothing for tables in existing but not target
});

// Detect embedded table changes last, in case column property changes affect link counts
zip_matching(target_schema, *this, [&](const ObjectSchema* target, const ObjectSchema* existing) {
if (existing && target && existing->is_embedded != target->is_embedded) {
changes.emplace_back(schema_change::ChangeTableType{target});
}
});

return changes;
}

Expand Down
58 changes: 58 additions & 0 deletions test/object-store/migrations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,64 @@ TEST_CASE("migration: Automatic") {
}
}

SECTION("change table to embedded - multiple incoming links per object resolved by removing a column") {
Schema schema = {
{"child_table",
{
{"value", PropertyType::Int},
}},
{"parent_table",
{
{"child_property", PropertyType::Object | PropertyType::Nullable, "child_table"},
{"child_property_duplicate", PropertyType::Object | PropertyType::Nullable, "child_table"},
}},
};
Schema schema2 = {
{"child_table",
IsEmbedded{true},
{
{"value", PropertyType::Int},
}},
{"parent_table",
{
{"child_property", PropertyType::Object | PropertyType::Nullable, "child_table"},
}},
};

auto realm = Realm::get_shared_realm(config);
realm->update_schema(schema, 1);
realm->begin_transaction();
auto child_table = ObjectStore::table_for_object_type(realm->read_group(), "child_table");
Obj child_object1 = child_table->create_object();
child_object1.set("value", 42);
Obj child_object2 = child_table->create_object();
child_object2.set("value", 43);
auto parent_table = ObjectStore::table_for_object_type(realm->read_group(), "parent_table");
auto child_object_key1 = child_object1.get_key();
auto child_object_key2 = child_object2.get_key();
parent_table->create_object().set_all(child_object_key1, child_object_key1);
parent_table->create_object().set_all(child_object_key2, child_object_key2);
realm->commit_transaction();
REQUIRE(parent_table->size() == 2);
REQUIRE(child_table->size() == 2);
REQUIRE_FALSE(child_table->is_embedded());

REQUIRE_NOTHROW(realm->update_schema(schema2, 2, nullptr));

REQUIRE(realm->schema_version() == 2);
REQUIRE(parent_table->size() == 2);
REQUIRE(child_table->size() == 2);
REQUIRE(child_table->is_embedded());
CppContext context(realm);
for (int i = 0; i < 2; i++) {
Object parent_object(realm, "parent_table", i);
Object child_object =
any_cast<Object>(parent_object.get_property_value<util::Any>(context, "child_property"));
Int value = any_cast<Int>(child_object.get_property_value<util::Any>(context, "value"));
REQUIRE(value == 42 + i);
}
}

SECTION("change table to embedded - multiple incoming links - resolved in migration block") {
Schema schema = {
{"child_table",
Expand Down