From ed67f4305a4a39a33bd0993ab03e348b8ff7cf64 Mon Sep 17 00:00:00 2001 From: Tero Halla-aho Date: Sat, 20 Dec 2025 22:59:48 +0200 Subject: [PATCH] Add migration preflight and sync prod DB --- PROGRESS.md | 2 ++ deploy/deploy.sh | 25 +++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/PROGRESS.md b/PROGRESS.md index d36caed..8bb1564 100644 --- a/PROGRESS.md +++ b/PROGRESS.md @@ -101,3 +101,5 @@ ## 2025-12-20 — Migration history repair - Restored missing migration `20251212_agent_billing` (agent billing columns + listing billing settings table) so Prisma history matches the DB. - Reconciled test DB migration history: aligned checksum for `20251212_agent_billing` and marked `20260310_site_settings` and `20260311_billing_preferences` as applied to stop Prisma errors and surface listings again. +- Applied the same agent billing schema to staging/prod DB (`lomavuokraus`) and marked the migration as applied; Prisma status now clean there too. +- Deploy script now runs a Prisma migration status preflight using DATABASE_URL from env or in-cluster secret and fails fast on drift before applying manifests. diff --git a/deploy/deploy.sh b/deploy/deploy.sh index 4b19c27..4d1320d 100755 --- a/deploy/deploy.sh +++ b/deploy/deploy.sh @@ -71,7 +71,7 @@ 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 -maybe_run_prisma_migrations() { +resolve_database_url() { local db_url="${DATABASE_URL:-}" if [[ -z "$db_url" ]]; then # If DATABASE_URL isn't available locally, try to reuse the in-cluster secret. @@ -87,6 +87,25 @@ maybe_run_prisma_migrations() { fi fi + echo "$db_url" +} + +prisma_preflight() { + local db_url="$1" + if [[ -z "$db_url" ]]; then + echo "DATABASE_URL unavailable; skipping Prisma status preflight" >&2 + return 0 + fi + + echo "Prisma preflight: checking migration status for APP_ENV=$APP_ENV (namespace=$K8S_NAMESPACE)" + if ! DATABASE_URL="$db_url" npx prisma migrate status >/dev/null; then + echo "Prisma migrate status failed. Fix migration history before deploying." >&2 + exit 1 + fi +} + +maybe_run_prisma_migrations() { + local db_url="$1" 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 @@ -95,7 +114,9 @@ maybe_run_prisma_migrations() { fi } -maybe_run_prisma_migrations +DB_URL_FOR_DEPLOY="$(resolve_database_url)" +prisma_preflight "$DB_URL_FOR_DEPLOY" +maybe_run_prisma_migrations "$DB_URL_FOR_DEPLOY" TMP_MANIFEST=$(mktemp) envsubst < k8s/app.yaml > "$TMP_MANIFEST"