Skip to content

fix(make): isolate docker compose project and ports per checkout - #4

Merged
jayleaton merged 1 commit into
mainfrom
fix/test-db-isolation
Oct 22, 2026
Merged

jayleaton merged 1 commit into
mainfrom
fix/test-db-isolation

Conversation

@jayleaton

Copy link
Copy Markdown
Owner

Problem

make test and make db-up used one fixed compose project (ai-patterns-go) and fixed host ports (test Postgres 54329, Postgres 5432, pgweb 5050). Parallel worktrees, the normal way agents work on this template, either shared each other's containers or failed with "port is already allocated". The mobile branch hit this on 54339.

How

make (any checkout)
  CHECKOUT_ID  = cksum(path of the checkout)
  project      = <dir>-<hash>               e.g. feat-foundation-6e0df7, wt2-409717
  postgres     = 25000 + id % 1000          stable per checkout, so DATABASE_URL is stable
  pgweb        = 26000 + id % 1000
  test pg      = 127.0.0.1::5432            Docker picks a free port; make test reads it back
  • Overrides: every value can be set in the environment or .env (COMPOSE_PROJECT_NAME, POSTGRES_PORT, PGWEB_PORT, TEST_POSTGRES_PORT, DATABASE_URL). make test uses TEST_DATABASE_URL as is when set.
  • make env prints the values, and db-up and test print them too.
  • .env.example no longer hardcodes DATABASE_URL; make derives it.
  • Docs: AGENTS.md and README.md now describe this.
  • CI is unaffected. It runs go test with its own TEST_DATABASE_URL and never calls make.

Evidence

Run in two worktrees started concurrently: this one and a second worktree at the same commit (later removed). Both ran GOFLAGS=-count=1 make test at the same time:

worktree A worktree B
compose project feat-foundation-6e0df7 wt2-409717
test Postgres 127.0.0.1:32770 127.0.0.1:32771
result all 7 packages ok all 7 packages ok

Both finished in 14 s. Concurrent make db-up + make migrate also worked: Postgres 25980/25709, pgweb 26980/26709, and migrations applied in both. make ci passes locally in 18.5 s; gofmt and golangci-lint report 0 issues.

Merge Danger

Door: two-way. This only changes local tooling.

Blast Radius: local-dev. Existing checkouts get new default ports for the dev database. A .env that pins POSTGRES_PORT/DATABASE_URL keeps working. A dev database created under the old shared project is not reused; run make db-up && make migrate once, or set COMPOSE_PROJECT_NAME=ai-patterns-go in .env to keep it.

🤖 Generated with Claude Code

Parallel worktrees shared one compose project and fixed ports, so a
second make test reused or collided with another worktree's Postgres.
The Makefile now derives the compose project name and dev Postgres/pgweb
ports from the checkout path, publishes the test Postgres on a free port
chosen by Docker, and prints them (make env). All values stay
overridable via env or .env; CI calls go test directly and is unaffected.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@jayleaton
jayleaton marked this pull request as ready for review October 22, 2026 07:05
@jayleaton
jayleaton merged commit 3864a94 into main Oct 22, 2026
8 checks passed
@jayleaton
jayleaton deleted the fix/test-db-isolation branch October 22, 2026 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant