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 <noreply@anthropic.com>
This commit is contained in:
Hardik 2026-06-19 11:51:59 +05:30
parent 7daf3091bc
commit 4da39fe5d1
2 changed files with 24 additions and 1 deletions

View file

@ -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

View file

@ -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