Files
worldwideview/docker-entrypoint.sh
Silvertakana 7f7d7588f3 fix(docker): auto-baseline Prisma P3005 on container startup (#181)
* fix: add id-token write permission and fix invalid inputs in GC workflow

The claude-code-action@v1 uses OIDC for OAuth token auth, which requires
id-token: write in workflow permissions. Without it ACTIONS_ID_TOKEN_REQUEST_URL
is not set and the action fails after 3 retry attempts.

Also moves model and allowed_tools from unsupported top-level inputs into
claude_args where they are valid CLI flags for claude-code-action@v1.

* fix(gc): show agent output and explicit dry-run issue posting

- Add show_full_output: true so Claude responses appear in CI logs
- Replace vague dry-run instruction with explicit gh issue create/comment
  commands so output has a concrete destination (tracking issue)
- Bake DRY_RUN value into prompt at workflow-expansion time to eliminate
  ambiguity about env var reading

* docs: mark cloud-auth-architecture.md as superseded by ADR-003

* fix(docker): guard community seeder cd and add ENCRYPTION_MASTER_KEY default

- Wrap `cd $SEEDERS_DIR/community` in an existence check so the
  data-engine container no longer crashes when local-seeders/community/
  is an empty or uncloned nested git repo. The container was entering
  Docker's exponential-backoff restart loop (2s→60s) on every boot.
- Add ENCRYPTION_MASTER_KEY to the wwv environment block with a
  local-dev fallback, matching the AUTH_SECRET pattern. Without it the
  Next.js instrumentation hook throws at startup and the app never
  serves a request.

* fix(docker): auto-baseline P3005 when schema exists without migration history

Wraps prisma migrate deploy in the container entrypoint to detect P3005
(schema populated by pnpm dev / prisma db push before the Docker stack was
started). When detected, runs prisma migrate diff to confirm zero schema
drift, then marks each migration as applied via migrate resolve --applied
and retries deploy. Exits with a clear error if drift is found.

Also documents the auto-baseline behavior in database-migrations.md and
fixes duplicate section numbering in that file.
2026-05-23 16:11:13 +12:00

100 lines
4.3 KiB
Bash

#!/bin/sh
# ─── Docker Entrypoint ───────────────────────────────────────
# Ensures the PostgreSQL database is migrated before starting
# the application. DATABASE_URL must point to a PostgreSQL
# instance (Supabase, self-hosted, etc).
set -e
if [ -z "$DATABASE_URL" ]; then
echo "────────────────────────────────────────────────────────────────"
echo "🚨 CRITICAL: DATABASE_URL IS NOT SET"
echo "────────────────────────────────────────────────────────────────"
echo "WorldWideView has migrated to PostgreSQL. If you are an old user,"
echo "please update your docker-compose.yml to include a Postgres service."
echo ""
echo "To migrate your legacy data after setting up Postgres, run:"
echo "docker exec -it <container_name> node scripts/migrate-legacy.mjs"
echo "────────────────────────────────────────────────────────────────"
# If dev.db exists, we know they are upgrading. Don't crash, just wait for config.
if [ -f "prisma/dev.db" ]; then
echo "[entrypoint] Legacy data detected. Waiting for DATABASE_URL to be configured..."
# We don't exit here, we'll let the app try to start or just sleep.
# For now, let's just let it proceed so they can see the setup page error.
else
echo "[entrypoint] Exiting. Please provide a DATABASE_URL."
exit 1
fi
fi
echo "[entrypoint] Running database migrations..."
set +e
MIGRATE_OUT=$(prisma migrate deploy 2>&1)
MIGRATE_CODE=$?
set -e
if [ $MIGRATE_CODE -eq 0 ]; then
echo "[entrypoint] Migrations complete."
elif echo "$MIGRATE_OUT" | grep -q "P3005"; then
# P3005: schema exists but _prisma_migrations has no history.
# Happens when 'prisma db push' (pnpm dev) ran before 'prisma migrate deploy' (docker).
# Safe to auto-baseline only if there is zero schema drift.
echo "[entrypoint] P3005: schema exists without migration history."
echo "[entrypoint] Verifying schema matches migrations before auto-baselining..."
set +e
DIFF_OUT=$(prisma migrate diff \
--from-url "$DATABASE_URL" \
--to-schema-datamodel prisma/schema.prisma \
--script 2>&1)
DIFF_CODE=$?
set -e
if [ $DIFF_CODE -ne 0 ]; then
echo "[entrypoint] Could not verify schema state — aborting." >&2
echo "$DIFF_OUT" >&2
exit 1
fi
# Positive-match for DDL keywords: any real drift emits at least one of these.
# Checking for presence is safer than checking for absence of content.
if echo "$DIFF_OUT" | grep -qiE "^(CREATE|ALTER|DROP|INSERT|UPDATE|DELETE|TRUNCATE)"; then
echo "[entrypoint] Schema drift detected — cannot auto-baseline." >&2
echo "[entrypoint] Run 'pnpm db:reset' to wipe and re-migrate, or resolve drift manually." >&2
echo "$MIGRATE_OUT" >&2
exit 1
fi
echo "[entrypoint] No drift detected. Baselining existing schema..."
for migration_dir in prisma/migrations/*/; do
if [ -f "${migration_dir}migration.sql" ]; then
migration_name=$(basename "$migration_dir")
echo "[entrypoint] Marking as applied: $migration_name"
set +e
prisma migrate resolve --applied "$migration_name"
RESOLVE_CODE=$?
set -e
if [ $RESOLVE_CODE -ne 0 ]; then
echo "[entrypoint] Failed to baseline $migration_name — aborting." >&2
exit 1
fi
fi
done
prisma migrate deploy
echo "[entrypoint] Migrations complete."
else
echo "$MIGRATE_OUT" >&2
exit $MIGRATE_CODE
fi
# Generate self-signed SSL certificates for local HTTPS bridging if they don't exist
if [ ! -f "./data/localhost.crt" ] || [ ! -f "./data/localhost.key" ]; then
echo "[entrypoint] Generating self-signed SSL certificates for port 3001..."
mkdir -p ./data
openssl req -nodes -new -x509 -keyout ./data/localhost.key -out ./data/localhost.crt -days 365 -subj "/CN=localhost" 2>/dev/null || echo "[entrypoint] Warning: Failed to generate SSL certs"
fi
# Start the HTTPS proxy in the background
if [ -f "./scripts/https-proxy.mjs" ]; then
node ./scripts/https-proxy.mjs &
fi
exec node server.js