Skip to content

Commit

Permalink
Bail out earlier on errors if automatic backlink handling is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
tgoyne committed Oct 6, 2022
1 parent bf6d938 commit bf7acf6
Showing 1 changed file with 17 additions and 20 deletions.
37 changes: 17 additions & 20 deletions src/realm/table.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1173,37 +1173,34 @@ void Table::set_embedded(bool embedded, bool handle_backlinks)

for (size_t i = 0; i < size; ++i) {
if (incoming_link_count[i] == LinkCount::None) {
if (!handle_backlinks) {
throw std::logic_error(util::format("Cannot convert '%1' to embedded: at least one object has no "
"incoming links and would be deleted.",
get_class_name()));
}
orphans.push_back(cluster->get_real_key(i));
}
else if (incoming_link_count[i] == LinkCount::Multiple) {
if (!handle_backlinks) {
throw std::logic_error(util::format(
"Cannot convert '%1' to embedded: at least one object has more than one incoming link.",
get_class_name()));
}
multiple_incoming_links.push_back(cluster->get_real_key(i));
}
}

return IteratorControl::AdvanceToNext;
});

if (handle_backlinks) {
for (auto key : orphans) {
remove_object(key);
}
for (auto key : multiple_incoming_links) {
auto obj = get_object(key);
obj.handle_multiple_backlinks_during_schema_migration();
obj.remove();
}
// orphans and multiple_incoming_links will always be empty if `handle_backlinks = false`
for (auto key : orphans) {
remove_object(key);
}
else {
if (!orphans.empty()) {
throw std::logic_error(util::format(
"Cannot convert '%1' to embedded: at least one object has no incoming links and would be deleted.",
get_class_name()));
}
if (!multiple_incoming_links.empty()) {
throw std::logic_error(
util::format("Cannot convert '%1' to embedded: at least one object has more than one incoming link.",
get_class_name()));
}
for (auto key : multiple_incoming_links) {
auto obj = get_object(key);
obj.handle_multiple_backlinks_during_schema_migration();
obj.remove();
}

do_set_table_type(Type::Embedded);
Expand Down

0 comments on commit bf7acf6

Please sign in to comment.