Repository navigation
CBEF-134: Write a derived owner once when its base-declared owned type changes - #197
davidkelly wants to merge 2 commits into
Conversation
…e changes Motivation ========== When an owned entity changes, the save also writes its owner's document unless the owner is already written. For an owned type declared on a base type, that check used the base type, but the main pass recorded the owner under its derived type. So a derived owner was written twice in one save: an insert and an upsert sent concurrently, and the insert failed with DocumentExistsException whenever the upsert landed first. Modification ============ - Use the owner entry's actual entity type in the owner pass. - Move write planning into PlanWrites, which builds the pending writes without sending them, so tests can check them without a cluster. Results ======= 4 unit tests count the planned writes (3 fail without the fix) and an integration test checks the stored documents (it failed 3 of 3 runs without the fix). 1,235 unit and 410 integration tests pass.
emilienbev
left a comment
There was a problem hiding this comment.
Good fix, and PlanWrites is a nice seam. The tests drive a real change tracker with no cluster.
My concerns.
- The lookup is still a string of the CLR short type name plus the key, and the keyspace is not part of it. So the same class of bug can come back. A set of owner entries would remove it.
- The worst case of the old bug has no test. A deleted derived owner used to plan an upsert next to the delete, so the document could come back.
- Bigger picture, see my note on #192 about the owned type path.
FYI, not for this PR. EF lets a deleted entity and a new one share a key in one save. We then send a delete and an insert for the same document in parallel. Cosmos handles that through SharedIdentityEntry. Maybe a ticket?
Motivation ========== The owner pass skipped an owner whose "ClrType.Name:key" string was already written. Two types with the same short name and key looked like one owner, so the second owner was not written. Modification ============ - Write an owner in the owner pass only if it is Unchanged; the main pass writes every added, modified or deleted owner. Dedupe by entry. - Remove writtenRoots. Walk to the root owner in FindRootOwner, with StateManager.FindPrincipal per hop. - Add unit tests: deleted derived owner, two changed owned types on one owner, and same-named owner types with the same key. - Drop the integration test's collection in a finally; refer to CBEF-136 for the owned-type field mapping. Results ======= 1,238 unit tests pass. The same-name test fails on the previous code; the new tests fail on the declaring-type logic.
|
Thanks @emilienbev. All seven points are in e234cfd. With the state check, the deleted-owner case can no longer plan an upsert, and there is now a test for it. The owned-type design point is in CBEF-136. For the delete + insert with a shared key, I filed CBEF-138. |
emilienbev
left a comment
There was a problem hiding this comment.
Looks good, a few small comments on the changelog and test comments.
| public Collar Collar { get; set; } = null!; | ||
| } | ||
|
|
||
| // Its own type and property name, and the test reads the documents directly, so it does not |
There was a problem hiding this comment.
Small thing. This comment says the test does not depend on how owned types map to fields. But lines 49 to 51 assert the flat Text and Color fields, and CBEF-136 plans to prefix them (Tag_Text). Could we say that here instead, so whoever lands CBEF-136 knows these asserts will change?
|
|
||
| ### Fixed | ||
|
|
||
| - **A derived entity is written once when an owned type declared on its base type changes.** With |
There was a problem hiding this comment.
Small thing. On main this bug also hit deletes. Removing a derived owner planned an upsert next to the delete, so the document could survive the delete. Could we add a sentence for that?
| namespace Couchbase.EntityFrameworkCode.IntegrationTests.Tests; | ||
|
|
||
| /// <summary> | ||
| /// A derived entity (TPH) with an owned type declared on its base type used to be written twice in |
There was a problem hiding this comment.
Nit: this summary still tells the history of the bug, like the unit test one did. Maybe keep only the last part, that it checks the round trip on a cluster and reads the documents directly?
The issue in brief: with TPH inheritance, a derived entity with an owned type declared on its base type was written twice in one
SaveChanges. When an owned entity changes, the save also writes the owner's document unless the owner is already written. That check looked the owner up under the type that declares the owned navigation (the base type), while the main pass had recorded it under its actual (derived) type. So the derived entity got an insert and an upsert sent at the same time, and the insert failed withDocumentExistsExceptionwhenever the upsert landed first. This made the JsonQuery spec fixture's seed fail (all 438 tests in CI).Change
PlanWrites, which builds the pending writes without sending them.SaveChangesAsynccalls it and then sends the writes as before, so behaviour does not change. It makes the double write testable without a cluster.Tests
CouchbaseOwnerWriteTests, no cluster): plans the writes and checks that each document is written once. Cases: an added derived entity, base and derived added together, a modified derived entity with a changed base-declared owned type, only the owned type changed (one upsert), a deleted derived entity (only a delete), one owner with two changed owned types (written once), and two owner types with the same class name and key (both written). Without the fix, the first three fail; the others guard the owner pass. 1,238/1,238 pass.DerivedOwnerWriteTests): adds a base and a derived entity, then changes the derived entity and its base-declared owned type, and checks the stored documents with the SDK. Without the fix it failed 3 of 3 runs withDocumentExistsException. Full suite 410/410.The integration test reads the documents directly, because two other bugs found while writing it would get in the way. Owned references with a property of the same name are flattened into one field. An
OwnsOnedeclared on a TPH base type reads back with null properties. Both are part of CBEF-136. The test givesCollarits own property name for that reason.JsonQuery tests still fail after this. They also need CBEF-135 (#196) and the known "relational model built twice for JSON entity types" fix.
Ticket: CBEF-134
🤖 Generated with Claude Code