Skip to content

Fix transaction.set() failure without retry on "already-exists" error #6729

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/young-knives-shake.md
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 snap1 = await transaction.get(docRef);

if (retryCounter === 1) {
expect(snap1.exists()).to.be.false;
// On the first attemp, create a doc before transaction.set(), so that
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: "attemp" -> "attempt"

// 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 result = await getDoc(docRef);
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: Please rename result to snapshot.

expect(result.get('count')).to.equal(2);
});
});

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