From 013036ea8162cf99abcd1750cafaf02aaaf55b86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Leon=20Tonh=C3=A4user?= Date: Mon, 10 Aug 2026 23:14:02 +0200 Subject: [PATCH] ops: add migrate/seed/preflight compose profile for clean fresh setup The portal database lives on an internal-only network and is not exposed to the host, so `pnpm drizzle-kit migrate` and admin seeding cannot run from the host. Add one-shot `ops`-profile services built from the builder stage that run migrations, seed the first admin, and run the preflight smoke test against the real env from inside the network. Share the app env via an x-portal-env anchor. Update the runbook to use the new commands. Co-Authored-By: Claude --- compose.yaml | 86 ++++++++++++++++++++++++++++++++++++++-------- docs/operations.md | 23 ++++++++++--- 2 files changed, 89 insertions(+), 20 deletions(-) diff --git a/compose.yaml b/compose.yaml index 2804876..8548905 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,3 +1,21 @@ +# Shared application environment. Services with `environment: *portal-env` below +# all receive the same vars, so migrations, seeding and preflight run against the +# same config as the live app. +x-portal-env: &portal-env + DATABASE_URL: ${DATABASE_URL:-postgres://portal:change-me@portal-db:5432/portal} + BETTER_AUTH_SECRET: ${BETTER_AUTH_SECRET} + BETTER_AUTH_URL: ${BETTER_AUTH_URL:-https://redaktionen.leon-tonhaeuser.de} + IMMICH_BASE_URL: ${IMMICH_BASE_URL} + IMMICH_API_KEY: ${IMMICH_API_KEY} + SMTP_HOST: ${SMTP_HOST} + SMTP_PORT: ${SMTP_PORT:-587} + SMTP_USER: ${SMTP_USER} + SMTP_PASSWORD: ${SMTP_PASSWORD} + SMTP_FROM: ${SMTP_FROM} + LEGAL_IMPRINT_URL: ${LEGAL_IMPRINT_URL} + LEGAL_PRIVACY_URL: ${LEGAL_PRIVACY_URL} + LEGAL_TERMS_URL: ${LEGAL_TERMS_URL} + services: portal-db: image: postgres:17-alpine @@ -25,20 +43,7 @@ services: restart: unless-stopped ports: - "3000:3000" - environment: - DATABASE_URL: ${DATABASE_URL:-postgres://portal:change-me@portal-db:5432/portal} - BETTER_AUTH_SECRET: ${BETTER_AUTH_SECRET} - BETTER_AUTH_URL: ${BETTER_AUTH_URL:-https://redaktionen.leon-tonhaeuser.de} - IMMICH_BASE_URL: ${IMMICH_BASE_URL} - IMMICH_API_KEY: ${IMMICH_API_KEY} - SMTP_HOST: ${SMTP_HOST} - SMTP_PORT: ${SMTP_PORT:-587} - SMTP_USER: ${SMTP_USER} - SMTP_PASSWORD: ${SMTP_PASSWORD} - SMTP_FROM: ${SMTP_FROM} - LEGAL_IMPRINT_URL: ${LEGAL_IMPRINT_URL} - LEGAL_PRIVACY_URL: ${LEGAL_PRIVACY_URL} - LEGAL_TERMS_URL: ${LEGAL_TERMS_URL} + environment: *portal-env depends_on: portal-db: condition: service_healthy @@ -46,6 +51,57 @@ services: - portal-internal - immich-net + # One-shot: apply Drizzle migrations. Runs in the builder stage (which has the + # source + drizzle-kit) on the internal network so it can reach portal-db. + # docker compose --profile ops run --rm migrate + migrate: + build: + context: . + dockerfile: Dockerfile + target: builder + environment: *portal-env + command: ["pnpm", "exec", "drizzle-kit", "migrate"] + depends_on: + portal-db: + condition: service_healthy + networks: + - portal-internal + profiles: ["ops"] + + # One-shot: seed the first admin. Override the command to pass credentials: + # docker compose --profile ops run --rm seed \ + # pnpm tsx scripts/create-admin.ts admin@example.de "Password!" "Name" + seed: + build: + context: . + dockerfile: Dockerfile + target: builder + environment: *portal-env + command: ["pnpm", "tsx", "scripts/create-admin.ts"] + depends_on: + portal-db: + condition: service_healthy + networks: + - portal-internal + profiles: ["ops"] + + # One-shot: smoke test env + DB + Better Auth + SMTP + Immich connectivity. + # docker compose --profile ops run --rm preflight + preflight: + build: + context: . + dockerfile: Dockerfile + target: builder + environment: *portal-env + command: ["pnpm", "tsx", "scripts/preflight.ts"] + depends_on: + portal-db: + condition: service_healthy + networks: + - portal-internal + - immich-net + profiles: ["ops"] + volumes: portal-db-data: @@ -53,4 +109,4 @@ networks: portal-internal: internal: true immich-net: - external: true + external: true \ No newline at end of file diff --git a/docs/operations.md b/docs/operations.md index 35c1739..5c93bb6 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -68,15 +68,28 @@ When placing Nginx or Traefik in front of the portal: --- -## 5. Database Migrations +## 5. Database Migrations, Admin Seed, Preflight -Database schema migrations are managed via Drizzle Kit. +These one-shot tasks run in the `builder` stage on the internal network so they +can reach `portal-db` (which is not exposed to the host). They are grouped under +the `ops` compose profile and do not start with `docker compose up`. -- **Apply Migrations**: +- **Apply migrations** (run before starting a new app version): ```bash - pnpm drizzle-kit migrate + docker compose --profile ops run --rm migrate ``` -- Migrations must be executed prior to starting a new app container version. +- **Seed the first admin** (override the command to pass credentials): + ```bash + docker compose --profile ops run --rm seed \ + pnpm tsx scripts/create-admin.ts admin@example.de "AdminPassword!" "Name" + ``` +- **Smoke-test env + connectivity** (DB, Better Auth, SMTP, Immich): + ```bash + docker compose --profile ops run --rm preflight + ``` + +Migrations and the admin seed must be executed after the database is healthy and +before the app is started for the first time. ---