Skip to content

Correct handling of __name__ in firestore indexes #8862

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
Jul 17, 2025
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- Fixed issue where `__name__` fields with DESCENDING order were incorrectly filtered from index listings, causing duplicate index issues (#7629) and deployment conflicts (#8859). The fix now preserves `__name__` fields with explicit DESCENDING order while filtering out implicit ASCENDING `__name__` fields.
27 changes: 26 additions & 1 deletion src/firestore/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,34 @@
apiClient = new Client({ urlPrefix: firestoreOrigin(), apiVersion: "v1" });
printer = new PrettyPrint();

/**
* Process indexes by filtering out implicit __name__ fields with ASCENDING order.
* Keeps explicit __name__ fields with DESCENDING order.
* @param indexes Array of indexes to process
* @returns Processed array of indexes with filtered fields

Check warning on line 25 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Invalid JSDoc tag (preference). Replace "returns" JSDoc tag with "return"
*/
public static processIndexes(indexes: types.Index[]): types.Index[] {
return indexes.map((index: types.Index): types.Index => {
// Per https://firebase.google.com/docs/firestore/query-data/index-overview#default_ordering_and_the_name_field
// this matches the direction of the last non-name field in the index.
let fields = index.fields;
const lastField = index.fields?.[index.fields.length - 1];
if (lastField?.fieldPath === "__name__") {
const defaultDirection = index.fields?.[index.fields.length - 2]?.order;
if (lastField?.order === defaultDirection) {
fields = fields.slice(0, -1);
}
}
return {
...index,
fields,
};
});
}

/**
* Deploy an index specification to the specified project.
* @param options the CLI options.

Check warning on line 48 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing @param "options.force"

Check warning on line 48 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing @param "options.nonInteractive"

Check warning on line 48 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Missing @param "options.project"
* @param indexes an array of objects, each will be validated and then converted
* to an {@link Spec.Index}.
* @param fieldOverrides an array of objects, each will be validated and then
Expand All @@ -28,11 +53,11 @@
*/
async deploy(
options: { project: string; nonInteractive: boolean; force: boolean },
indexes: any[],

Check warning on line 56 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
fieldOverrides: any[],

Check warning on line 57 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unexpected any. Specify a different type
databaseId = "(default)",
): Promise<void> {
const spec = this.upgradeOldSpec({

Check warning on line 60 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
indexes,
fieldOverrides,
});
Expand All @@ -40,8 +65,8 @@
this.validateSpec(spec);

// Now that the spec is validated we can safely assert these types.
const indexesToDeploy: Spec.Index[] = spec.indexes;

Check warning on line 68 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe member access .indexes on an `any` value

Check warning on line 68 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value
const fieldOverridesToDeploy: Spec.FieldOverride[] = spec.fieldOverrides;

Check warning on line 69 in src/firestore/api.ts

View workflow job for this annotation

GitHub Actions / lint (20)

Unsafe assignment of an `any` value

const existingIndexes: types.Index[] = await this.listIndexes(options.project, databaseId);
const existingFieldOverrides: types.Field[] = await this.listFieldOverrides(
Expand Down Expand Up @@ -183,7 +208,7 @@
return [];
}

return indexes;
return FirestoreApi.processIndexes(indexes);
}

/**
Expand Down
140 changes: 140 additions & 0 deletions src/firestore/indexes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,146 @@ describe("IndexSpecMatching", () => {
});
});

describe("IndexListingWithNameFields", () => {
it("should filter out __name__ fields with in the default order, when the default is ASCENDING", () => {
const mockIndexes: API.Index[] = [
{
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123",
queryScope: API.QueryScope.COLLECTION,
fields: [
{ fieldPath: "foo", order: API.Order.ASCENDING },
{ fieldPath: "__name__", order: API.Order.ASCENDING },
],
state: API.State.READY,
},
];

const result = FirestoreApi.processIndexes(mockIndexes);

expect(result[0].fields).to.have.length(1);
expect(result[0].fields[0].fieldPath).to.equal("foo");
});

it("should filter out __name__ fields with in the default order, when the default is DESCENDING", () => {
const mockIndexes: API.Index[] = [
{
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123",
queryScope: API.QueryScope.COLLECTION,
fields: [
{ fieldPath: "foo", order: API.Order.DESCENDING },
{ fieldPath: "__name__", order: API.Order.DESCENDING },
],
state: API.State.READY,
},
];

const result = FirestoreApi.processIndexes(mockIndexes);

expect(result[0].fields).to.have.length(1);
expect(result[0].fields[0].fieldPath).to.equal("foo");
});

it("should keep __name__ fields with DESCENDING order, when the default is ASCENDING", () => {
const mockIndexes: API.Index[] = [
{
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123",
queryScope: API.QueryScope.COLLECTION,
fields: [
{ fieldPath: "foo", order: API.Order.ASCENDING },
{ fieldPath: "__name__", order: API.Order.DESCENDING },
],
state: API.State.READY,
},
];

const result = FirestoreApi.processIndexes(mockIndexes);

expect(result[0].fields).to.have.length(2);
expect(result[0].fields[0].fieldPath).to.equal("foo");
expect(result[0].fields[1].fieldPath).to.equal("__name__");
expect(result[0].fields[1].order).to.equal(API.Order.DESCENDING);
});

it("should keep __name__ fields with ASCENDING order, when the default is DESCENDING", () => {
const mockIndexes: API.Index[] = [
{
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123",
queryScope: API.QueryScope.COLLECTION,
fields: [
{ fieldPath: "foo", order: API.Order.DESCENDING },
{ fieldPath: "__name__", order: API.Order.ASCENDING },
],
state: API.State.READY,
},
];

const result = FirestoreApi.processIndexes(mockIndexes);

expect(result[0].fields).to.have.length(2);
expect(result[0].fields[0].fieldPath).to.equal("foo");
expect(result[0].fields[1].fieldPath).to.equal("__name__");
expect(result[0].fields[1].order).to.equal(API.Order.ASCENDING);
});

it("should distinguish between indexes that differ only by __name__ order", () => {
const mockIndexes: API.Index[] = [
{
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123",
queryScope: API.QueryScope.COLLECTION,
fields: [
{ fieldPath: "foo", order: API.Order.ASCENDING },
{ fieldPath: "__name__", order: API.Order.ASCENDING },
],
state: API.State.READY,
},
{
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/def456",
queryScope: API.QueryScope.COLLECTION,
fields: [
{ fieldPath: "foo", order: API.Order.ASCENDING },
{ fieldPath: "__name__", order: API.Order.DESCENDING },
],
state: API.State.READY,
},
];

const result = FirestoreApi.processIndexes(mockIndexes);

// First index should have __name__ field filtered out
expect(result[0].fields).to.have.length(1);
expect(result[0].fields[0].fieldPath).to.equal("foo");

// Second index should keep __name__ field with DESCENDING order
expect(result[1].fields).to.have.length(2);
expect(result[1].fields[0].fieldPath).to.equal("foo");
expect(result[1].fields[1].fieldPath).to.equal("__name__");
expect(result[1].fields[1].order).to.equal(API.Order.DESCENDING);

// The two processed indexes should be different (fixing the duplicate issue)
expect(JSON.stringify(result[0].fields)).to.not.equal(JSON.stringify(result[1].fields));
});

it("should handle indexes with no __name__ fields", () => {
const mockIndexes: API.Index[] = [
{
name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123",
queryScope: API.QueryScope.COLLECTION,
fields: [
{ fieldPath: "foo", order: API.Order.ASCENDING },
{ fieldPath: "bar", arrayConfig: API.ArrayConfig.CONTAINS },
],
state: API.State.READY,
},
];

const result = FirestoreApi.processIndexes(mockIndexes);

expect(result[0].fields).to.have.length(2);
expect(result[0].fields[0].fieldPath).to.equal("foo");
expect(result[0].fields[1].fieldPath).to.equal("bar");
});
});
Comment on lines +450 to +588
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The new tests cover composite indexes well, but they miss an important edge case: an index that only contains the __name__ field. Add test cases to verify the behavior for single-field __name__ indexes.

  it("should filter out a single __name__ ASCENDING field", () => {
    const mockIndexes: API.Index[] = [
      {
        name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123",
        queryScope: API.QueryScope.COLLECTION,
        fields: [
          { fieldPath: "__name__", order: API.Order.ASCENDING },
        ],
        state: API.State.READY,
      },
    ];

    const result = FirestoreApi.processIndexes(mockIndexes);

    expect(result[0].fields).to.have.length(0);
  });

  it("should keep a single __name__ DESCENDING field", () => {
    const mockIndexes: API.Index[] = [
      {
        name: "/projects/myproject/databases/(default)/collectionGroups/collection/indexes/abc123",
        queryScope: API.QueryScope.COLLECTION,
        fields: [
          { fieldPath: "__name__", order: API.Order.DESCENDING },
        ],
        state: API.State.READY,
      },
    ];

    const result = FirestoreApi.processIndexes(mockIndexes);

    expect(result[0].fields).to.have.length(1);
    expect(result[0].fields[0].fieldPath).to.equal("__name__");
    expect(result[0].fields[0].order).to.equal(API.Order.DESCENDING);
  });


describe("IndexSorting", () => {
it("should be able to handle empty arrays", () => {
expect(([] as Spec.Index[]).sort(sort.compareSpecIndex)).to.eql([]);
Expand Down
Loading