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

Do not set primary key column before migration function is called #8057

Merged
merged 1 commit into from
Jan 3, 2025
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: 1 addition & 3 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 All @@ -28,7 +27,6 @@

### Fixed
* 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
* 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 @@ -140,9 +140,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 @@ -833,7 +831,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
Loading