diff --git a/.env.local b/.env.local index b2ca47d5..b4640f0a 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_test OTEL_ENABLED=false PORT=8080 VITE_RIVER_API_BASE_URL=http://localhost:8080/api diff --git a/Makefile b/Makefile index a2805449..cdcf2950 100644 --- a/Makefile +++ b/Makefile @@ -47,3 +47,12 @@ verify: verify/sqlc .PHONY: verify/sqlc verify/sqlc: cd internal/dbsqlc && sqlc diff + +.PHONY: docker-db/up +docker-db/up: + docker compose -f docker-compose.dev.yaml down + docker compose -f docker-compose.dev.yaml up + +.PHONY: docker-db/down +docker-db/down: + docker compose -f docker-compose.dev.yaml down diff --git a/docker-compose.dev.yaml b/docker-compose.dev.yaml new file mode 100644 index 00000000..ca3b3ccd --- /dev/null +++ b/docker-compose.dev.yaml @@ -0,0 +1,37 @@ +name: riverui-dev + +services: + postgres: + image: "postgres:16-alpine" + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=postgres + healthcheck: + test: ["CMD", "pg_isready", "-d", "river_dev", "-U", "postgres"] + timeout: 20s + retries: 10 + start_period: 3s + volumes: + - ./scripts/docker-compose-dev/postgres/init:/docker-entrypoint-initdb.d + 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 redownload + volumes: + - gopath:/go + command: + - -c + - | + 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: + gopath: diff --git a/docs/development.md b/docs/development.md index a19b8884..6c97467a 100644 --- a/docs/development.md +++ b/docs/development.md @@ -42,6 +42,18 @@ $ go install github.com/riverqueue/river/cmd/river $ 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 be bound to `localhost:5432`. +```sh +# start/restart +make docker-db/up + +# stop +make docker-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