fix(deploy): migrate after stop and shrink DB pool
Deploy / release (push) Skipped
Deploy / deploy (push) Successful in 1m52s

Stage migrate hit ER_CON_COUNT_ERROR while live still held the pool. Build in stage with DATABASE_POOL_SIZE=5, run db:migrate only after stopping the service (with retries), and lower the default pool from 40 to 10.

Co-authored-by: Cursor <[email protected]>
This commit is contained in:
SimoandCursor committed 2026-07-21 21:44:06 +02:00
1 parent 687e1f9fb0
commit fa40eebeed
4 files changed
+34 -7

No files matched your search

+26 -3
View File
@@ -293,9 +293,13 @@ jobs:
find . -maxdepth 3 -name '*.tsbuildinfo' -delete 2>/dev/null || true
rm -rf .output dist .next .next/types .next/dev
# Stage shares MySQL with the live app + emulator. Keep the stage pool
# tiny so install/test/build cannot exhaust max_connections.
export DATABASE_POOL_SIZE="${DEPLOY_DATABASE_POOL_SIZE:-5}"
echo "STAGE DATABASE_POOL_SIZE=${DATABASE_POOL_SIZE}"
pnpm install --frozen-lockfile
# Additive migrations while the old build still serves traffic.
pnpm db:migrate
# prisma generate does not need a live DB connection.
pnpm prisma:generate
pnpm typecheck
pnpm test
@@ -308,10 +312,29 @@ jobs:
exit 1
fi
echo "Cutover: stop service, sync code, swap .next artifact..."
echo "Cutover: stop service (free DB connections), migrate, swap .next..."
CUTOVER_STARTED=1
sudo systemctl stop atom-nexst.service || true
pkill -f 'next-server' || true
# Give MariaDB a moment to reclaim the live pool.
sleep 2
# Migrate only after live is stopped — avoids ER_CON_COUNT_ERROR while
# the old process still holds DATABASE_POOL_SIZE connections.
cd "${STAGE}"
MIGRATE_OK=0
for i in 1 2 3 4 5; do
if pnpm db:migrate; then
MIGRATE_OK=1
break
fi
echo "migrate attempt ${i}/5 failed (likely DB connections), retrying..."
sleep 3
done
if [ "${MIGRATE_OK}" != "1" ]; then
echo "ERROR: db:migrate failed after retries" >&2
exit 1
fi
cd "${LIVE}"
echo "Hard reset live tree to origin/main (no nuclear src wipe)..."