Skip to content

Commit

Permalink
Fix transaction.set() failure without retry on "already-exists" error (
Browse files Browse the repository at this point in the history
  • Loading branch information
milaGGL committed Oct 28, 2022
1 parent d7acc96 commit e2a90bf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .changeset/young-knives-shake.md
@@ -0,0 +1,6 @@
---
'@firebase/firestore': patch
'firebase': patch
---

Fix transaction.set() failure without retry on "already-exists" error.
1 change: 1 addition & 0 deletions packages/firestore/src/core/transaction_runner.ts
Expand Up @@ -120,6 +120,7 @@ export class TransactionRunner<T> {
return (
code === 'aborted' ||
code === 'failed-precondition' ||
code === 'already-exists' ||
!isPermanentError(code)
);
}
Expand Down
24 changes: 24 additions & 0 deletions packages/firestore/test/integration/api/transactions.test.ts
Expand Up @@ -629,6 +629,30 @@ apiDescribe('Database transactions', (persistence: boolean) => {
});
});

it('retries when document already exists', () => {
return withTestDb(persistence, async db => {
let retryCounter = 0;
const docRef = doc(collection(db, 'nonexistent'));

await runTransaction(db, async transaction => {
++retryCounter;
const snap = await transaction.get(docRef);

if (retryCounter === 1) {
expect(snap.exists()).to.be.false;
// On the first attempt, create a doc before transaction.set(), so that
// the transaction fails with "already-exists" error, and retries.
await setDoc(docRef, { count: 1 });
}

transaction.set(docRef, { count: 2 });
});
expect(retryCounter).to.equal(2);
const snap = await getDoc(docRef);
expect(snap.get('count')).to.equal(2);
});
});

it('are successful with no transaction operations', () => {
return withTestDb(persistence, db => runTransaction(db, async () => {}));
});
Expand Down

0 comments on commit e2a90bf

Please sign in to comment.