Skip to content

Commit

Permalink
fix: avoid adding duplicate orderBys in startAfter()
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Chen committed Mar 22, 2021
1 parent 9cea104 commit e8ff476
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
3 changes: 3 additions & 0 deletions dev/src/reference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1521,6 +1521,9 @@ export class Query<T = firestore.DocumentData> implements firestore.Query<T> {
// If no explicit ordering is specified, use the first inequality to
// define an implicit order.
for (const fieldFilter of this._queryOptions.fieldFilters) {
if (FieldPath.documentId().isEqual(fieldFilter.field)) {
hasDocumentId = true;
}
if (fieldFilter.isInequalityFilter()) {
fieldOrders.push(new FieldOrder(fieldFilter.field));
break;
Expand Down
15 changes: 13 additions & 2 deletions dev/test/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import {
} from './util/helpers';

import api = google.firestore.v1;
import protobuf = google.protobuf;

const PROJECT_ID = 'test-project';
const DATABASE_ROOT = `projects/${PROJECT_ID}/databases/(default)`;
Expand Down Expand Up @@ -285,9 +286,10 @@ function endAt(
return {endAt: cursor};
}

export function queryEqualsWithParent(
export function queryEqualsWithParentAndReadTime(
actual: api.IRunQueryRequest | undefined,
parent: string,
readTime?: protobuf.ITimestamp,
...protoComponents: api.IStructuredQuery[]
): void {
expect(actual).to.not.be.undefined;
Expand Down Expand Up @@ -315,6 +317,10 @@ export function queryEqualsWithParent(
];
}

if (readTime) {
query.readTime = readTime;
}

// 'extend' removes undefined fields in the request object. The backend
// ignores these fields, but we need to manually strip them before we compare
// the expected and the actual request.
Expand All @@ -326,7 +332,12 @@ export function queryEquals(
actual: api.IRunQueryRequest | undefined,
...protoComponents: api.IStructuredQuery[]
): void {
queryEqualsWithParent(actual, '', ...protoComponents);
queryEqualsWithParentAndReadTime(
actual,
'',
/* readTime= */ undefined,
...protoComponents
);
}

function bundledQueryEquals(
Expand Down
49 changes: 46 additions & 3 deletions dev/test/recursive-delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ import {
import {
allDescendants,
fieldFilters,
orderBy,
queryEquals,
queryEqualsWithParent,
queryEqualsWithParentAndReadTime,
result,
select,
startAt as queryStartAt,
} from './query';
import {
createRequest,
Expand Down Expand Up @@ -147,9 +149,10 @@ describe('recursiveDelete() method:', () => {
it('for nested collections', async () => {
const overrides: ApiOverride = {
runQuery: req => {
queryEqualsWithParent(
queryEqualsWithParentAndReadTime(
req,
'root/doc',
/* readTime= */ undefined,
select('__name__'),
allDescendants(/* kindless= */ true),
fieldFilters(
Expand All @@ -173,9 +176,10 @@ describe('recursiveDelete() method:', () => {
it('documents', async () => {
const overrides: ApiOverride = {
runQuery: req => {
queryEqualsWithParent(
queryEqualsWithParentAndReadTime(
req,
'root/doc',
/* readTime= */ undefined,
select('__name__'),
allDescendants(/* kindless= */ true)
);
Expand All @@ -187,6 +191,45 @@ describe('recursiveDelete() method:', () => {
firestore = await createInstance(overrides);
return firestore.recursiveDelete(firestore.doc('root/doc'));
});

it('creates retry query after stream exception with last received doc', async () => {
let callCount = 0;
const overrides: ApiOverride = {
runQuery: request => {
callCount++;
if (callCount === 1) {
return stream(result('doc1'), new Error('failed in test'));
} else {
queryEqualsWithParentAndReadTime(
request,
/* parent= */ '',
/* readTime= */ {seconds: '5', nanos: 6},
select('__name__'),
allDescendants(/* kindless= */ true),
orderBy('__name__', 'ASCENDING'),
queryStartAt(false, {
referenceValue:
`projects/${PROJECT_ID}/databases/(default)/` +
'documents/collectionId/doc1',
}),
fieldFilters(
'__name__',
'GREATER_THAN_OR_EQUAL',
startAt('root'),
'__name__',
'LESS_THAN',
endAt('root')
)
);
return stream();
}
},
batchWrite: () => response(successResponse(1)),
};

const firestore = await createInstance(overrides);
await firestore.recursiveDelete(firestore.collection('root'));
});
});

describe('deletes', () => {
Expand Down

0 comments on commit e8ff476

Please sign in to comment.