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

Correct handling of __name__ in firestore indexes #8862

merged 6 commits into from
Jul 17, 2025

Conversation

joehan
Copy link
Contributor

@joehan joehan commented Jul 17, 2025

Description

Fix index filtering to match the documented behavior. Builds off of #8860 - thank you for the help!
Fixes Fixes #7629, #8859

Scenarios Tested

Tested using the scenario @aalej set up in #8860 (comment) - it now works on repeated deployments!

TorbenWetter and others added 5 commits July 17, 2025 14:40
Preserve __name__ fields with DESCENDING order while filtering out
implicit ASCENDING ones to fix deployment conflicts.

- Fixes duplicate index issues (#7629)
- Fixes deployment conflicts (#8859)
- Add comprehensive test coverage

Fixes #7629, #8859
Refactored __name__ field filtering from listIndexes into static method
FirestoreApi.processIndexes() to ensure tests verify production code
rather than duplicating the implementation.
Simplified the filter expression in processIndexes() to a single boolean
expression as suggested in PR feedback for better readability and
conciseness.
@joehan joehan changed the title Jh ind Correct hadnling of __name__ in firestore indexes Jul 17, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Summary of Changes

Hello @joehan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses and fixes an issue in Firestore index filtering where __name__ fields were incorrectly handled, leading to problems like duplicate index entries and deployment conflicts. The changes ensure that index listings accurately reflect the documented behavior, specifically by preserving __name__ fields with explicit DESCENDING order while filtering out implicit ASCENDING ones.

Highlights

  • Firestore Index Filtering Logic: Introduced a new static method FirestoreApi.processIndexes in src/firestore/api.ts to correctly filter __name__ fields in Firestore index listings. This method removes implicit __name__ fields (those with ASCENDING order matching the preceding field's order) while preserving explicit __name__ fields (e.g., those with DESCENDING order).
  • Index Listing Integration: The listIndexes method in src/firestore/api.ts now utilizes the FirestoreApi.processIndexes utility to ensure all returned index specifications adhere to the corrected filtering rules, aligning with documented behavior.
  • Comprehensive Testing: Added a new test suite IndexListingWithNameFields in src/firestore/indexes.spec.ts with multiple test cases to validate the processIndexes logic, covering various scenarios including different __name__ field orders and ensuring proper distinction between indexes.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@joehan joehan changed the title Correct hadnling of __name__ in firestore indexes Correct handling of __name__ in firestore indexes Jul 17, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request corrects the handling of implicit __name__ fields in Firestore indexes by filtering them out. The implementation has a critical bug that can cause a crash when processing an index with an empty fields array, and it also incorrectly handles indexes that only contain a __name__ field. Additionally, the new tests are missing coverage for this single-field __name__ index scenario.

Comment on lines +450 to +588
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");
});
});
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);
  });

@joehan joehan requested a review from aalej July 17, 2025 18:30
Copy link
Contributor

@aalej aalej left a comment

Choose a reason for hiding this comment

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

LGTM! Tested multiple deployments for each scenario

  • Deploy does not ask deletions when prod and local indexes match (OK)
  • Deploy correctly creates new indexes if they don't exist in prod (OK)
  • Deploying with __name__ explecitly set (OK)

Edit: Tested deploying to a new project(OK)

@joehan joehan enabled auto-merge (squash) July 17, 2025 20:35
@joehan joehan disabled auto-merge July 17, 2025 20:35
@joehan joehan merged commit 67ebf87 into master Jul 17, 2025
47 of 50 checks passed
@joehan joehan deleted the jh-ind branch July 17, 2025 20:35
@github-project-automation github-project-automation bot moved this from Approved [PR] to Done in [Cloud] Extensions + Functions Jul 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrect firebase firestore:indexes output generated
3 participants