Skip to content

Fix: set exists:false precondition for deleted docs #6323

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

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
10 changes: 8 additions & 2 deletions packages/firestore/src/core/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,18 @@ export class Transaction {

/**
* Returns the version of this document when it was read in this transaction,
* as a precondition, or no precondition if it was not read.
* as a precondition, or an existence precondition if the document did not
Copy link
Contributor

Choose a reason for hiding this comment

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

This kind of list would normally only have one "or,"
so we'd have:

". . . as a precondition, an existence precondition if the document did not exist, or . . . ."

* exist, or no precondition if it was not read.
*/
private precondition(key: DocumentKey): Precondition {
const version = this.readVersions.get(key.toString());
if (!this.writtenDocs.has(key.toString()) && version) {
return Precondition.updateTime(version);
if (version.isEqual(SnapshotVersion.min())) {
// The document doesn't exist.
return Precondition.exists(false);
} else {
return Precondition.updateTime(version);
}
} else {
return Precondition.none();
}
Expand Down
52 changes: 52 additions & 0 deletions packages/firestore/test/integration/api/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
Firestore,
FirestoreError,
getDoc,
deleteDoc,
runTransaction,
setDoc
} from '../util/firebase_export';
Expand Down Expand Up @@ -610,6 +611,57 @@ apiDescribe('Database transactions', (persistence: boolean) => {
});
});

it('can set and delete inside a transaction on a deleted document', () => {
return withTestDb(persistence, db => {
const docRef = doc(collection(db, 'foo'));

// A function that reads a document then sets some data for it inside the
// context of the given transaction.
const readAndSet: (txn: Transaction) => Promise<Transaction> = (
txn: Transaction
) => txn.get(docRef).then(() => txn.set(docRef, { count: 16 }));

// A function that reads a document then deletes it inside the context of
// the given transaction.
const readAndDelete: (txn: Transaction) => Promise<Transaction> = (
txn: Transaction
) => txn.get(docRef).then(() => txn.delete(docRef));

return (
// Perform a readAndSet, then delete the document, then perform readAndSet
// again. This sequence of operations is valid and should succeed.
runTransaction(db, txn => readAndSet(txn))
.then(() => getDoc(docRef))
.then(snapshot => {
expect(snapshot.exists()).to.equal(true);
expect(snapshot.data()).to.deep.equal({ count: 16 });
})
.then(() => deleteDoc(docRef))
.then(() => getDoc(docRef))
.then(snapshot => expect(snapshot.exists()).to.equal(false))
// Perform a transaction to read and set for the second time.
.then(() => runTransaction(db, txn => readAndSet(txn)))
.then(() => getDoc(docRef))
.then(snapshot => {
expect(snapshot.exists()).to.equal(true);
expect(snapshot.data()).to.deep.equal({ count: 16 });
})
// Perform readAndDelete twice. This is also valid and should succeed.
.then(() => runTransaction(db, txn => readAndDelete(txn)))
.then(() => getDoc(docRef))
.then(snapshot => expect(snapshot.exists()).to.equal(false))
.then(() => runTransaction(db, txn => readAndDelete(txn)))
.then(() => getDoc(docRef))
.then(snapshot => {
expect(snapshot.exists()).to.equal(false);
})
.catch((err: FirestoreError) => {
expect.fail('Expected the transaction to succeed, but got ' + err);
})
);
});
});

// PORTING NOTE: These tests are for FirestoreDataConverter support and apply
// only to web.
apiDescribe('withConverter() support', (persistence: boolean) => {
Expand Down