firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

Incorrect warning about missing index

Open ottob opened this issue 1 year ago • 3 comments
trafficstars

[REQUIRED] Environment info

firebase-tools: 13.4.0

Platform: macOS

[REQUIRED] Test case

Haven't been able to create one. We have over 100 indexes. They all work fine except this one where we get the warning. We have a similar index that is almost the same except that the final field is reversed. Could this cause an issue?

...
 {
          "fieldPath": "updated",
          "order": "ASCENDING"
        }

[REQUIRED] Steps to reproduce

firebase deploy --only firestore:indexes

Index defined in indexes.json

{
      "collectionGroup": "cases",
      "queryScope": "COLLECTION",
      "fields": [
        {
          "fieldPath": "userRoles",
          "arrayConfig": "CONTAINS"
        },
        {
          "fieldPath": "userOrganizationId",
          "order": "ASCENDING"
        },
        {
          "fieldPath": "caregiverId",
          "order": "ASCENDING"
        },
        {
          "fieldPath": "snoozed",
          "order": "ASCENDING"
        },
        {
          "fieldPath": "status",
          "order": "ASCENDING"
        },
        {
          "fieldPath": "visibility",
          "order": "ASCENDING"
        },
        {
          "fieldPath": "unsigned",
          "order": "DESCENDING"
        },
        {
          "fieldPath": "updated",
          "order": "DESCENDING"
        }
      ]
    },

[REQUIRED] Expected behavior

Deploy without warning

[REQUIRED] Actual behavior

i firestore: The following indexes are defined in your project but are not present in your firestore indexes file: (cases) -- (userRoles,CONTAINS) (caregiverId,ASCENDING) (snoozed,ASCENDING) (status,ASCENDING) (userOrganizationId,ASCENDING) (visibility,ASCENDING) (unsigned,DESCENDING) (updated,DESCENDING)

ottob avatar Mar 07 '24 08:03 ottob

Hello @ottob. Do you have indexes defined from the Firebase console that haven't been added to the index file here?

exaby73 avatar Mar 08 '24 04:03 exaby73

Do you have indexes defined from the Firebase console that haven't been added to the index file here?

No, then I think we would get a warning about it.

ottob avatar Mar 08 '24 15:03 ottob

It's possible this is related to how the Document ID is included in the index. Can you check the indexes that are created in the console and pay attention to the direction of the __name__ field (ASCENDING or DESCENDING) .

The Firebase CLI has an issue where it currently compares indexes with the __name__ stripped off: https://github.com/firebase/firebase-tools/blob/master/src/firestore/api.ts#L187

However the Firestore SDKs perform order by normalization where if the __name__ is left off of a query it is automatically given the direction of the last order by in the query (Which may be automatically added from an inequality) https://github.com/googleapis/nodejs-firestore/blob/5da7138abc15e99edc51fdfd25a70bf5eb0c97b9/dev/src/reference.ts#L1985

Basically your index might be getting created from the CLI as ... updated DESC, __name__ ASC, but the query from the SDK is attempting to be used as ... updated DESC, __name__ DESC

If this is indeed the issue, then you may be able to work around it by explicitly stating the order by.

i.e.

...
    .order_by("updated", "DESCENDING")
    .order_by("__name__", "ASCENDING")

NickChittle avatar Mar 12 '24 18:03 NickChittle