-
-
Notifications
You must be signed in to change notification settings - Fork 508
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
Fix upsert handling of nullable fields #1764
Conversation
This brings consistency to the upsert handling of fields with a null values. Non-nullable fields were not unset from the database to prevent loss of data, but nullable fields had their null value set in the daatabase. Since the object being upserted may be an incomplete object that doesn't completely represent the database state, we only write null values to the database if the upsert results in a document insertion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion to improve a comment, but LGTM otherwise.
@@ -329,11 +329,15 @@ private function executeUpsert($document, array $options) | |||
|
|||
foreach (array_keys($criteria) as $field) { | |||
unset($data['$set'][$field]); | |||
unset($data['$inc'][$field]); | |||
unset($data['$setOnInsert'][$field]); | |||
} | |||
|
|||
// Do not send an empty $set modifier |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps this should be updated to "Do not send empty update operators"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, changed it 👍
805f5ef
to
151f30d
Compare
Merge time! Thanks @alcaeus |
Based on the feedback on #1761, the handling for upserting fields with a null value is correct:
null
values should never overwrite pre-existing data in upsert queries, since the document being upserted is in an unknown state and we can't know for certain that the value should be unset.This PR brings this same behavior to fields with the
nullable
attribute set, where an upsert would always overwrite database values with anull
value: this is now only done if the upsert results in an insertion, thus preserving pre-existing data but ensuring the normal behavior of thenullable
flag is preserved.