Skip to content

Commit e2a90bf

Browse files
authored
Fix transaction.set() failure without retry on "already-exists" error (#6729)
1 parent d7acc96 commit e2a90bf

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

.changeset/young-knives-shake.md

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/firestore': patch
3+
'firebase': patch
4+
---
5+
6+
Fix transaction.set() failure without retry on "already-exists" error.

packages/firestore/src/core/transaction_runner.ts

+1
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ export class TransactionRunner<T> {
120120
return (
121121
code === 'aborted' ||
122122
code === 'failed-precondition' ||
123+
code === 'already-exists' ||
123124
!isPermanentError(code)
124125
);
125126
}

packages/firestore/test/integration/api/transactions.test.ts

+24
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,30 @@ apiDescribe('Database transactions', (persistence: boolean) => {
629629
});
630630
});
631631

632+
it('retries when document already exists', () => {
633+
return withTestDb(persistence, async db => {
634+
let retryCounter = 0;
635+
const docRef = doc(collection(db, 'nonexistent'));
636+
637+
await runTransaction(db, async transaction => {
638+
++retryCounter;
639+
const snap = await transaction.get(docRef);
640+
641+
if (retryCounter === 1) {
642+
expect(snap.exists()).to.be.false;
643+
// On the first attempt, create a doc before transaction.set(), so that
644+
// the transaction fails with "already-exists" error, and retries.
645+
await setDoc(docRef, { count: 1 });
646+
}
647+
648+
transaction.set(docRef, { count: 2 });
649+
});
650+
expect(retryCounter).to.equal(2);
651+
const snap = await getDoc(docRef);
652+
expect(snap.get('count')).to.equal(2);
653+
});
654+
});
655+
632656
it('are successful with no transaction operations', () => {
633657
return withTestDb(persistence, db => runTransaction(db, async () => {}));
634658
});

0 commit comments

Comments
 (0)