Skip to content

Conversation

@stevensJourney
Copy link
Collaborator

@stevensJourney stevensJourney commented Dec 17, 2024

Overview

For reference see Discord thread.

The resolved sync buckets would be missing entries which matched data after updating (moving) relational linked columns.

Root Cause

The getParameterSets method was not returning the correct parameter sets after updating a todo_list's list_id column. The query fetched all bucket_parameters that:

  • Match the provided lookups (related to the input list_ids),
  • Have _id values smaller than the current checkpoint, and
  • Match the group_id (sync rules set).

The issue arose during the $group stage, where results were grouped by the key field alone. Since the key was not unique for different lookup values, updates introducing REMOVE entries with empty bucket_parameters could override valid entries, leading to incorrect results.

Steps to Reproduce

See the screenshot of the bucket_parameters collection in MongoDB before and after executing an UPDATE todos set list_id = 2 where id = 12.

Initial State:

Todo id=12 belongs to list id=10 with its corresponding lookup and bucket_parameters.
Todo id=11 belongs to list id=2.

The bucket_parameters entries match the input lookups for:

  • list_id=10 => todo_id=12 and
  • list_id=2 => todo_id=11

Update:

Update Todo id=12 to belong to list id=2..

This creates a bucket_parameters entry which now associates list_id=2 => todo_id=12 and a REMOVE bucket_parameters entry which removes the association of list_id=10 (lookup) to todo_id=12. The REMOVE entry is created after the new association.

Issue:

When grouping results by key, the REMOVE entry (with empty bucket_parameters) overrides valid entries with the same key but a different lookup (e.g., for list 2).

Refer to the attached screenshot for a clear visual of the issue:

image

Before the update: the relevant lookups for each todo item are valid.

After the update: A REMOVE operation appears, the grouping in the getParameterSets query will return the bucket_parameters for the last entry which is the empty array from the remove entry.

Solution

The $group stage was updated to use a composite _id object:

{ key: '$key', lookup: '$lookup' }

This ensures that each unique combination of key and lookup is preserved in the grouped results, preventing REMOVE entries from overriding valid data.

Verification
Tested the fix by reproducing the issue as described above.

After applying the fix, the query correctly groups results by both key and lookup, preserving the valid bucket_parameters for each combination.

@changeset-bot
Copy link

changeset-bot bot commented Dec 17, 2024

🦋 Changeset detected

Latest commit: 2298b5c

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 6 packages
Name Type
@powersync/service-core Patch
@powersync/service-image Patch
@powersync/service-module-mongodb Patch
@powersync/service-module-mysql Patch
@powersync/service-module-postgres Patch
test-client Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@stevensJourney stevensJourney marked this pull request as ready for review December 17, 2024 08:14
@stevensJourney stevensJourney merged commit 320e646 into main Dec 17, 2024
15 checks passed
@stevensJourney stevensJourney deleted the lookup_test branch December 17, 2024 13:08
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.

2 participants