Block writes during schema migration to prevent conflicts#8475
Open
Block writes during schema migration to prevent conflicts#8475
Conversation
Outside writes that race with a running migration could conflict with the migration replication, which surfaced as confusing RC_PUSH errors. Block inserts/upserts/removes and RxDocument modifications via a new ensureRxCollectionIsNotMigrating guard (COL25) while migrationInProgress is true on the collection.
…gration is needed - Replace ensureRxCollectionIsNotClosed + ensureRxCollectionIsNotMigrating pairs at write call sites with a single isWriteAllowed helper that asserts both conditions. - Set migrationInProgress eagerly in the migration plugin's createRxCollection hook for schemas with version > 0, so writes are refused with COL25 even before startMigration() has been called. Cleared once migrationNeeded() resolves false or once the migration finishes/errors/cancels.
Contributor
✅ Verify Test Reproduction: Tests FAILED without the fix (expected)This confirms the changed tests correctly reproduce the bug that the source changes fix. This workflow runs the changed tests without the source fix to verify they reproduce the bug. Show output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR contains:
Describe the problem you have without this PR
When a schema migration is running on an RxCollection, external writes (insert, upsert, bulkInsert, etc.) could race with the migration's replication process. This could cause confusing
RC_PUSHerrors or other conflicts instead of failing fast with a clear error message.Additionally, writes were allowed even when a migration was pending but not yet started, which could interfere with the migration process.
Solution
This PR implements a
migrationInProgressflag on RxCollection that:Blocks all writes during migration: The flag is set when a migration starts and cleared when it completes, fails, or is cancelled. Any write attempt while the flag is true throws a clear
COL25error.Blocks writes when migration is pending: The flag is optimistically set when a collection is created (for schema version > 0), then cleared if no migration is actually needed. This prevents writes from interfering with pending migrations.
Provides clear error messaging: Instead of racing errors, users get an immediate
COL25error with guidance to wait for the migration to complete.Changes made:
migrationInProgressproperty toRxCollectionBaseisWriteAllowed()helper function that checks both closed state and migration stateensureRxCollectionIsNotClosed()calls withisWriteAllowed()in all write methods (insert, bulkInsert, upsert, bulkUpsert, incrementalUpsert, bulkRemove)migrationInProgressflag when collection is createdRxMigrationState.startMigration()to manage the flag throughout the migration lifecycleCOL25error definition and documentation warningTest Plan
Added 4 new unit tests in
migration-schema.test.ts:migrationInProgressflag is reset on migration errorAll tests pass and cover the key scenarios where writes could interfere with migrations.
https://claude.ai/code/session_01PFvmQnnz2GCozXqMfCjYvy