From 5c6ec01618b609791b582d2afc7a21b883429817 Mon Sep 17 00:00:00 2001 From: Djoume Salvetti Date: Tue, 24 Sep 2024 16:10:06 -0400 Subject: [PATCH] Fixes firestore replication of multiple document with the same serverTimestamp (#6436) --- src/plugins/replication-firestore/index.ts | 4 +-- test/replication-firestore.test.ts | 35 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/src/plugins/replication-firestore/index.ts b/src/plugins/replication-firestore/index.ts index 389b9d8053c..3eab5ffcacc 100644 --- a/src/plugins/replication-firestore/index.ts +++ b/src/plugins/replication-firestore/index.ts @@ -138,8 +138,8 @@ export function replicateFirestore( ); sameTimeQuery = query(pullQuery, where(serverTimestampField, '==', lastServerTimestamp), - where(primaryPath, '>', lastPulledCheckpoint.id), - orderBy(primaryPath, 'asc'), + where(documentId(), '>', lastPulledCheckpoint.id), + orderBy(documentId(), 'asc'), limit(batchSize) ); } else { diff --git a/test/replication-firestore.test.ts b/test/replication-firestore.test.ts index 2c283766d36..e9aacad2772 100644 --- a/test/replication-firestore.test.ts +++ b/test/replication-firestore.test.ts @@ -420,5 +420,40 @@ describe('replication-firestore.test.ts', function () { collection.database.destroy(); }); + it('replicates all docs with identical serverTimestamp from the server', async () => { + const firestoreState = getFirestoreState(); + const collection = await humansCollection.create(0); + + const now = new Date(); + const h1 = { + ...makeFirestoreHumanDocument( + schemaObjects.humanData('replicated-1', 35, 'replicated') + ), + serverTimestamp: now, + }; + const h2 = { + ...makeFirestoreHumanDocument( + schemaObjects.humanData('replicated-2', 27, 'replicated') + ), + serverTimestamp: now, + }; + + await setDoc(DocRef(firestoreState.collection, 'replicated-1'), h1); + await setDoc(DocRef(firestoreState.collection, 'replicated-2'), h2); + + await syncOnce(collection, firestoreState, { + pull: { + batchSize: 1, + }, + push: {}, + }); + + const allLocalDocs = await collection.find().exec(); + + assert.strictEqual(allLocalDocs.length, 2); + + collection.database.destroy(); + }); + }); });