Skip to content

Commit b4abd04

Browse files
authored
test: move mongodb to different port to avoid port conflicts (#14993)
This moves the exposed mongodb docker image port from 27017 to 27018 in order to avoid conflict with locally installed mongodb applications running on port 27017. Previously, you would have to make sure you don't have mongodb running locally when using the docker image - otherwise, it would fail to connect. It also adds the old .env variables commented out in the .env.example for users who do not wish to use docker. The CONTRIBUTING.md now includes docker installation steps for macOS users.
1 parent 4c91d04 commit b4abd04

8 files changed

Lines changed: 33 additions & 14 deletions

File tree

.env.example

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,16 @@ PAYLOAD_DATABASE=mongodb
33

44
# Optional - used for the `translateNewKeys` script
55
OPENAI_KEY=
6+
7+
8+
9+
10+
11+
12+
13+
14+
15+
# If you're NOT using our docker scripts and want to manually install your database, uncomment these and point them to your local database.
16+
# Do not uncomment these if you're using our Docker scripts to run your database.
17+
# MONGODB_URL=mongodb://127.0.0.1/payloadtests
18+
# POSTGRES_URL=postgres://127.0.0.1:5432/payloadtests

.github/actions/start-database/action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@ runs:
2626
shell: bash
2727
run: |
2828
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
29+
echo "url=mongodb://payload:payload@localhost:27018/payload?authSource=admin&directConnection=true&replicaSet=rs0" >> $GITHUB_OUTPUT
3030
3131
- name: Start MongoDB Atlas Local
3232
id: mongodb-atlas
3333
if: inputs.database == 'mongodb-atlas'
3434
shell: bash
3535
run: |
3636
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
37+
echo "url=mongodb://localhost:27019/payload?directConnection=true&replicaSet=mongodb-atlas-local" >> $GITHUB_OUTPUT
3838
3939
- name: Start PostgreSQL
4040
id: postgres

CONTRIBUTING.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ Set `PAYLOAD_DATABASE` in your `.env` file to choose the database adapter:
8787
- `supabase` - Supabase (PostgreSQL)
8888
- `d1` - D1 (SQLite)
8989

90-
Then use Docker to start your database:
90+
Then use Docker to start your database.
91+
92+
On MacOS, the easiest way to install Docker is to use brew. Simply run `pnpm install --cask docker`, open the docker desktop app, apply the recommended settings and you're good to go.
9193

9294
### PostgreSQL
9395

@@ -107,7 +109,7 @@ pnpm docker:mongodb:restart:clean # Start fresh (removes data)
107109
pnpm docker:mongodb:stop # Stop
108110
```
109111

110-
URL: `mongodb://payload:payload@localhost:27017/payload?authSource=admin&directConnection=true&replicaSet=rs0`
112+
URL: `mongodb://payload:payload@localhost:27018/payload?authSource=admin&directConnection=true&replicaSet=rs0`
111113

112114
### MongoDB Atlas Local
113115

@@ -117,17 +119,20 @@ pnpm docker:mongodb-atlas:restart:clean # Start fresh (removes data)
117119
pnpm docker:mongodb-atlas:stop # Stop
118120
```
119121

120-
URL: `mongodb://localhost:27018/payload?directConnection=true&replicaSet=mongodb-atlas-local` (no auth required)
122+
URL: `mongodb://localhost:27019/payload?directConnection=true&replicaSet=mongodb-atlas-local` (no auth required)
121123

122124
### SQLite
123125

124126
SQLite databases don't require Docker - they're stored as files in the project.
125127

126128
### Testing with your own database
127129

128-
If you wish to use your own MongoDB database for the `test` directory instead of using the docker database, all you need to do is add the following env variable to your `.env` file:
130+
If you wish to use your own MongoDB database for the `test` directory instead of using the docker database, add the following to your `.env` file:
129131

130-
- `DATABASE_URL` to your database URL e.g. `mongodb://127.0.0.1/your-test-db`.
132+
```env
133+
MONGODB_URL=mongodb://127.0.0.1/payloadtests # Point this to your locally installed MongoDB database
134+
POSTGRES_URL=postgres://127.0.0.1:5432/payloadtests # Point this to your locally installed PostgreSQL database
135+
```
131136

132137
### Running the e2e and int tests
133138

test/generateDatabaseAdapter.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import { fileURLToPath } from 'node:url'
55
const filename = fileURLToPath(import.meta.url)
66
const dirname = path.dirname(filename)
77

8+
// Runs on port 27018 to avoid conflicts with locally installed MongoDB
89
const mongooseAdapterArgs = `
910
ensureIndexes: true,
1011
url:
1112
process.env.MONGODB_URL || process.env.DATABASE_URL ||
12-
'mongodb://payload:payload@localhost:27017/payload?authSource=admin&directConnection=true&replicaSet=rs0',
13+
'mongodb://payload:payload@localhost:27018/payload?authSource=admin&directConnection=true&replicaSet=rs0',
1314
`
1415

1516
export const allDatabaseAdapters = {
@@ -21,15 +22,15 @@ export const allDatabaseAdapters = {
2122
})`,
2223
// mongodb-atlas uses Docker-based MongoDB Atlas Local (all-in-one with search)
2324
// Start with: pnpm docker:mongodb-atlas:start
24-
// Runs on port 27018 to avoid conflicts with mongodb
25+
// Runs on port 27019 to avoid conflicts with mongodb
2526
'mongodb-atlas': `
2627
import { mongooseAdapter } from '@payloadcms/db-mongodb'
2728
2829
export const databaseAdapter = mongooseAdapter({
2930
ensureIndexes: true,
3031
url:
3132
process.env.MONGODB_ATLAS_URL || process.env.DATABASE_URL ||
32-
'mongodb://localhost:27018/payload?directConnection=true&replicaSet=mongodb-atlas-local',
33+
'mongodb://localhost:27019/payload?directConnection=true&replicaSet=mongodb-atlas-local',
3334
})`,
3435
cosmosdb: `
3536
import { mongooseAdapter, compatibilityOptions } from '@payloadcms/db-mongodb'

test/helpers/db/mongodb-atlas/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ services:
1616
container_name: mongodb-atlas-payload-test
1717
hostname: mongodb-atlas-local # Sets a fixed replica set name (used in connection string replicaSet param)
1818
ports:
19-
- '27018:27017'
19+
- '27019:27017'
2020
volumes:
2121
- mongodb_atlas_data:/data/db
2222
environment:

test/helpers/db/mongodb-atlas/run-test-connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ import { testConnection } from '../mongodb/test-connection.js'
99

1010
await testConnection(
1111
process.env.MONGODB_ATLAS_URL ||
12-
'mongodb://localhost:27018/payload?directConnection=true&replicaSet=mongodb-atlas-local',
12+
'mongodb://localhost:27019/payload?directConnection=true&replicaSet=mongodb-atlas-local',
1313
)

test/helpers/db/mongodb/docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ services:
1717
image: mongodb/mongodb-community-server:8.2-ubi9
1818
container_name: mongodb-payload-test
1919
ports:
20-
- '27017:27017'
20+
- '27018:27017'
2121
volumes:
2222
- mongodb_data:/data/db
2323
- ./keyfile:/etc/mongodb/keyfile:ro

test/helpers/db/mongodb/run-test-connection.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ import { testConnection } from './test-connection.js'
99

1010
await testConnection(
1111
process.env.MONGODB_URL ||
12-
'mongodb://payload:payload@localhost:27017/payload?authSource=admin&directConnection=true&replicaSet=rs0',
12+
'mongodb://payload:payload@localhost:27018/payload?authSource=admin&directConnection=true&replicaSet=rs0',
1313
)

0 commit comments

Comments
 (0)