Skip to content

Commit e3c512a

Browse files
authored
test: improve database test setup (#14982)
This PR brings over most of the test suite improvements I made [here](payloadcms/enterprise-plugins#249) to our payload monorepo. - Replaces mongodb-memory-server with an actual mongo db using the mongodb-community-server docker image. This unblocks the vitest migration PR (#14337). Currently, debugging does not work in that PR - this is due to the global setup script that has to start the mongo memory db. - Just like postgres, all mongodb databases now support vector search. mongodb-atlas-local supports it natively, and for mongodb-community-server, our docker compose script installs `mongot`, which unlocks support for vector search. This means we could add [vector storage/search tests similar to the ones we have for postgres](https://github.com/payloadcms/payload/blob/main/test/database/postgres-vector.int.spec.ts) - Int tests now run against both mongodb adapters: mongodb (mongodb-community-server) and mongodb-atlas (mongodb-atlas-local) - Adds docker scripts for mongodb, mongodb-atlas, and postgres, documented in README.md. Updates default db adapter URLs to automatically pick up databases started by those scripts. This makes it easier for people cloning the repo to get started with consistent databases matching CI - no complicated manual installation steps - Simplified db setup handling locally in CI. In CI, everything is scoped to `.github/actions/start-database/action.yml`. Locally, everything is scoped to `test/helpers/db`. Each database adapter now shares the same username, password and db name - Use consistent db connection string env variables, all ending with _URL - Updates the CONTRIBUTING.md with up-to-date information and adds a new database section. We now recommend everyone to use those docker scripts
1 parent 042d7eb commit e3c512a

135 files changed

Lines changed: 915 additions & 443 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Database adapters: mongodb, mongodb-atlas, cosmosdb, documentdb, firestore, postgres, postgres-custom-schema, postgres-uuid, postgres-read-replica, vercel-postgres-read-replica, sqlite, sqlite-uuid, supabase, d1
2+
PAYLOAD_DATABASE=mongodb
3+
4+
# Optional - used for the `translateNewKeys` script
5+
OPENAI_KEY=

.github/actions/setup/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ inputs:
99
description: Pnpm version override
1010
pnpm-run-install:
1111
description: Whether to run pnpm install
12-
default: true
12+
default: 'true'
1313
pnpm-restore-cache:
1414
description: Whether to restore cache
15-
default: true
15+
default: 'true'
1616
pnpm-install-cache-key:
1717
description: The cache key override for the pnpm install cache
1818

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
name: Start Database
2+
description: Starts the required database for tests
3+
4+
inputs:
5+
database:
6+
description: 'Database type: mongodb, mongodb-atlas, cosmosdb, documentdb, firestore, postgres, postgres-custom-schema, postgres-uuid, postgres-read-replica, vercel-postgres-read-replica, sqlite, sqlite-uuid, supabase, d1'
7+
required: true
8+
9+
outputs:
10+
POSTGRES_URL:
11+
description: PostgreSQL connection URL
12+
value: ${{ steps.postgres.outputs.url }}
13+
MONGODB_URL:
14+
description: MongoDB connection URL
15+
value: ${{ steps.mongodb.outputs.url }}
16+
MONGODB_ATLAS_URL:
17+
description: MongoDB Atlas connection URL
18+
value: ${{ steps.mongodb-atlas.outputs.url }}
19+
20+
runs:
21+
using: composite
22+
steps:
23+
- name: Start MongoDB
24+
id: mongodb
25+
if: contains(fromJSON('["mongodb", "cosmosdb", "documentdb", "firestore"]'), inputs.database)
26+
shell: bash
27+
run: |
28+
docker compose -f test/helpers/db/mongodb/docker-compose.yml up -d --wait
29+
echo "url=mongodb://payload:payload@localhost:27017/payload?authSource=admin&directConnection=true&replicaSet=rs0" >> $GITHUB_OUTPUT
30+
31+
- name: Start MongoDB Atlas Local
32+
id: mongodb-atlas
33+
if: inputs.database == 'mongodb-atlas'
34+
shell: bash
35+
run: |
36+
docker compose -f test/helpers/db/mongodb-atlas/docker-compose.yml up -d --wait
37+
echo "url=mongodb://localhost:27018/payload?directConnection=true&replicaSet=mongodb-atlas-local" >> $GITHUB_OUTPUT
38+
39+
- name: Start PostgreSQL
40+
id: postgres
41+
if: startsWith(inputs.database, 'postgres')
42+
shell: bash
43+
run: |
44+
docker compose -f test/helpers/db/postgres/docker-compose.yml up -d --wait
45+
echo "url=postgresql://payload:payload@localhost:5433/payload" >> $GITHUB_OUTPUT
46+
47+
- name: Configure PostgreSQL custom schema
48+
if: inputs.database == 'postgres-custom-schema'
49+
shell: bash
50+
run: |
51+
docker exec postgres-payload-test psql -U payload -d payload -c "CREATE SCHEMA custom;"
52+
53+
- name: Start Supabase
54+
id: supabase
55+
if: inputs.database == 'supabase'
56+
uses: supabase/setup-cli@v1
57+
with:
58+
version: latest
59+
60+
- name: Initialize Supabase
61+
if: inputs.database == 'supabase'
62+
shell: bash
63+
run: |
64+
supabase init
65+
supabase start

.github/workflows/main.yml

Lines changed: 42 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,10 @@ name: ci
22

33
on:
44
pull_request:
5-
types:
6-
- opened
7-
- reopened
8-
- synchronize
5+
types: [opened, reopened, synchronize]
6+
97
push:
10-
branches:
11-
- main
8+
branches: [main]
129

1310
concurrency:
1411
# <workflow_name>-<branch_name>-<true || commit_sha if branch is protected>
@@ -53,7 +50,7 @@ jobs:
5350
- 'package.json'
5451
templates:
5552
- 'templates/**'
56-
- name: Log all filter results
53+
- name: Log filter results
5754
run: |
5855
echo "needs_build: ${{ steps.filter.outputs.needs_build }}"
5956
echo "needs_tests: ${{ steps.filter.outputs.needs_tests }}"
@@ -74,7 +71,7 @@ jobs:
7471

7572
build:
7673
needs: changes
77-
if: ${{ needs.changes.outputs.needs_build == 'true' }}
74+
if: needs.changes.outputs.needs_build == 'true'
7875
runs-on: ubuntu-24.04
7976

8077
steps:
@@ -96,7 +93,7 @@ jobs:
9693
tests-unit:
9794
runs-on: ubuntu-24.04
9895
needs: [changes, build]
99-
if: ${{ needs.changes.outputs.needs_tests == 'true' }}
96+
if: needs.changes.outputs.needs_tests == 'true'
10097
steps:
10198
- uses: actions/checkout@v5
10299

@@ -120,7 +117,7 @@ jobs:
120117
tests-types:
121118
runs-on: ubuntu-24.04
122119
needs: [changes, build]
123-
if: ${{ needs.changes.outputs.needs_tests == 'true' }}
120+
if: needs.changes.outputs.needs_tests == 'true'
124121
steps:
125122
- uses: actions/checkout@v5
126123

@@ -144,14 +141,15 @@ jobs:
144141
tests-int:
145142
runs-on: ubuntu-24.04
146143
needs: [changes, build]
147-
if: ${{ needs.changes.outputs.needs_tests == 'true' }}
144+
if: needs.changes.outputs.needs_tests == 'true'
148145
name: int-${{ matrix.database }}
149146
timeout-minutes: 45
150147
strategy:
151148
fail-fast: false
152149
matrix:
153150
database:
154151
- mongodb
152+
- mongodb-atlas
155153
- documentdb
156154
- cosmosdb
157155
- firestore
@@ -163,28 +161,12 @@ jobs:
163161
- sqlite-uuid
164162

165163
env:
166-
POSTGRES_USER: postgres
167-
POSTGRES_PASSWORD: postgres
168-
POSTGRES_DB: payloadtests
169164
AWS_ENDPOINT_URL: http://127.0.0.1:4566
170165
AWS_ACCESS_KEY_ID: localstack
171166
AWS_SECRET_ACCESS_KEY: localstack
172167
AWS_REGION: us-east-1
173168

174169
services:
175-
postgres:
176-
# Custom postgres 17 docker image that supports both pg-vector and postgis: https://github.com/payloadcms/postgis-vector
177-
image: ${{ (startsWith(matrix.database, 'postgres') ) && 'ghcr.io/payloadcms/postgis-vector:latest' || '' }}
178-
env:
179-
# must specify password for PG Docker container image, see: https://registry.hub.docker.com/_/postgres?tab=description&page=1&name=10
180-
POSTGRES_USER: ${{ env.POSTGRES_USER }}
181-
POSTGRES_PASSWORD: ${{ env.POSTGRES_PASSWORD }}
182-
POSTGRES_DB: ${{ env.POSTGRES_DB }}
183-
ports:
184-
- 5432:5432
185-
# needed because the postgres container does not provide a healthcheck
186-
options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
187-
188170
redis:
189171
image: redis:latest
190172
ports:
@@ -210,50 +192,29 @@ jobs:
210192
- name: Start LocalStack
211193
run: pnpm docker:start
212194

213-
- name: Install Supabase CLI
214-
uses: supabase/setup-cli@v1
215-
with:
216-
version: latest
217-
if: matrix.database == 'supabase'
218-
219-
- name: Initialize Supabase
220-
run: |
221-
supabase init
222-
supabase start
223-
if: matrix.database == 'supabase'
224-
225-
- name: Configure PostgreSQL
226-
run: |
227-
psql "postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" -c "CREATE ROLE runner SUPERUSER LOGIN;"
228-
psql "postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" -c "SELECT version();"
229-
echo "POSTGRES_URL=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" >> $GITHUB_ENV
230-
if: startsWith(matrix.database, 'postgres')
231-
232-
- name: Configure PostgreSQL with custom schema
233-
run: |
234-
psql "postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" -c "CREATE SCHEMA custom;"
235-
if: matrix.database == 'postgres-custom-schema'
236-
237-
- name: Configure Supabase
238-
run: |
239-
echo "POSTGRES_URL=postgresql://postgres:postgres@127.0.0.1:54322/postgres" >> $GITHUB_ENV
240-
if: matrix.database == 'supabase'
241-
242195
- name: Configure Redis
243196
run: |
244-
echo "REDIS_URL=redis://127.0.0.1:6379" >> $GITHUB_ENV
197+
echo "REDIS_URL=redis://127.0.0.1:6379" >> $GITHUB_ENV
198+
199+
- name: Start database
200+
id: db
201+
uses: ./.github/actions/start-database
202+
with:
203+
database: ${{ matrix.database }}
245204

246205
- name: Integration Tests
247206
run: pnpm test:int
248207
env:
249208
NODE_OPTIONS: --max-old-space-size=8096
250209
PAYLOAD_DATABASE: ${{ matrix.database }}
251-
POSTGRES_URL: ${{ env.POSTGRES_URL }}
210+
POSTGRES_URL: ${{ steps.db.outputs.POSTGRES_URL }}
211+
MONGODB_URL: ${{ steps.db.outputs.MONGODB_URL }}
212+
MONGODB_ATLAS_URL: ${{ steps.db.outputs.MONGODB_ATLAS_URL }}
252213

253214
tests-e2e:
254215
runs-on: ubuntu-24.04
255216
needs: [changes, build]
256-
if: ${{ needs.changes.outputs.needs_tests == 'true' }}
217+
if: needs.changes.outputs.needs_tests == 'true'
257218
name: e2e-${{ matrix.suite }}
258219
timeout-minutes: 45
259220
strategy:
@@ -349,7 +310,13 @@ jobs:
349310

350311
- name: Start LocalStack
351312
run: pnpm docker:start
352-
if: ${{ matrix.suite == 'plugin-cloud-storage' }}
313+
if: matrix.suite == 'plugin-cloud-storage'
314+
315+
- name: Start database
316+
id: db
317+
uses: ./.github/actions/start-database
318+
with:
319+
database: mongodb
353320

354321
- name: Store Playwright's Version
355322
run: |
@@ -493,7 +460,7 @@ jobs:
493460

494461
- name: Start LocalStack
495462
run: pnpm docker:start
496-
if: ${{ matrix.suite == 'plugin-cloud-storage' }}
463+
if: matrix.suite == 'plugin-cloud-storage'
497464

498465
- name: Store Playwright's Version
499466
run: |
@@ -542,7 +509,7 @@ jobs:
542509
build-and-test-templates:
543510
runs-on: ubuntu-24.04
544511
needs: [changes, build]
545-
if: ${{ needs.changes.outputs.needs_build == 'true' }}
512+
if: needs.changes.outputs.needs_build == 'true'
546513
name: build-template-${{ matrix.template }}-${{ matrix.database }}
547514
strategy:
548515
fail-fast: false
@@ -613,7 +580,8 @@ jobs:
613580

614581
# Avoid dockerhub rate-limiting
615582
- name: Cache Docker images
616-
uses: ScribeMD/docker-cache@0.5.0
583+
# Use AndreKurait/docker-cache@0.6.0 instead of ScribeMD/docker-cache@0.5.0 to fix the following issue: https://github.com/ScribeMD/docker-cache/issues/837 ("Warning: Failed to restore: Cache service responded with 400")
584+
uses: AndreKurait/docker-cache@0.6.0
617585
with:
618586
key: docker-${{ runner.os }}-mongo-${{ env.MONGODB_VERSION }}
619587

@@ -672,7 +640,7 @@ jobs:
672640
tests-type-generation:
673641
runs-on: ubuntu-24.04
674642
needs: [changes, build]
675-
if: ${{ needs.changes.outputs.needs_tests == 'true' }}
643+
if: needs.changes.outputs.needs_tests == 'true'
676644
steps:
677645
- uses: actions/checkout@v5
678646

@@ -699,14 +667,16 @@ jobs:
699667
if: always()
700668
runs-on: ubuntu-24.04
701669
needs:
702-
- lint
703-
- build
704-
- build-and-test-templates
705-
- tests-unit
706-
- tests-int
707-
- tests-e2e
708-
- tests-types
709-
- tests-type-generation
670+
[
671+
lint,
672+
build,
673+
build-and-test-templates,
674+
tests-unit,
675+
tests-int,
676+
tests-e2e,
677+
tests-types,
678+
tests-type-generation,
679+
]
710680

711681
steps:
712682
- if: ${{ always() && (contains(needs.*.result, 'failure') || contains(needs.*.result, 'cancelled')) }}

.github/workflows/post-release-templates.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ jobs:
6262
- name: Start PostgreSQL
6363
uses: CasperWA/postgresql-action@v1.2
6464
with:
65-
postgresql version: '14' # See https://hub.docker.com/_/postgres for available versions
65+
postgresql version: "14" # See https://hub.docker.com/_/postgres for available versions
6666
postgresql db: ${{ env.POSTGRES_DB }}
6767
postgresql user: ${{ env.POSTGRES_USER }}
6868
postgresql password: ${{ env.POSTGRES_PASSWORD }}
@@ -75,7 +75,7 @@ jobs:
7575
psql "postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" -c "CREATE ROLE runner SUPERUSER LOGIN;"
7676
psql "postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" -c "SELECT version();"
7777
echo "POSTGRES_URL=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" >> $GITHUB_ENV
78-
echo "DATABASE_URI=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" >> $GITHUB_ENV
78+
echo "DATABASE_URL=postgresql://$POSTGRES_USER:$POSTGRES_PASSWORD@localhost:5432/$POSTGRES_DB" >> $GITHUB_ENV
7979
8080
- name: Start MongoDB
8181
uses: supercharge/mongodb-github-action@1.12.0
@@ -108,13 +108,13 @@ jobs:
108108
uses: peter-evans/create-pull-request@v7
109109
with:
110110
token: ${{ secrets.GH_TOKEN_POST_RELEASE_TEMPLATES }}
111-
labels: 'area: templates'
111+
labels: "area: templates"
112112
author: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
113-
commit-message: 'templates: bump templates for ${{ needs.wait_for_release.outputs.release_tag }}'
113+
commit-message: "templates: bump templates for ${{ needs.wait_for_release.outputs.release_tag }}"
114114
branch: ${{ steps.commit.outputs.branch }}
115115
base: main
116116
assignees: ${{ github.actor }}
117-
title: 'templates: bump for ${{ needs.wait_for_release.outputs.release_tag }}'
117+
title: "templates: bump for ${{ needs.wait_for_release.outputs.release_tag }}"
118118
body: |
119119
🤖 Automated bump of templates for ${{ needs.wait_for_release.outputs.release_tag }}
120120

CLAUDE.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@ Payload is a monorepo structured around Next.js, containing the core CMS platfor
7272
- `pnpm run dev` - Start dev server with default config (`test/_community/config.ts`)
7373
- `pnpm run dev <directory_name>` - Start dev server with specific test config (e.g. `pnpm run dev fields` loads `test/fields/config.ts`)
7474
- `pnpm run dev:postgres` - Run dev server with Postgres
75-
- `pnpm run dev:memorydb` - Run dev server with in-memory MongoDB
7675

7776
### Development Environment
7877

0 commit comments

Comments
 (0)