couchbase / couchbase-lite-ios

Lightweight, embedded, syncable NoSQL database engine for iOS and MacOS apps.
Apache License 2.0
1.63k stars 297 forks source link

CBL-5514: Swift MutableDocument's collection is not set when a new document is saved #3287

Closed velicuvlad closed 5 months ago

velicuvlad commented 5 months ago
velicuvlad commented 5 months ago

Tests failing is unrelated to this PR. Somewhat flaky as it seems the timeout period is not big enough... all of them pass on local. Will investigate as part of the code coverage ticket.

velicuvlad commented 5 months ago

I can confirm that this is behaviour is already there for Obj-C. The Obj-C tests are just to confirm the behaviour.

A new MutableDoc is getting created without any collection ref, meanwhile it gets one when it is saved (as part of the process). I initially thought the change would be bigger as I didn't know that Swift has private setters (Pasin made me aware - kudos). For Swift the collection attribute was non-modifiable as it was a let.

What exactly do you refer as getting invalid? The C4Collection object? We do not allow the change of doc.collection so in the case you want to move a document, a brand new copy must be made anyway... I would expect that if the collection is invalid by magic, as it cannot be changed, that document is pretty much orphan and the solution becomes the same -> make a brand new copy of it

When we pull a doc out of the db, it does come attached with a collection so we already hold ref to it. I can't think of a case for which that collection would be invalided at the exact time of the save, when save is a collection operation - so if doesn't exist it will fail on save in the first place