Skip to content

Commit

Permalink
Fix issue when changing type of primary key
Browse files Browse the repository at this point in the history
If you change the type of the primary key and call Realm::update_schema()
without a migration function, the new column will not be set as primary
key column.
  • Loading branch information
jedelbo committed Nov 11, 2024
1 parent 43a1aac commit e2b9d3c
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
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)},
};
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

0 comments on commit e2b9d3c

Please sign in to comment.