Skip to content

Forbid queries endAt an uncommitted server timestamp. #1529

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 6 commits into from
Feb 12, 2019
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
17 changes: 14 additions & 3 deletions packages/firestore/src/api/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ import {
FieldValue,
FieldValueOptions,
ObjectValue,
RefValue
RefValue,
ServerTimestampValue
} from '../model/field_value';
import { DeleteMutation, Mutation, Precondition } from '../model/mutation';
import { FieldPath, ResourcePath } from '../model/path';
Expand Down Expand Up @@ -1557,7 +1558,8 @@ export class Query implements firestore.Query {
* position.
*
* Will throw if the document does not contain all fields of the order by
* of the query.
* of the query or if any of the fields in the order by are an uncommitted
* server timestamp.
*/
private boundFromDocument(
methodName: string,
Expand All @@ -1578,7 +1580,16 @@ export class Query implements firestore.Query {
components.push(new RefValue(this.firestore._databaseId, doc.key));
} else {
const value = doc.field(orderBy.field);
if (value !== undefined) {
if (value instanceof ServerTimestampValue) {
throw new FirestoreError(
Code.INVALID_ARGUMENT,
'Invalid query. You are trying to start or end a query using a ' +
'document for which the field "' +
orderBy.field +
'" is an uncommitted server timestamp. (Since the value of ' +
'this field is unknown, you cannot start/end a query with it.)'
);
} else if (value !== undefined) {
components.push(value);
} else {
const field = orderBy.field.canonicalString();
Expand Down
59 changes: 59 additions & 0 deletions packages/firestore/test/integration/api/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import * as firestore from '@firebase/firestore-types';
import { expect } from 'chai';

import { CACHE_SIZE_UNLIMITED } from '../../../src/api/database';
import { EventsAccumulator } from '../util/events_accumulator';
import firebase from '../util/firebase_export';
import {
ALT_PROJECT_ID,
Expand Down Expand Up @@ -795,6 +796,64 @@ apiDescribe('Validation:', persistence => {
});
});

validationIt(
persistence,
'cannot be sorted by an uncommitted server timestamp',
db => {
return withTestCollection(
persistence,
/*docs=*/ {},
async (collection: firestore.CollectionReference) => {
await db.disableNetwork();

const offlineAccumulator = new EventsAccumulator<
firestore.QuerySnapshot
>();
const onlineAccumulator = new EventsAccumulator<
firestore.QuerySnapshot
>();

const unsubscribe = collection.onSnapshot(snapshot => {
// Skip the initial empty snapshot.
if (snapshot.empty) return;

expect(snapshot.docs).to.have.lengthOf(1);
const docSnap: firestore.DocumentSnapshot = snapshot.docs[0];

if (snapshot.metadata.hasPendingWrites) {
// Offline snapshot. Since the server timestamp is uncommitted,
// we shouldn't be able to query by it.
expect(() =>
collection
.orderBy('timestamp')
.endAt(docSnap)
.onSnapshot(() => {})
).to.throw('uncommitted server timestamp');
offlineAccumulator.storeEvent(snapshot);
} else {
// Online snapshot. Since the server timestamp is committed, we
// should be able to query by it.
collection
.orderBy('timestamp')
.endAt(docSnap)
.onSnapshot(() => {});
onlineAccumulator.storeEvent(snapshot);
}
});

const doc: firestore.DocumentReference = collection.doc();
doc.set({ timestamp: FieldValue.serverTimestamp() });
await offlineAccumulator.awaitEvent();

await db.enableNetwork();
await onlineAccumulator.awaitEvent();

unsubscribe();
}
);
}
);

validationIt(
persistence,
'must not have more components than order by.',
Expand Down