Add migration preflight and sync prod DB
Some checks failed
CI / checks (push) Waiting to run
CI / checks (pull_request) Has been cancelled

This commit is contained in:
Tero Halla-aho 2025-12-20 22:59:48 +02:00
parent dabe6b6268
commit ed67f4305a
2 changed files with 25 additions and 2 deletions

View file

@ -101,3 +101,5 @@
## 2025-12-20 — Migration history repair ## 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. - 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. - 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.

View file

@ -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 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:-}" local db_url="${DATABASE_URL:-}"
if [[ -z "$db_url" ]]; then if [[ -z "$db_url" ]]; then
# If DATABASE_URL isn't available locally, try to reuse the in-cluster secret. # If DATABASE_URL isn't available locally, try to reuse the in-cluster secret.
@ -87,6 +87,25 @@ maybe_run_prisma_migrations() {
fi fi
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 if [[ -n "$db_url" ]]; then
echo "Running Prisma migrations for APP_ENV=$APP_ENV (namespace=$K8S_NAMESPACE)" echo "Running Prisma migrations for APP_ENV=$APP_ENV (namespace=$K8S_NAMESPACE)"
DATABASE_URL="$db_url" npx prisma migrate deploy DATABASE_URL="$db_url" npx prisma migrate deploy
@ -95,7 +114,9 @@ maybe_run_prisma_migrations() {
fi 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) TMP_MANIFEST=$(mktemp)
envsubst < k8s/app.yaml > "$TMP_MANIFEST" envsubst < k8s/app.yaml > "$TMP_MANIFEST"