Skip to content

Commit

Permalink
Merge branch 'master' into community
Browse files Browse the repository at this point in the history
  • Loading branch information
jedelbo committed Jan 3, 2025
2 parents 99abe88 + f3303e5 commit fd96376
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +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?.?.?)
* None.
* Changing type of primary key column crashes if more than one object ([#8056](https://github.com/realm/realm-core/issues/8056), since v14.13.2)

### Breaking changes
* None.
Expand Down
11 changes: 7 additions & 4 deletions src/realm/object-store/object_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,7 @@ 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.is_primary)
table.set_primary_key_column(key); // You can end here if this is a migration
else if (property.requires_index())
if (property.requires_index())
table.add_search_index(key);
else if (property.requires_fulltext_index())
table.add_fulltext_index(key);
Expand Down Expand Up @@ -824,7 +822,12 @@ static void apply_post_migration_changes(Group& group, std::vector<SchemaChange>
handle_backlinks_automatically);
}
void operator()(RemoveTable) {}
void operator()(ChangePropertyType) {}
void operator()(ChangePropertyType op)
{
if (op.new_property->is_primary) {
set_primary_key(table(op.object), op.new_property);
}
}
void operator()(MakePropertyNullable) {}
void operator()(MakePropertyRequired) {}
void operator()(AddProperty) {}
Expand Down
50 changes: 50 additions & 0 deletions test/object-store/migrations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,55 @@ TEST_CASE("migration: Automatic", "[migration]") {
});
}

SECTION("change primary key from int to string") {
using namespace std::string_literals;
Schema schema{{"Foo",
{
{"_id", PropertyType::Int, Property::IsPrimary{true}},
{"value", PropertyType::String},
}}};
Schema schema2{{"Foo",
{
{"_id", PropertyType::String, Property::IsPrimary{true}},
{"value", PropertyType::String},
}}};
TestFile config;
config.schema_mode = SchemaMode::Automatic;
config.schema = schema;
config.schema_version = 1;
{
auto realm = Realm::get_shared_realm(config);

CppContext ctx(realm);
std::any value1 = AnyDict{
{"_id", INT64_C(1)},
{"value", "foo"s},
};
std::any value2 = AnyDict{
{"_id", INT64_C(2)},
{"value", "bar"s},
};
realm->begin_transaction();
auto s = realm->schema().find("Foo");
Object::create(ctx, realm, *s, value1);
Object::create(ctx, realm, *s, value2);
realm->commit_transaction();
}
config.schema = schema2;
config.schema_version = 2;
config.migration_function = [](SharedRealm old_realm, SharedRealm realm, Schema&) {
Results r{Class(old_realm, &*old_realm->schema().find("Foo"))};
auto sz = r.size();
auto t = ObjectStore::table_for_object_type(realm->read_group(), "Foo");
for (size_t i = 0; i < sz; i++) {
Obj o = r.get(i);
auto new_obj = t->get_object(o.get_key());
new_obj.set("_id", util::to_string(o.get<Int>("_id")));
}
};
auto realm = Realm::get_shared_realm(config);
}

SECTION("change primary key from string to UUID without migration function") {
using namespace std::string_literals;
Schema schema{{"Foo",
Expand All @@ -1153,6 +1202,7 @@ TEST_CASE("migration: Automatic", "[migration]") {
auto realm = Realm::get_shared_realm(config);
realm->update_schema(schema2, 2);

// Make sure you can actually create an object with UUID as primary key
CppContext ctx(realm);
std::any values = AnyDict{
{"_id", UUID("3b241101-0000-0000-0000-4136c566a964"s)},
Expand Down

0 comments on commit fd96376

Please sign in to comment.