deploy: run migrations using in-cluster DATABASE_URL

This commit is contained in:
Tero Halla-aho 2025-12-18 12:45:30 +02:00
parent 16fa9bb051
commit 675c0c0924
2 changed files with 26 additions and 7 deletions

View file

@ -24,4 +24,4 @@ Deploy commands
Notes
- Ensure `deploy/.last-image` exists (run `deploy/build.sh` first).
- `AUTH_SECRET`/`DATABASE_URL` should be in your env or loaded via `scripts/load-secrets.sh`.
- `deploy/deploy.sh` now runs `prisma migrate deploy` automatically when `DATABASE_URL` is set (recommended for test/staging/prod).
- `deploy/deploy.sh` runs `prisma migrate deploy` automatically when `DATABASE_URL` is set; if it isn't, it will try to read `DATABASE_URL` from the in-cluster `lomavuokraus-web-secrets` in the target namespace (recommended for test/staging/prod).

View file

@ -71,12 +71,31 @@ APP_VERSION="${APP_VERSION:-$(echo \"$IMAGE\" | awk -F: '{print $NF}')}"
export K8S_NAMESPACE APP_HOST API_HOST NEXT_PUBLIC_SITE_URL NEXT_PUBLIC_API_BASE APP_ENV CLUSTER_ISSUER INGRESS_CLASS APP_REPLICAS K8S_IMAGE APP_VERSION
if [[ -n "${DATABASE_URL:-}" ]]; then
echo "Running Prisma migrations for APP_ENV=$APP_ENV"
npx prisma migrate deploy
else
echo "DATABASE_URL not set; skipping Prisma migrations" >&2
fi
maybe_run_prisma_migrations() {
local db_url="${DATABASE_URL:-}"
if [[ -z "$db_url" ]]; then
# If DATABASE_URL isn't available locally, try to reuse the in-cluster secret.
# This prevents "works in cluster but deploy skipped migrations" drift.
if command -v kubectl >/dev/null 2>&1 && command -v jq >/dev/null 2>&1; then
if kubectl -n "$K8S_NAMESPACE" get secret lomavuokraus-web-secrets >/dev/null 2>&1; then
db_url="$(
kubectl -n "$K8S_NAMESPACE" get secret lomavuokraus-web-secrets -o json \
| jq -r '.data.DATABASE_URL // empty' \
| base64 -d 2>/dev/null || true
)"
fi
fi
fi
if [[ -n "$db_url" ]]; then
echo "Running Prisma migrations for APP_ENV=$APP_ENV (namespace=$K8S_NAMESPACE)"
DATABASE_URL="$db_url" npx prisma migrate deploy
else
echo "DATABASE_URL not set and lomavuokraus-web-secrets/DATABASE_URL not found; skipping Prisma migrations" >&2
fi
}
maybe_run_prisma_migrations
TMP_MANIFEST=$(mktemp)
envsubst < k8s/app.yaml > "$TMP_MANIFEST"