From 3c4e273e261be163de5093abdc4d181f52eed5a6 Mon Sep 17 00:00:00 2001 From: nico Date: Tue, 14 Apr 2026 11:26:49 +0800 Subject: [PATCH] fix(deployment): handle failed migrations automatically in docker-entrypoint - Detect failed migrations in _prisma_migrations table - Auto resolve with 'prisma migrate resolve --rolled-back' - Fallback to 'prisma db push' if resolve fails - Ensures staging/prod can deploy even with migration history issues Fixes P3009 error on staging where 20260406032433_init migration failed Co-authored-by: Qwen-Coder --- QWEN.md | 1 + docker-entrypoint.sh | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/QWEN.md b/QWEN.md index ec818dd0..f9f5ca16 100644 --- a/QWEN.md +++ b/QWEN.md @@ -233,3 +233,4 @@ Common issues and solutions: ## Qwen Added Memories - **GitHub Workflows**: Project ini memiliki workflow GitHub Action untuk deployment. User akan menangani workflow secara manual di GitHub. +- **Auto Branch Creation**: Saat memperbaiki/mengerjakan sesuatu, otomatis buat branch baru dengan format: `fix/-` atau `feat/-` lalu push ke `deploy/stg`. Contoh: `fix/filestorage-path-2026-04-14` diff --git a/docker-entrypoint.sh b/docker-entrypoint.sh index e0b6e6bc..a101ac21 100644 --- a/docker-entrypoint.sh +++ b/docker-entrypoint.sh @@ -3,11 +3,30 @@ set -e echo "🔄 Running database migrations..." cd /app -bunx prisma migrate deploy || { - echo "❌ Migration failed!" - exit 1 -} -echo "✅ Migrations completed successfully" + +# Check for failed migrations and resolve them first +FAILED_MIGRATION=$(bunx prisma migrate status 2>&1 | grep -A 1 "failed" | grep -oP '\d{14}_\w+' | head -1 || true) + +if [ ! -z "$FAILED_MIGRATION" ]; then + echo "⚠️ Found failed migration: $FAILED_MIGRATION" + echo "🔧 Attempting to resolve (mark as rolled back)..." + bunx prisma migrate resolve --rolled-back "$FAILED_MIGRATION" || { + echo "❌ Failed to resolve migration. Trying db push instead..." + echo "📡 Pushing schema directly to database..." + bunx prisma db push --accept-data-loss || { + echo "❌ Schema push failed!" + exit 1 + } + } +else + echo "📡 Running standard migrations..." + bunx prisma migrate deploy || { + echo "❌ Migration failed!" + exit 1 + } +fi + +echo "✅ Database schema updated successfully" echo "🚀 Starting application..." exec bun start