Embedded objects migration: How to delete Orphaned objects after migration? #2393
-
Hi. the initial state of classes were following:
Then I decided to make MasterItemTruckExt an embedded object and did the following change.
This part is working fine. The next challenge is to write a migration function to consolidate old and new versions of realm databases. And I have the following migration function.
I have tried to remove orphaned objects with following:
then
I am getting this issue then I try this but again no luck.
So I would very happy to find any workaround how to resolve this. Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 5 replies
-
You'll need to remove them in the NewRealm - OldRealm is readonly. What you could do is add the following block at the end of the migration function: var toDelete = migration.NewRealm.DynamicApi.All("MasterItemTruckExt").Filter("@links.MasterItem.MasterItemTruckExt.@count == 0");
migration.NewRealm.RemoveRange(toDelete); I haven't tested this, so likely won't work from the get-go, but might get you on the right path. We'll try to look into this issue at some point this week and come up with a more comprehensive docs about it. Sorry for the omission. |
Beta Was this translation helpful? Give feedback.
-
I have added three new tests for this as part of #2394: @vardansargsyan92 As @nirinchev already mentioned, deleting objects in the You would have to iterate in the new realm therefore. The problem is now, looking at https://github.com/realm/realm-dotnet/blob/df/embedded-object-migration-test/Tests/Realm.Tests/Database/MigrationTests.cs#L272 specifically (which is the iteration we're talking about here - the one mentioned by Nikola) we see a crash. It originates in https://github.com/realm/realm-dotnet/blob/df/embedded-object-migration-test/Realm/Realm/Realm.cs#L1736 where the We check against the Metadata and find this to be set to I've fixed a bug in core a while ago where this was actually done pre migration: I'm going to investigate further. |
Beta Was this translation helpful? Give feedback.
I have added three new tests for this as part of #2394:
https://github.com/realm/realm-dotnet/blob/df/embedded-object-migration-test/Tests/Realm.Tests/Database/MigrationTests.cs#L149-L343
@vardansargsyan92 As @nirinchev already mentioned, deleting objects in the
oldRealm
is not going to have any effect, since that's not going to be forwarded to thenewRealm
while the migration is already in progress. TheoldRealm
is basically just a reference to the database using the old schema and thenewRealm
is the view onto the database using the new schema.You would have to iterate in the new realm therefore. The problem is now, looking at https://github.com/realm/realm-dotnet/blob/df/embedded-obje…