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

feat: Add support for one-one mutation from sec. side #1247

Merged
merged 9 commits into from
Mar 29, 2023

Conversation

AndrewSisley
Copy link
Contributor

Relevant issue(s)

Resolves #1213 #1214

Description

Adds support for one-one mutation (create/update requests, client API) from the secondary side of a one-one relationship.

Suggest reviewing commit by commit, there is also additional context within the commit bodies that may be useful to reviewers. It also should make reading the tests easier as github has mangled the diff whilst reading them at a PR level.

@AndrewSisley AndrewSisley added feature New feature or request area/query Related to the query component area/collections Related to the collections system action/no-benchmark Skips the action that runs the benchmark. labels Mar 28, 2023
@AndrewSisley AndrewSisley added this to the DefraDB v0.5 milestone Mar 28, 2023
@AndrewSisley AndrewSisley requested a review from a team March 28, 2023 22:50
@AndrewSisley AndrewSisley self-assigned this Mar 28, 2023
@codecov
Copy link

codecov bot commented Mar 28, 2023

Codecov Report

Merging #1247 (8e0858f) into develop (f767394) will increase coverage by 0.08%.
The diff coverage is 77.96%.

Impacted file tree graph

@@             Coverage Diff             @@
##           develop    #1247      +/-   ##
===========================================
+ Coverage    70.57%   70.66%   +0.08%     
===========================================
  Files          182      182              
  Lines        17221    17251      +30     
===========================================
+ Hits         12154    12190      +36     
+ Misses        4132     4127       -5     
+ Partials       935      934       -1     
Impacted Files Coverage Δ
db/collection_update.go 71.67% <69.23%> (-0.50%) ⬇️
db/collection.go 68.84% <94.73%> (+0.14%) ⬆️
client/descriptions.go 89.18% <100.00%> (ø)

... and 6 files with indirect coverage changes

Copy link
Member

@shahzadlone shahzadlone left a comment

Choose a reason for hiding this comment

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

question: Some diffs were a bit odd this was showing as added and then after a bit of scroll I realized it was just moved: "Note: This test should probably not pass, as it contains a reference to a document that doesnt exist." So just wanted to confirm this isn't the byproduct of this PR right?

Rest looks G

return cid.Undef, err
}

// If this field was a secondary relation ID the related document will have been
Copy link
Member

Choose a reason for hiding this comment

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

praise: appreciate this comment

return relationFieldDescription, valid && !relationFieldDescription.IsPrimaryRelation()
}

// patchPrimaryDoc patches the primary document linked to from the given dockey via the given secondary
Copy link
Member

Choose a reason for hiding this comment

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

todo: reword please, this reads weird "... primary document linked to from the given dockey via the given secondary .."

Copy link
Collaborator

Choose a reason for hiding this comment

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

It is a bit hard to understand at the moment.

Copy link
Contributor Author

@AndrewSisley AndrewSisley Mar 29, 2023

Choose a reason for hiding this comment

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

will try and reword

  • reword doc

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have reworded the documentation slightly, but I have been finding it difficult to articulate what this function does in a more concise way than the code itself.

Comment on lines +50 to +52
// Note: This test should probably not pass, as it contains a
// reference to a document that doesnt exist. It is doubly odd
// given that saving from the secondary side errors as expected
Copy link
Member

Choose a reason for hiding this comment

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

question: Is there an issue for this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is conceptually part of the problem that we dont really do much/any data validation on write, I'm not sure their is an existing issue

Copy link
Member

Choose a reason for hiding this comment

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

See #935 for write validation

Copy link
Collaborator

@fredcarle fredcarle left a comment

Choose a reason for hiding this comment

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

LGTM. I guess now we have to fix mutations from the primary side?

Comment on lines +740 to +748
// NOTE: We delay the final Clean() call until we know
// the commit on the transaction is successful. If we didn't
// wait, and just did it here, then *if* the commit fails down
// the line, then we have no way to roll back the state
// side-effect on the document func called here.
txn.OnSuccess(func() {
doc.Clean()
})

Copy link
Collaborator

Choose a reason for hiding this comment

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

question: I wonder why this was in the loop before? I feel like it would have been a redundant additions to the OnSuccess functions.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I feel like it would have been a redundant additions to the OnSuccess functions.

yes :P

I wonder why this was in the loop before?

I assume it was just a historical accident and replaced some logic that predated the OnSuccess funcs without moving it

Copy link
Member

Choose a reason for hiding this comment

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

I think just me being dumb 🙃

return relationFieldDescription, valid && !relationFieldDescription.IsPrimaryRelation()
}

// patchPrimaryDoc patches the primary document linked to from the given dockey via the given secondary
Copy link
Collaborator

Choose a reason for hiding this comment

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

It is a bit hard to understand at the moment.

@AndrewSisley
Copy link
Contributor Author

AndrewSisley commented Mar 29, 2023

question: Some diffs were a bit odd this was showing as added and then after a bit of scroll I realized it was just moved: "Note: This test should probably not pass, as it contains a reference to a document that doesnt exist." So just wanted to confirm this isn't the byproduct of this PR right?

Rest looks G

This was noted in the description, at a PR level github has manged the diff, wasn't a problem last time I checked the commit-diffs. The issue mentioned predates this PR, and has been accidentally solved from the secondary side which now requires the doc to exist.

Was inverted, and returned false for primary relations and true for secondaries.
There is no point doing this for each field, and it risks bugs by calling it so late within the loop where it may be skipped over by 'continue' controls
Note: This does not affect the collection.Update function, as that goes via `c.save`, and not `c.applyPatch`.  That behaviour will be changed with either mutation-create, or a different ticket for those calls. It might be nice to investigate unifying these at somepoint, but not now.

The two new functions isSecondaryIDField and patchPrimaryDoc will be used as-is when adding support for wrong-side save calls.  They are broken into two functions due to the differences in `c.save` and `c.applyPatch`.  It may be worth reviewing these two functions within the context of both commits.
This includes support for request create mutations, and collection.Save calls.
@AndrewSisley AndrewSisley force-pushed the sisley/feat/I1213-one-one-create-2 branch from e206e53 to 8e0858f Compare March 29, 2023 17:48
@AndrewSisley AndrewSisley merged commit 84e88cc into develop Mar 29, 2023
@AndrewSisley AndrewSisley deleted the sisley/feat/I1213-one-one-create-2 branch March 29, 2023 18:06
shahzadlone pushed a commit that referenced this pull request Apr 13, 2023
* Remove commented out code

* Remove commented out code

* Fix IsPrimaryRelation

Was inverted, and returned false for primary relations and true for secondaries.

* Remove doc.Clean from loop

There is no point doing this for each field, and it risks bugs by calling it so late within the loop where it may be skipped over by 'continue' controls

* Expand right-side create test to query both directions

Should make sure both work.

* Add test for update mutation from primary side

* Add support for update from sec. side

Note: This does not affect the collection.Update function, as that goes via `c.save`, and not `c.applyPatch`.  That behaviour will be changed with either mutation-create, or a different ticket for those calls. It might be nice to investigate unifying these at somepoint, but not now.

The two new functions isSecondaryIDField and patchPrimaryDoc will be used as-is when adding support for wrong-side save calls.  They are broken into two functions due to the differences in `c.save` and `c.applyPatch`.  It may be worth reviewing these two functions within the context of both commits.

* Add support for c.save-mutations from sec. side

This includes support for request create mutations, and collection.Save calls.
shahzadlone pushed a commit to shahzadlone/defradb that referenced this pull request Feb 23, 2024
…1247)

* Remove commented out code

* Remove commented out code

* Fix IsPrimaryRelation

Was inverted, and returned false for primary relations and true for secondaries.

* Remove doc.Clean from loop

There is no point doing this for each field, and it risks bugs by calling it so late within the loop where it may be skipped over by 'continue' controls

* Expand right-side create test to query both directions

Should make sure both work.

* Add test for update mutation from primary side

* Add support for update from sec. side

Note: This does not affect the collection.Update function, as that goes via `c.save`, and not `c.applyPatch`.  That behaviour will be changed with either mutation-create, or a different ticket for those calls. It might be nice to investigate unifying these at somepoint, but not now.

The two new functions isSecondaryIDField and patchPrimaryDoc will be used as-is when adding support for wrong-side save calls.  They are broken into two functions due to the differences in `c.save` and `c.applyPatch`.  It may be worth reviewing these two functions within the context of both commits.

* Add support for c.save-mutations from sec. side

This includes support for request create mutations, and collection.Save calls.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
action/no-benchmark Skips the action that runs the benchmark. area/collections Related to the collections system area/query Related to the query component feature New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

One to One relationship create mutations fail from secondary direction
4 participants