diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f0119bf97..b0118bf59 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -44,7 +44,7 @@ permissions: # Client: Bundle the production build, one chunk per map engine # Client: End-to-End the app in Chromium and WebKit # Plugin SDK: Types & Coverage -# Repo: Docker Image builds the image and boots it until ready +# Repo: Docker Image builds the image and boots it until ready, on SQLite and on Postgres # Repo: Helm Chart & Compose # Repo: Sonar Scan SonarCloud analysis, not required # ci-ok the one required check (below) @@ -282,6 +282,13 @@ jobs: # migration that changes a column regenerates it in the same change. run: cd server && npm run check:db-types + - name: Postgres baseline matches the entities + # server/assets/postgres/ is the schema a fresh Postgres database starts + # from, generated from the entities by gen:pg-baseline. Until a migration + # lands after its cut it must match them exactly; after, it is frozen + # and only its list of migrations is checked. + run: cd server && npm run check:pg-baseline + server-lint-checks: name: 'Server: Lint & Checks' needs: changes @@ -1211,10 +1218,72 @@ jobs: docker logs trek-ci 2>&1 | grep 'SIGTERM received' >/dev/null \ || { echo "::error::the server never saw SIGTERM"; exit 1; } + # The same image on a Postgres database (TREK_DB_URL): an empty database + # gets the baseline schema, the admin is seeded, and the server answers + # ready. The database runs beside it on a private network, never exposed. + - name: Start the container on Postgres + run: | + set -euo pipefail + docker network create trek-ci-pg + docker run -d --name trek-ci-db --network trek-ci-pg \ + -e POSTGRES_PASSWORD=ci -e POSTGRES_DB=trek postgres:17 + for _ in $(seq 1 30); do + docker exec trek-ci-db pg_isready -U postgres -d trek >/dev/null 2>&1 && break + sleep 2 + done + docker exec trek-ci-db pg_isready -U postgres -d trek \ + || { echo "::error::Postgres did not accept connections within 60 seconds."; exit 1; } + mkdir -p "$RUNNER_TEMP/trek-pg/data" "$RUNNER_TEMP/trek-pg/uploads" + docker run -d --name trek-ci-on-pg --network trek-ci-pg -p 127.0.0.1:3001:3000 \ + --read-only --tmpfs /tmp:noexec,nosuid,size=128m \ + --security-opt no-new-privileges:true \ + --cap-drop ALL --cap-add CHOWN --cap-add SETUID --cap-add SETGID --cap-add KILL \ + -v "$RUNNER_TEMP/trek-pg/data:/app/data" \ + -v "$RUNNER_TEMP/trek-pg/uploads:/app/uploads" \ + -e ENCRYPTION_KEY="$(openssl rand -hex 32)" \ + -e TREK_DB_URL=postgres://postgres:ci@trek-ci-db:5432/trek \ + trek:ci + + # Twice: the first start creates the schema in the empty database, the + # restart finds it migrated and must start without touching it. + - name: Wait until the server on Postgres is ready, then again after a restart + run: | + wait_ready() { + for _ in $(seq 1 90); do + if [ "$(docker inspect -f '{{.State.Running}}' trek-ci-on-pg)" != "true" ]; then + echo "::error::The container on Postgres exited before it became ready." + exit 1 + fi + if curl -sf http://127.0.0.1:3001/api/health/ready; then + echo + return 0 + fi + sleep 2 + done + echo "::error::/api/health/ready on Postgres did not answer within 180 seconds." + exit 1 + } + wait_ready + docker restart -t 30 trek-ci-on-pg >/dev/null + wait_ready + + - name: Stop the container on Postgres gracefully + run: | + set -euo pipefail + docker stop -t 30 trek-ci-on-pg >/dev/null + code="$(docker inspect -f '{{.State.ExitCode}}' trek-ci-on-pg)" + [ "$code" = "0" ] || { echo "::error::docker stop ended with exit code $code, not a graceful shutdown"; exit 1; } + - name: Container log if: always() run: docker logs trek-ci 2>&1 || true + - name: Container log on Postgres + if: always() + run: | + docker logs trek-ci-on-pg 2>&1 || true + docker logs trek-ci-db 2>&1 | tail -50 || true + # The Helm chart and the compose files are what self-hosters deploy from, # and before this job only a release ever touched them. The chart is linted, # rendered with the defaults and with each secret and persistence mode, and diff --git a/CLAUDE.md b/CLAUDE.md index e5de590eb..6563d96ae 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -53,6 +53,7 @@ cd server && npm run lint:boundaries # no new import cycles or cro cd server && npm run lint:dialect # SQLite-only SQL spellings may only shrink (CI gate; rules in server/CLAUDE.md) cd server && npm run lint:engine-dispatch # engine checks outside src/db/dialect/ and src/db/engine/ may only shrink (CI gate) cd server && npm run probe:pg # dialect helpers + the repository statements it reaches against Postgres (CI job; needs TREK_PG_PROBE_URL) +cd server && npm run check:pg-baseline # the Postgres baseline in server/assets/postgres/ matches the entities and the migrations (CI gate) cd server && npm run lint:tx # no new method writing twice outside one transaction (CI gate) cd server && npm run lint:test-sql # raw SQL fixtures in tests/ may only shrink; seed through tests/helpers/factories (CI gate) cd server && npm run lint:test-mocks # vi.mock of config, realtime and db modules in tests/ may only shrink (CI gate) diff --git a/Dockerfile b/Dockerfile index 6253a8eaf..d941c1e91 100644 --- a/Dockerfile +++ b/Dockerfile @@ -91,8 +91,9 @@ ENV KITINERARY_EXTRACTOR_PATH=/usr/local/bin/kitinerary-extractor COPY --chown=node:node --from=server-builder /app/server/dist ./server/dist # Runtime data assets read from server/assets at runtime: airports.json (flight -# transport search) and atlas/*.geojson.gz + atlas/admin1/*.geojson.gz (Atlas -# country/region map). The build +# transport search), atlas/*.geojson.gz + atlas/admin1/*.geojson.gz (Atlas +# country/region map) and postgres/baseline.* (the schema a fresh Postgres +# database starts from). The build # only emits dist, so these must be copied explicitly or the features silently # degrade to empty in the image. COPY --chown=node:node --from=server-builder /app/server/assets ./server/assets diff --git a/server/.env.example b/server/.env.example index 6b0cc80be..8778acca5 100644 --- a/server/.env.example +++ b/server/.env.example @@ -52,7 +52,7 @@ DEMO_MODE=false # Demo mode: resets data hourly # BACKUP_MAX_DECOMPRESSED_MB=5120 # Max size (MB) a restore archive may unpack to, on both restore paths: the zip-bomb guard. Raise it if a large backup is refused with "Backup exceeds the maximum decompressed size". (default: 5120) # BACKUP_UPLOAD_LIMIT_MB=500 # Max size (MB) of a backup archive you can upload when restoring. Raise it if your backup exceeds 500 MB. If you sit behind a reverse proxy, raise its upload limit too (e.g. nginx client_max_body_size). -# TREK_DB_URL=postgres://trek:password@db:5432/trek # Run on a Postgres database instead of the SQLite file in data/. Not supported yet: a boot with it set is refused. Cannot be combined with TREK_DB_FILE, TREK_DB_JOURNAL_MODE or TREK_DB_SYNCHRONOUS. +# TREK_DB_URL=postgres://trek:password@db:5432/trek # Run on a Postgres database instead of the SQLite file in data/. TREK creates its schema in an empty database and refuses one holding tables it did not create. sslmode=disable, require, verify-ca or verify-full (the last three always verify the certificate; sslrootcert= names the CA file). Cannot be combined with TREK_DB_FILE, TREK_DB_JOURNAL_MODE or TREK_DB_SYNCHRONOUS. # TREK_DB_POOL_MAX=10 # Most connections TREK opens to the Postgres database (1 to 100). Unused on SQLite. (default: 10) # TREK_DB_JOURNAL_MODE=WAL # SQLite journal mode (DELETE, TRUNCATE, PERSIST, MEMORY, WAL, OFF). WAL is fastest and the right choice on local disk. Set DELETE if the data directory lives on network storage (Azure App Service, an SMB/NFS volume on a NAS) — WAL coordinates writers through a shared-memory file that those filesystems do not implement reliably. The mode is stored inside the database file, so it survives a restart; changing this applies on the next boot. # TREK_DB_PRE_MIGRATE_SNAPSHOT=true # Copy the database to data/pre-migrate--