From 4da39fe5d155a49dd0fdf8935e5d1427156b92d2 Mon Sep 17 00:00:00 2001 From: Hardik Date: Fri, 19 Jun 2026 11:51:59 +0530 Subject: [PATCH] fix(automation): apply master migrations to the test DB The test DB mirrors prod, which can be behind master, so the latest code 500s on columns prod doesn't have yet (e.g. poDate from the optional-PO-date feature). - staging-up.sh runs prisma migrate deploy after install. - refresh-test-db.sh re-applies master migrations after each nightly data copy, so the running staging/autofix DB stays at the schema of the code under test. Co-Authored-By: Claude Opus 4.8 --- automation/refresh-test-db.sh | 21 ++++++++++++++++++++- automation/staging-up.sh | 4 ++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/automation/refresh-test-db.sh b/automation/refresh-test-db.sh index bf01590..88f01aa 100644 --- a/automation/refresh-test-db.sh +++ b/automation/refresh-test-db.sh @@ -36,7 +36,7 @@ prod_tables=$(psql -tAc "SELECT count(*) FROM information_schema.tables WHERE ta test_tables=$(psql -tAc "SELECT count(*) FROM information_schema.tables WHERE table_schema='public';" "$TEST_URL") if [ "$test_tables" = "$prod_tables" ] && [ "$test_tables" -gt 0 ]; then - log "Done. $TEST_DB has $test_tables public tables (prod has $prod_tables)." + log "Data copied. $TEST_DB has $test_tables public tables (prod has $prod_tables)." rm -f "$errfile" else log "WARNING: table counts differ (test=$test_tables prod=$prod_tables). Recent errors:" @@ -44,3 +44,22 @@ else rm -f "$errfile" exit 1 fi + +# The test DB now has PROD's schema, which may be behind master. Apply master's +# unreleased migrations so the code under test (staging + autofix) doesn't 500 on +# columns prod doesn't have yet (e.g. poDate). Uses a stable master checkout. +MIG_DIR="" +for d in "$HOME/pelagia-staging/App" "$HOME/pelagia-autofix/App"; do + [ -d "$d/prisma/migrations" ] && { MIG_DIR="$d"; break; } +done +if [ -n "$MIG_DIR" ]; then + export NVM_DIR="$HOME/.nvm"; . "$NVM_DIR/nvm.sh" 2>/dev/null || true + log "Applying master migrations from $MIG_DIR ..." + if ( cd "$MIG_DIR" && DATABASE_URL="$TEST_URL" pnpm db:migrate:deploy ) >/tmp/migrate-test-db.log 2>&1; then + log "Migrations applied." + else + log "WARNING: migrate deploy failed; see /tmp/migrate-test-db.log"; tail -5 /tmp/migrate-test-db.log + fi +else + log "No master checkout with migrations found; skipping migrate (test DB has prod schema only)." +fi diff --git a/automation/staging-up.sh b/automation/staging-up.sh index 79e9e44..ec8a64c 100644 --- a/automation/staging-up.sh +++ b/automation/staging-up.sh @@ -59,6 +59,10 @@ chmod +x "$DIR/App/run-staging.sh" cd "$DIR/App" echo "Installing deps..."; pnpm install --frozen-lockfile echo "Generating Prisma client..."; pnpm db:generate +# Bring the test DB schema up to the code under test. The test DB mirrors prod, +# which may be behind master, so master's unreleased migrations (e.g. poDate) +# must be applied or the new code 500s on the missing columns. +echo "Applying pending migrations to the test DB..."; pnpm db:migrate:deploy if pm2 describe "$NAME" >/dev/null 2>&1; then pm2 restart "$NAME" --update-env