Skip to content

Conversation

@rkistner
Copy link
Contributor

Specific scenario is when all of the following conditions are met:

  1. We're replicating from an Atlas Flex instance.
  2. There were changestream events recorded while the PowerSync service is paused.
  3. We're only replicating from a single database.

Then we've observed an ns with for example {db: '67b83e86cd20730f1e766dde_ps'}, instead of the expected {db: 'ps'} on the change stream.

This caused issues on multiple levels internally:

  1. We only used the collection name as the "relation_id" for the internal "source table" (bug 1).
  2. The storage module then detected the equivalent of a renamed collection, since it saw the same relation_id, but a different schema.
  3. We then truncate the table, but never did a snapshot of the "new" collection (bug 2).
  4. This resulted in the collection being missing, apart from documents specifically updated again.

This fixes the issue on multiple levels:

  1. Remove the relation_id for MongoDB source databases, and only rely on the db and collection names directly.
    1. This was simpler than consistently migrating the relation_id.
    2. We should probably do the same for MySQL, but that would require more testing.
    3. This means we can now correctly handle multiple collections of the same name in different dbs.
    4. This would avoid the "truncating" issue from above.
  2. If we do see a new collection, snapshot it. While we don't expect this to happen, it will allow for more graceful handling of edge cases if we run into any again.
  3. Implement a workaround for the incorrect ns from Flex instances.
  4. Add proper logging for operations like truncating collections, for if we do run into scenarios like these again.

There is another report with similar symptoms, but not using a Flex instance. See this thread.

@changeset-bot
Copy link

changeset-bot bot commented Feb 24, 2025

🦋 Changeset detected

Latest commit: 58ee591

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

This PR includes changesets to release 9 packages
Name Type
@powersync/service-module-postgres-storage Patch
@powersync/service-module-mongodb-storage Patch
@powersync/service-module-mongodb Patch
@powersync/service-core Patch
@powersync/service-module-mysql Patch
@powersync/service-module-postgres Patch
@powersync/service-image Patch
@powersync/service-core-tests 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

@rkistner rkistner changed the title [WIP] Fix truncating collections from MongoDB source database Fix truncating collections from MongoDB source database Feb 24, 2025
@rkistner rkistner marked this pull request as ready for review February 24, 2025 16:07
Copy link
Collaborator

@stevensJourney stevensJourney left a comment

Choose a reason for hiding this comment

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

LGTM. Tested MongoDB locally and verified with a Flex instance.

@rkistner rkistner merged commit 2f75fd7 into main Feb 25, 2025
20 checks passed
@rkistner rkistner deleted the fix-truncating-collections branch February 25, 2025 10:26
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