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

RCORE-2265: Fix issue when changing type of primary key #8046

Merged
merged 2 commits into from
Nov 18, 2024
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
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* None.

### Fixed
* <How do the end-user experience this issue? what was the impact?> ([#????](https://github.com/realm/realm-core/issues/????), since v?.?.?)
* Migrating primary key to a new type without migration function would cause an assertion to fail. ([#8045](https://github.com/realm/realm-core/issues/8045), since v10.0.0)
* None.

### Breaking changes
Expand Down
9 changes: 6 additions & 3 deletions src/realm/object-store/object_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ ColKey add_column(Group& group, Table& table, Property const& property)
REALM_ASSERT(property.type != PropertyType::LinkingObjects);

if (property.is_primary) {
// Primary key columns should have been created when the table was created
// Primary key columns should have been created when the table was created.
// Unless this is a migration
if (auto col = table.get_column_key(property.name)) {
return col;
}
Expand All @@ -139,9 +140,11 @@ ColKey add_column(Group& group, Table& table, Property const& property)
else {
auto key =
table.add_column(to_core_type(property.type), property.name, is_nullable(property.type), collection_type);
if (property.requires_index())
if (property.is_primary)
table.set_primary_key_column(key); // You can end here if this is a migration
else if (property.requires_index())
table.add_search_index(key);
if (property.requires_fulltext_index())
else if (property.requires_fulltext_index())
table.add_fulltext_index(key);
return key;
}
Expand Down
25 changes: 25 additions & 0 deletions test/object-store/migrations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,31 @@ TEST_CASE("migration: Automatic", "[migration]") {
});
}

SECTION("change primary key from string to UUID without migration function") {
using namespace std::string_literals;
Schema schema{{"Foo",
{
{"_id", PropertyType::String, Property::IsPrimary{true}},
}}};
Schema schema2{{"Foo",
{
{"_id", PropertyType::UUID, Property::IsPrimary{true}},
}}};
InMemoryTestFile config;
config.schema_mode = SchemaMode::Automatic;
config.schema = schema;
auto realm = Realm::get_shared_realm(config);
realm->update_schema(schema2, 2);

CppContext ctx(realm);
std::any values = AnyDict{
{"_id", UUID("3b241101-0000-0000-0000-4136c566a964"s)},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens if the string is not a valid UUID?

Copy link
Contributor Author

@jedelbo jedelbo Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then shit happens. But that is beside the point of this test.

};
realm->begin_transaction();
Object::create(ctx, realm, *realm->schema().find("Foo"), values);
realm->commit_transaction();
}

SECTION("object accessors inside migrations") {
using namespace std::string_literals;

Expand Down
Loading