Skip to content

Commit 4aca0cb

Browse files
ninjaAB-5joehan
andauthored
Always keep __name__ field in indexes when list indexes (#9160)
* Always keep the __name__ field in index when list indexes * fix unit tests * teach indexMatchesSpec to understand __name__ normalization of standard database * update CHANGELOG and adapt changes to unit tests --------- Co-authored-by: Joe Hanley <[email protected]>
1 parent f6cb63a commit 4aca0cb

File tree

3 files changed

+182
-149
lines changed

3 files changed

+182
-149
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
- Fixed an issue with deploying indexes to Firestore Enterprise edition databases where explicit `__name__` fields could be incorrectly handled.
12
- The `experimental:mcp` command has been renamed to `mcp`. The old name is now an alias.
23
- `firebase_update_environment` MCP tool supports accepting Gemini in Firebase Terms of Service.
34
- Fixed a bug when `firebase init dataconnect` failed to create a React app when launched from VS Code extension (#9171).

src/firestore/api.ts

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,28 +21,26 @@ export class FirestoreApi {
2121
printer = new PrettyPrint();
2222

2323
/**
24-
* Process indexes by filtering out implicit __name__ fields with ASCENDING order.
25-
* Keeps explicit __name__ fields with DESCENDING order.
26-
* @param indexes Array of indexes to process
27-
* @return Processed array of indexes with filtered fields
24+
* Process indexes by appending the implicit __name__ fields with default order for STANDARD edition database.
25+
* No-op if exists __name__ field at the end.
26+
* No-op is ENTERPRISE edition databases.
27+
* @param index Spec index to process
28+
* @return Processed spec index with potential additional __name__ suffix
2829
*/
29-
public static processIndexes(indexes: types.Index[]): types.Index[] {
30-
return indexes.map((index: types.Index): types.Index => {
31-
// Per https://firebase.google.com/docs/firestore/query-data/index-overview#default_ordering_and_the_name_field
32-
// this matches the direction of the last non-name field in the index.
33-
let fields = index.fields;
34-
const lastField = index.fields?.[index.fields.length - 1];
35-
if (lastField?.fieldPath === "__name__") {
36-
const defaultDirection = index.fields?.[index.fields.length - 2]?.order;
37-
if (lastField?.order === defaultDirection) {
38-
fields = fields.slice(0, -1);
39-
}
40-
}
41-
return {
42-
...index,
43-
fields,
44-
};
45-
});
30+
public static processIndex(index: Spec.Index): Spec.Index {
31+
// Per https://firebase.google.com/docs/firestore/query-data/index-overview#default_ordering_and_the_name_field
32+
// this matches the direction of the last non-name field in the index.
33+
const fields = index.fields;
34+
const lastField = index.fields?.[index.fields.length - 1];
35+
if (lastField?.fieldPath !== "__name__") {
36+
const defaultDirection = index.fields?.[index.fields.length - 1]?.order;
37+
const nameSuffix = { fieldPath: "__name__", order: defaultDirection } as types.IndexField;
38+
fields.push(nameSuffix);
39+
}
40+
return {
41+
...index,
42+
fields,
43+
};
4644
}
4745

4846
/**
@@ -212,7 +210,7 @@ export class FirestoreApi {
212210
return [];
213211
}
214212

215-
return FirestoreApi.processIndexes(indexes);
213+
return indexes;
216214
}
217215

218216
/**
@@ -559,14 +557,19 @@ export class FirestoreApi {
559557

560558
// TODO(b/439901837): Compare `unique` index configuration when it's supported.
561559

562-
if (index.fields.length !== spec.fields.length) {
560+
let specIdx = spec;
561+
if (edition === DatabaseEdition.STANDARD) {
562+
specIdx = FirestoreApi.processIndex(specIdx);
563+
}
564+
565+
if (index.fields.length !== specIdx.fields.length) {
563566
return false;
564567
}
565568

566569
let i = 0;
567570
while (i < index.fields.length) {
568571
const iField = index.fields[i];
569-
const sField = spec.fields[i];
572+
const sField = specIdx.fields[i];
570573

571574
if (iField.fieldPath !== sField.fieldPath) {
572575
return false;

0 commit comments

Comments
 (0)