Skip to content

Commit 1c45fd5

Browse files
authored
Add composite index testing guidance to ReadMe (#5421)
1 parent 4701536 commit 1c45fd5

File tree

3 files changed

+56
-23
lines changed

3 files changed

+56
-23
lines changed

.github/workflows/ci_tests.yml

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,22 +121,6 @@ jobs:
121121
with:
122122
credentials_json: ${{ secrets.GCP_SERVICE_ACCOUNT }}
123123
- uses: google-github-actions/setup-gcloud@v0
124-
# create composite indexes with Terraform
125-
- name: Setup Terraform
126-
if: contains(matrix.module, ':firebase-firestore')
127-
uses: hashicorp/setup-terraform@v2
128-
- name: Terraform Init
129-
if: contains(matrix.module, ':firebase-firestore')
130-
run: |
131-
cd firebase-firestore
132-
terraform init
133-
continue-on-error: true
134-
- name: Terraform Apply
135-
if: github.event_name == 'pull_request' && contains(matrix.module, ':firebase-firestore')
136-
run: |
137-
cd firebase-firestore
138-
terraform apply -var-file=../google-services.json -auto-approve
139-
continue-on-error: true
140124
- name: ${{ matrix.module }} Integ Tests
141125
env:
142126
FIREBASE_CI: 1
@@ -178,6 +162,34 @@ jobs:
178162
with:
179163
credentials_json: ${{ secrets.GCP_SERVICE_ACCOUNT }}
180164
- uses: google-github-actions/setup-gcloud@v0
165+
166+
# create composite indexes with Terraform
167+
- name: Setup Terraform
168+
uses: hashicorp/setup-terraform@v2
169+
- name: Terraform Init
170+
run: |
171+
cd firebase-firestore
172+
terraform init
173+
continue-on-error: true
174+
- name: Terraform Apply
175+
if: github.event_name == 'pull_request'
176+
run: |
177+
cd firebase-firestore
178+
179+
# Define a temporary file, redirect both stdout and stderr to the file
180+
output_file=$(mktemp)
181+
if ! terraform apply -var-file=../google-services.json -auto-approve > "$output_file" 2>&1 ; then
182+
cat "$output_file"
183+
if cat "$output_file" | grep -q "index already exists"; then
184+
echo "==================================================================================="
185+
echo -e "\e[93m\e[1mTerraform apply failed due to index already exists; We can safely ignore this error.\e[0m"
186+
echo "==================================================================================="
187+
fi
188+
exit 1
189+
fi
190+
rm -f "$output_file"
191+
continue-on-error: true
192+
181193
- name: Firestore Named DB Integ Tests
182194
env:
183195
FIREBASE_CI: 1

firebase-firestore/README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,28 @@ Run:
8787
./gradlew :firebase-firestore:deviceCheck
8888
```
8989

90+
### Testing composite index query against production
91+
#### Setting Up the Environment:
92+
1. Create a `google-services.json` file in the root directory. This file should contain your target Firebase project's configuration.
93+
2. If not already logged in, authenticate with your Google Cloud Platform (GCP) account using
94+
`gcloud auth application-default login`. You can check your logged-in accounts by running
95+
`gcloud auth list`.
96+
3. Navigate to the `firebase-firestore` directory, create composite indexes by running:
97+
```
98+
terraform init
99+
terraform apply -var-file=../google-services.json -auto-approve
100+
```
101+
Note: If the index creation encounters issues, such as concurrent operations, consider running the
102+
index creation process again. Error messages indicating that indexes have already been created can
103+
be safely disregarded.
104+
105+
#### Adding new composite index query tests
106+
1. To create a new composite index for local development, click on the provided link in the test
107+
error message, which will direct you to the Firebase Console.
108+
2. Add the newly created composite index to the `firestore_index_config.tf` file. The "__name__"
109+
field is not required to be explicitly added to the file, as the index creation will auto complete
110+
it on behalf.
111+
90112
## Code Formatting
91113

92114
Run below to format Java code:

firebase-firestore/src/androidTest/java/com/google/firebase/firestore/CompositeIndexQueryTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,13 @@
3030
* Guidance for Creating Tests:
3131
* ----------------------------
3232
* When creating tests that require composite indexes, it is recommended to utilize the
33-
* "CompositeIndexTestHelper" class. This utility class provides methods for creating
34-
* and setting test documents and running queries with ease, ensuring proper data
35-
* isolation and query construction.
33+
* "CompositeIndexTestHelper" class. This utility class provides methods for creating and setting
34+
* test documents and running queries with ease, ensuring proper data isolation and query
35+
* construction.
3636
*
37-
* Please remember to update the main index configuration file (firestore_index_config.tf)
38-
* with any new composite indexes needed for the tests. This ensures synchronization with
39-
* other testing environments, including CI. You can generate the required index link by
40-
* clicking on the Firebase console link in the error message while running tests locally.
37+
* To get started, please refer to the instructions provided in the README file. This will guide
38+
* you through setting up your local testing environment and updating the Terraform configuration
39+
* with any new composite indexes required for your testing scenarios.
4140
*/
4241
@RunWith(AndroidJUnit4.class)
4342
public class CompositeIndexQueryTest {

0 commit comments

Comments
 (0)