Skip to content

Commit 0ef8237

Browse files
authored
chore: add postgres docker-compose file for local dev (#207)
* chore: add postgres docker-compose file for local dev
1 parent 16ccbd4 commit 0ef8237

10 files changed

Lines changed: 114 additions & 5 deletions

File tree

.github/workflows/maven.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
# refer: https://hub.docker.com/_/postgres
2121
env:
2222
POSTGRES_USER: postgres
23-
POSTGRES_PASSWORD: mysecretpassword
23+
POSTGRES_PASSWORD: postgres
2424
POSTGRES_DB: postgres
2525
ports:
2626
- 5432:5432

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ java-cosmos is a client for Azure CosmosDB 's SQL API (also called documentdb fo
2323
<dependency>
2424
<groupId>com.github.thunderz99</groupId>
2525
<artifactId>java-cosmos</artifactId>
26-
<version>0.8.22</version>
26+
<version>0.8.23</version>
2727
</dependency>
2828
```
2929

mongo/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# MongoDB local dev
2+
3+
This folder contains a Docker Compose setup for a single-node MongoDB replica set used by local unit tests.
4+
5+
## Start
6+
7+
From the repository root:
8+
9+
```bash
10+
cd mongo
11+
docker-compose up -d
12+
```
13+
14+
The setup starts:
15+
- `mongo-replica` on `localhost:27017`
16+
- `mongo-init-replica` to initialize replica set `rs0`
17+
18+
## Verify
19+
20+
```bash
21+
docker-compose ps
22+
docker logs mongo-init-replica
23+
```
24+
25+
When initialization succeeds, logs include `Replica set initialized.`
26+
27+
## Local unit test
28+
29+
Then you can run the follow cmd or use an IDE to run unit tests.
30+
31+
```bash
32+
mvn -Dtest='io.github.thunderz99.cosmos.impl.mongo.**.*Test' test
33+
```
34+
35+
## Stop
36+
37+
```bash
38+
docker-compose down
39+
```

mongo/docker-compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
version: '3.8'
21
services:
32
mongo-replica:
43
image: mongo:5.0

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<groupId>com.github.thunderz99</groupId>
55
<artifactId>java-cosmos</artifactId>
66
<packaging>jar</packaging>
7-
<version>0.8.22</version>
7+
<version>0.8.23</version>
88
<name>${project.groupId}:${project.artifactId}$</name>
99
<description>A lightweight Azure CosmosDB client for Java</description>
1010
<url>https://github.com/thunderz99/java-cosmos</url>

postgres/Dockerfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
FROM postgres:16
2+
3+
# Install dependencies and add the PostgreSQL APT repository
4+
RUN set -eux; \
5+
apt-get update; \
6+
apt-get install -y --no-install-recommends ca-certificates gnupg lsb-release wget; \
7+
mkdir -p /etc/apt/keyrings; \
8+
wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | gpg --dearmor -o /etc/apt/keyrings/pgdg.gpg; \
9+
echo "deb [signed-by=/etc/apt/keyrings/pgdg.gpg] http://apt.postgresql.org/pub/repos/apt $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list; \
10+
apt-get update; \
11+
apt-get install -y --no-install-recommends postgresql-16-cron; \
12+
rm -rf /var/lib/apt/lists/*
13+
14+
# Copy an initialization script to enable the extension
15+
COPY init-pgcron.sql /docker-entrypoint-initdb.d/

postgres/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# PostgreSQL local dev
2+
3+
This folder contains a Docker Compose setup for local PostgreSQL used by unit tests.
4+
5+
The image is built from `postgres/Dockerfile`, which installs `postgresql-16-cron` and runs `init-pgcron.sql` to enable `pg_cron`.
6+
7+
## Start
8+
9+
From the repository root:
10+
11+
```bash
12+
cd postgres
13+
docker-compose up -d --build
14+
```
15+
16+
Default connection details:
17+
- Host: `localhost`
18+
- Port: `5432`
19+
- Database: `postgres`
20+
- User: `postgres`
21+
- Password: `postgres`
22+
23+
## Verify
24+
25+
```bash
26+
docker-compose ps
27+
docker exec -it postgres16 psql -U postgres -d postgres -c "SELECT extname FROM pg_extension WHERE extname='pg_cron';"
28+
```
29+
30+
## Local unit test
31+
32+
Then you can run the follow cmd or use an IDE to run unit tests.
33+
34+
```bash
35+
mvn -Dtest='io.github.thunderz99.cosmos.impl.postgres.**.*Test' test
36+
```
37+
38+
## Stop
39+
40+
```bash
41+
docker-compose down
42+
```

postgres/docker-compose.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
services:
2+
postgres:
3+
build:
4+
context: .
5+
dockerfile: Dockerfile
6+
container_name: postgres16
7+
environment:
8+
POSTGRES_USER: postgres
9+
POSTGRES_PASSWORD: postgres
10+
POSTGRES_DB: postgres
11+
ports:
12+
- "5432:5432"
13+
command: postgres -c shared_preload_libraries=pg_cron

postgres/init-pgcron.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
CREATE EXTENSION IF NOT EXISTS pg_cron;

src/test/java/io/github/thunderz99/cosmos/impl/postgres/PostgresImplTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class PostgresImplTest {
1313
/**
1414
* local postgres connection string if test against a local setup.
1515
*/
16-
public static final String LOCAL_CONNECTION_STRING = "jdbc:postgresql://localhost:5432/postgres?user=postgres&password=mysecretpassword&sslmode=disable";
16+
public static final String LOCAL_CONNECTION_STRING = "jdbc:postgresql://localhost:5432/postgres?user=postgres&password=postgres&sslmode=disable";
1717

1818
String db = "unit_test_db_" + RandomStringUtils.randomAlphanumeric(6);
1919

0 commit comments

Comments
 (0)