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

Fix: Remove nullish values on set and on merge when no value is in storage #292

Merged

Conversation

chrispader
Copy link
Contributor

@chrispader chrispader commented Aug 2, 2023

@tgolen @marcaaron @hannojg

In the native implementation with SQLite, the first call to merge on a key where no value is in storage will act like a regular set since the values are inserted into storage directly.

This is due to this SQLite operation in SQLiteStorage.mergeItem:

INSERT INTO keyvaluepairs (record_key, valueJSON)
VALUES (:key, JSON(:value))
ON CONFLICT DO UPDATE
SET valueJSON = JSON_PATCH(valueJSON, JSON(:value));

In order for this to be consistent with how later merge with JSON_PATCH are working, we need to remove nullish values from the batchedChanged in the first merge operation.

Additionally, we'll want to remove nullish values on set as well, so that the values in cache and storage are always identical.

Details

Related Issues

Automated Tests

Linked PRs

@chrispader chrispader requested a review from a team as a code owner August 2, 2023 10:26
@chrispader chrispader changed the title Fix: remove nullish values on set and on merge when no value is in storage Fix: Remove nullish values on set and on merge when no value is in storage Aug 2, 2023
@melvin-bot melvin-bot bot requested review from bondydaa and removed request for a team August 2, 2023 10:26
@hannojg
Copy link
Contributor

hannojg commented Aug 2, 2023

To give another explanation to the issue at hand:

Calling Onyx.merge for a new key:

  • Onyx.merge(newKey, { prop1: 12, prop2: null})
  • Calls INSERT INTO as there is no conflict
  • In storage we now have: { prop1: 12, prop2: null}

Where as, when...

Calling Onyx.merge for an existing key:

  • Onyx.merge(key, { prop1: 12, prop2: null})
  • Calls JSON_PATCH as we have a conflict
  • In storage we now have: { prop1: 12 }

First of all, this is inconsistent behaviour, and secondly in the app code we don't expect the null values to be persisted, so it creates issues like this one:

mountiny
mountiny previously approved these changes Aug 2, 2023
Copy link
Contributor

@mountiny mountiny left a comment

Choose a reason for hiding this comment

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

Looking good but also tagging Marc and Tim for review

lib/Onyx.js Show resolved Hide resolved
Copy link
Collaborator

@tgolen tgolen left a comment

Choose a reason for hiding this comment

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

Left some comments to hopefully help the comments to be more clear to others.

lib/Onyx.js Outdated Show resolved Hide resolved
lib/Onyx.js Outdated Show resolved Hide resolved
lib/Onyx.js Outdated Show resolved Hide resolved
lib/Onyx.js Outdated Show resolved Hide resolved
lib/Onyx.js Outdated Show resolved Hide resolved
lib/Onyx.js Outdated Show resolved Hide resolved
marcaaron
marcaaron previously approved these changes Aug 2, 2023
Copy link
Contributor

@marcaaron marcaaron left a comment

Choose a reason for hiding this comment

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

Code LGTM. Agree with most of Tim's comments and think we could explain this edge case a bit better with some inline comments. @hannojg's description of the problem was very clear to me so maybe it can be incorporated somehow.

lib/Onyx.js Outdated Show resolved Hide resolved
lib/Onyx.js Outdated Show resolved Hide resolved
@chrispader chrispader dismissed stale reviews from marcaaron and mountiny via af25874 August 3, 2023 09:04
@hannojg
Copy link
Contributor

hannojg commented Aug 4, 2023

Awaiting another round of review, are we good to continue? This is blocking:

@mountiny @marcaaron @tgolen

@chrispader chrispader requested a review from tgolen August 4, 2023 09:23
Copy link
Collaborator

@tgolen tgolen left a comment

Choose a reason for hiding this comment

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

Love the comments. Very clear now. Thank you!

@tgolen tgolen merged commit 01ba77a into Expensify:main Aug 7, 2023
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants