From 6175d8f03423bd302fc96fcd4439c9b232ea3ba6 Mon Sep 17 00:00:00 2001 From: Mohamed Sallam Date: Sat, 9 Aug 2025 20:44:08 +0300 Subject: [PATCH 1/9] Save docker compose --- docker-compose.yml | 36 ++++++++++++++++++++++++++++++++++++ docs/development.md | 7 +++++++ 2 files changed, 43 insertions(+) create mode 100644 docker-compose.yml diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 00000000..3a4869d2 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,36 @@ +name: riverui_dev +services: + postgres: + image: "postgres:16-alpine" + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + - POSTGRES_DB=river_dev + - POSTGRES_HOST_AUTH_METHOD=trust + healthcheck: + test: [ "CMD", "pg_isready" ,"-d", "river_dev", "-U", "user" ] + timeout: 20s + retries: 10 + start_period: 3s + volumes: + - postgres:/var/lib/postgresql/data + ports: + - "5432:5432" + migrate: + image: "golang:1.24-alpine" + depends_on: + postgres: + condition: "service_healthy" + entrypoint: "/bin/sh" + # cache the go binaries so they don't always download + volumes: + - gopath:/go + command: + - -c + - | + echo "downloading river binary" + go install github.com/riverqueue/river/cmd/river@latest + river migrate-up --database-url "postgres://postgres:postgres@postgres/river_dev" +volumes: + postgres: + gopath: \ No newline at end of file diff --git a/docs/development.md b/docs/development.md index a19b8884..4a30603c 100644 --- a/docs/development.md +++ b/docs/development.md @@ -34,6 +34,13 @@ make dev By default the Go backend starts at http://localhost:8080 and the Vite React frontend starts at http://localhost:5173. +## (Optional) Run the database via Docker Compose +This option allows to skip migrating your local postgres database. +It still assumes that port 5432 is free to use. +```sh +docker compose up +``` + ## Migrate database ```sh From f928003f03d819810e97d5cf1b9afa3535dd9a18 Mon Sep 17 00:00:00 2001 From: Mohamed Sallam Date: Sat, 9 Aug 2025 22:53:51 +0300 Subject: [PATCH 2/9] Add postgres via docker compose --- Makefile | 9 +++++++++ docker-compose.yml => docker-compose.dev.yaml | 15 ++++++++------- docs/development.md | 19 ++++++++++++------- .../postgres/init/01-init.sql | 2 ++ 4 files changed, 31 insertions(+), 14 deletions(-) rename docker-compose.yml => docker-compose.dev.yaml (62%) create mode 100644 scripts/docker-compose-dev/postgres/init/01-init.sql diff --git a/Makefile b/Makefile index a2805449..eea6de8a 100644 --- a/Makefile +++ b/Makefile @@ -47,3 +47,12 @@ verify: verify/sqlc .PHONY: verify/sqlc verify/sqlc: cd internal/dbsqlc && sqlc diff + +.PHONY: db/up +db/up: + docker compose -f docker-compose.dev.yaml down + docker compose -f docker-compose.dev.yaml up + +.PHONY: db/down +db/down: + docker compose -f docker-compose.dev.yaml down diff --git a/docker-compose.yml b/docker-compose.dev.yaml similarity index 62% rename from docker-compose.yml rename to docker-compose.dev.yaml index 3a4869d2..a95c7b38 100644 --- a/docker-compose.yml +++ b/docker-compose.dev.yaml @@ -1,19 +1,18 @@ -name: riverui_dev +name: riverui-dev + services: postgres: image: "postgres:16-alpine" environment: - POSTGRES_USER=postgres - - POSTGRES_PASSWORD=postgres - - POSTGRES_DB=river_dev - POSTGRES_HOST_AUTH_METHOD=trust healthcheck: - test: [ "CMD", "pg_isready" ,"-d", "river_dev", "-U", "user" ] + test: [ "CMD", "pg_isready" ,"-d", "river_dev", "-U", "postgres" ] timeout: 20s retries: 10 start_period: 3s volumes: - - postgres:/var/lib/postgresql/data + - ./scripts/docker-compose-dev/postgres/init:/docker-entrypoint-initdb.d ports: - "5432:5432" migrate: @@ -22,7 +21,7 @@ services: postgres: condition: "service_healthy" entrypoint: "/bin/sh" - # cache the go binaries so they don't always download + # cache the go binaries so they don't redownload volumes: - gopath:/go command: @@ -30,7 +29,9 @@ services: - | echo "downloading river binary" go install github.com/riverqueue/river/cmd/river@latest + echo "migrating river_dev database" river migrate-up --database-url "postgres://postgres:postgres@postgres/river_dev" + echo "migrating river_test database" + river migrate-up --database-url "postgres://postgres:postgres@postgres/river_test" volumes: - postgres: gopath: \ No newline at end of file diff --git a/docs/development.md b/docs/development.md index 4a30603c..dea2e121 100644 --- a/docs/development.md +++ b/docs/development.md @@ -34,13 +34,6 @@ make dev By default the Go backend starts at http://localhost:8080 and the Vite React frontend starts at http://localhost:5173. -## (Optional) Run the database via Docker Compose -This option allows to skip migrating your local postgres database. -It still assumes that port 5432 is free to use. -```sh -docker compose up -``` - ## Migrate database ```sh @@ -49,6 +42,18 @@ $ go install github.com/riverqueue/river/cmd/river $ river migrate-up --database-url postgres://localhost/river_dev ``` +## Postgres with docker compose +If you decided to use postgres in docker compose, you can skip database migration steps for testing and development + +The database would be accessible through localhost +```sh +# start/restart +make db/up + +# stop +make db/down +``` + ## Run tests Raise test database: diff --git a/scripts/docker-compose-dev/postgres/init/01-init.sql b/scripts/docker-compose-dev/postgres/init/01-init.sql new file mode 100644 index 00000000..db3863d5 --- /dev/null +++ b/scripts/docker-compose-dev/postgres/init/01-init.sql @@ -0,0 +1,2 @@ +CREATE DATABASE river_dev; +CREATE DATABASE river_test; \ No newline at end of file From 561abb9e674b1a6949b81a935b73e52da574a2d1 Mon Sep 17 00:00:00 2001 From: Mohamed Sallam Date: Sat, 9 Aug 2025 22:54:17 +0300 Subject: [PATCH 3/9] Allow running tests against the docker compose database --- .env.local | 1 + 1 file changed, 1 insertion(+) diff --git a/.env.local b/.env.local index b2ca47d5..b74893db 100644 --- a/.env.local +++ b/.env.local @@ -1,5 +1,6 @@ CORS_ORIGINS=http://localhost:5173,https://example.com DATABASE_URL=postgres://postgres:postgres@localhost:5432/river_dev +TEST_DATABASE_URL=postgres://postgres:postgres@localhost:5432/river_dev OTEL_ENABLED=false PORT=8080 VITE_RIVER_API_BASE_URL=http://localhost:8080/api From 1ea957cb5bc01168d69d5b71233fdacace06fd12 Mon Sep 17 00:00:00 2001 From: Mohamed Sallam Date: Sat, 9 Aug 2025 23:08:32 +0300 Subject: [PATCH 4/9] Adjust postgres config --- docker-compose.dev.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml index a95c7b38..ff5a76c1 100644 --- a/docker-compose.dev.yaml +++ b/docker-compose.dev.yaml @@ -5,7 +5,7 @@ services: image: "postgres:16-alpine" environment: - POSTGRES_USER=postgres - - POSTGRES_HOST_AUTH_METHOD=trust + - POSTGRES_PASSWORD=postgres healthcheck: test: [ "CMD", "pg_isready" ,"-d", "river_dev", "-U", "postgres" ] timeout: 20s From d592f1f61c3f2610a356965e094dbd3588ea48e5 Mon Sep 17 00:00:00 2001 From: Mohamed Sallam Date: Sun, 10 Aug 2025 01:40:28 +0300 Subject: [PATCH 5/9] Change test DB url to test database --- .env.local | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.env.local b/.env.local index b74893db..b4640f0a 100644 --- a/.env.local +++ b/.env.local @@ -1,6 +1,6 @@ CORS_ORIGINS=http://localhost:5173,https://example.com DATABASE_URL=postgres://postgres:postgres@localhost:5432/river_dev -TEST_DATABASE_URL=postgres://postgres:postgres@localhost:5432/river_dev +TEST_DATABASE_URL=postgres://postgres:postgres@localhost:5432/river_test OTEL_ENABLED=false PORT=8080 VITE_RIVER_API_BASE_URL=http://localhost:8080/api From b6be6133ed4dd56a8a792a0e91804070021ba0a3 Mon Sep 17 00:00:00 2001 From: Mohamed Sallam Date: Sun, 10 Aug 2025 01:44:50 +0300 Subject: [PATCH 6/9] Fix docker compose format --- docker-compose.dev.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml index ff5a76c1..ca3b3ccd 100644 --- a/docker-compose.dev.yaml +++ b/docker-compose.dev.yaml @@ -7,7 +7,7 @@ services: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=postgres healthcheck: - test: [ "CMD", "pg_isready" ,"-d", "river_dev", "-U", "postgres" ] + test: ["CMD", "pg_isready", "-d", "river_dev", "-U", "postgres"] timeout: 20s retries: 10 start_period: 3s @@ -34,4 +34,4 @@ services: echo "migrating river_test database" river migrate-up --database-url "postgres://postgres:postgres@postgres/river_test" volumes: - gopath: \ No newline at end of file + gopath: From 0475238704d729391e105d1373fe776bd63ce4c1 Mon Sep 17 00:00:00 2001 From: Mohamed Sallam Date: Sun, 10 Aug 2025 10:48:54 +0300 Subject: [PATCH 7/9] Add docker to the db commands --- Makefile | 8 ++++---- docs/development.md | 4 ++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index eea6de8a..cdcf2950 100644 --- a/Makefile +++ b/Makefile @@ -48,11 +48,11 @@ verify: verify/sqlc verify/sqlc: cd internal/dbsqlc && sqlc diff -.PHONY: db/up -db/up: +.PHONY: docker-db/up +docker-db/up: docker compose -f docker-compose.dev.yaml down docker compose -f docker-compose.dev.yaml up -.PHONY: db/down -db/down: +.PHONY: docker-db/down +docker-db/down: docker compose -f docker-compose.dev.yaml down diff --git a/docs/development.md b/docs/development.md index dea2e121..89b8a5be 100644 --- a/docs/development.md +++ b/docs/development.md @@ -48,10 +48,10 @@ If you decided to use postgres in docker compose, you can skip database migratio The database would be accessible through localhost ```sh # start/restart -make db/up +make docker-db/up # stop -make db/down +make docker-db/down ``` ## Run tests From 2632a547a63367c812923e715cc4564a75644d1b Mon Sep 17 00:00:00 2001 From: Mohamed Sallam Date: Sun, 10 Aug 2025 10:50:45 +0300 Subject: [PATCH 8/9] Address documentation feedback --- docs/development.md | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/docs/development.md b/docs/development.md index 89b8a5be..7c5139e7 100644 --- a/docs/development.md +++ b/docs/development.md @@ -42,10 +42,9 @@ $ go install github.com/riverqueue/river/cmd/river $ river migrate-up --database-url postgres://localhost/river_dev ``` -## Postgres with docker compose -If you decided to use postgres in docker compose, you can skip database migration steps for testing and development - -The database would be accessible through localhost +## Postgres with Docker Compose +Using Docker compose, you can skip the database migration steps for testing and development. +The database will bind to localhost:5432. ```sh # start/restart make docker-db/up From 11526b4539349807d4cb937dac8dab79c7950b0c Mon Sep 17 00:00:00 2001 From: Mohamed Sallam Date: Sun, 10 Aug 2025 10:53:27 +0300 Subject: [PATCH 9/9] Address documentation feedback --- docs/development.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/docs/development.md b/docs/development.md index 7c5139e7..6c97467a 100644 --- a/docs/development.md +++ b/docs/development.md @@ -44,7 +44,8 @@ $ river migrate-up --database-url postgres://localhost/river_dev ## Postgres with Docker Compose Using Docker compose, you can skip the database migration steps for testing and development. -The database will bind to localhost:5432. + +The database will be bound to `localhost:5432`. ```sh # start/restart make docker-db/up