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

Automatic handling backlinks migration #5737

Merged
merged 31 commits into from
Aug 29, 2022

Conversation

nicola-cab
Copy link
Member

@nicola-cab nicola-cab commented Aug 11, 2022

What, How & Why?

Automatically handle backlinks migration from TopLevel ==> Embedded objects.
In case of 0 backlinks, delete automatically the object
In case of multiple backlinks, duplicate the object and assign 1 parent to each object.
Fixes: #4729
Fixes: #3898

☑️ ToDos

  • 📝 Changelog update
  • 🚦 Tests (or not relevant)
  • C-API, if public C++ API changed.

@cla-bot cla-bot bot added the cla: yes label Aug 11, 2022
test/object-store/migrations.cpp Outdated Show resolved Hide resolved
src/realm/object-store/object_store.cpp Show resolved Hide resolved
test/object-store/migrations.cpp Outdated Show resolved Hide resolved
src/realm/obj.cpp Outdated Show resolved Hide resolved
src/realm/obj.cpp Show resolved Hide resolved
src/realm/obj.cpp Outdated Show resolved Hide resolved
src/realm/obj.cpp Outdated Show resolved Hide resolved
Copy link
Member

@nirinchev nirinchev left a comment

Choose a reason for hiding this comment

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

Are we sure this is the API we want to expose to users? Particularly, I'm wondering if it's better to have this as a function that the user explicitly invokes, similarly to rename_property.

Alternatively, if we keep it as a property on the config, should the default be false or true? I can see some value in opting in for true as that would make sure such migrations work automatically, but I can understand the hesitance as it may mean deleting user data.

@nicola-cab
Copy link
Member Author

rename_property

In relation to add a new function. Yes, I could add a new function in object_store and propagate it in the CAPI. However, its usage is kind of meaningless outside the migration callback itself. I mean, if there are objects that are not respecting the constraints, core will throw an exception before client code could do anything meaningful with this new API.
But we can expose it, if you think SDKs would benefit from this.

I personally think that a new setting makes more sense in this case (but I am open to whatever we decide to do) ... and yes there is some value in letting core fix things automatically, but deleting user data should not be done without explicitly asking for it.
Maybe, we could set it to true by default once we know our users like the feature.

@nicola-cab
Copy link
Member Author

@ironage I haven't added the logic for constructing the list of embedded backlinks. So the PR is not yet ready to be merged.

CHANGELOG.md Outdated Show resolved Hide resolved
src/realm/obj.cpp Outdated Show resolved Hide resolved
src/realm/obj.cpp Outdated Show resolved Hide resolved
src/realm/obj.cpp Outdated Show resolved Hide resolved
test/object-store/migrations.cpp Outdated Show resolved Hide resolved
@nicola-cab nicola-cab marked this pull request as draft August 19, 2022 19:10
@nicola-cab
Copy link
Member Author

I am demoting this to draft, I need to clean up the code, fix a bunch of issues and review the main functions, this is not by any means ready for review/production

@nicola-cab nicola-cab marked this pull request as ready for review August 22, 2022 13:14
@nicola-cab
Copy link
Member Author

this is now ready for review @ironage

src/realm/obj.cpp Outdated Show resolved Hide resolved
src/realm/object-store/object_store.cpp Outdated Show resolved Hide resolved
src/realm/obj.cpp Show resolved Hide resolved
src/realm/obj.cpp Outdated Show resolved Hide resolved
src/realm/obj.cpp Outdated Show resolved Hide resolved
src/realm/obj.cpp Outdated Show resolved Hide resolved
src/realm/obj.cpp Outdated Show resolved Hide resolved
src/realm/obj.cpp Outdated Show resolved Hide resolved
@nicola-cab
Copy link
Member Author

@ironage I fixed all the comments, now I am going to try to integrate your PR: #5773 into this one.

ironage and others added 2 commits August 25, 2022 09:54
* move converters to core level

* use object_converters in migration

* handle mixed non-pk objects in converter
src/realm/CMakeLists.txt Outdated Show resolved Hide resolved
src/realm/obj.cpp Outdated Show resolved Hide resolved
src/realm/obj.hpp Outdated Show resolved Hide resolved
dst_link = m_opposite_of_dst->get_object(src_link_key);
}
else {
// in different Realms we create a new object
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it relevant to support a case where we copy between 2 different realms in case the object does not have a primary key? I guess it will be hard to create a test case for this?

Copy link
Member Author

Choose a reason for hiding this comment

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

I would not add a test for this right now, I don't think this could happen within the context of a schema migration.

converted_src = ObjLink{dst_link_table->get_key(), dst_link.get_key()};
}
else {
// no pk, and different Realm, create an object
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above

Copy link
Member Author

@nicola-cab nicola-cab Aug 26, 2022

Choose a reason for hiding this comment

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

I would say we could try to add a test covering this case later, I am not sure if this could happen within the context of a schema migration. We are reusing this code mostly because I ended up rewriting the same logic we already have in place for client reset.

@nicola-cab nicola-cab requested a review from jedelbo August 26, 2022 15:32
@sipersso
Copy link

FIY: Just tried this in the Kotlin SDK and the orphaned embedded objects did not get deleted automatically. I have a unit test for this and the app just crashes. I created a separate issue here: realm/realm-kotlin#1464

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Automatic handling of backlinks when migrating to EmbeddedObject Expose a Obj::get_parent API
5 participants