skip db migration on startup

This commit is contained in:
2026-01-15 17:06:42 +02:00
parent 48870802ca
commit a80b2b8d5e

View File

@@ -31,27 +31,59 @@ if [ "$NODE_ENV" = "production" ]; then
echo "❌ ERROR: SUPABASE_SERVICE_ROLE_KEY is required in production $(date '+%Y-%m-%d %H:%M:%S')" echo "❌ ERROR: SUPABASE_SERVICE_ROLE_KEY is required in production $(date '+%Y-%m-%d %H:%M:%S')"
exit 1 exit 1
fi fi
# Optional: Check URL formats (basic regex) # Optional: Check URL formats (basic regex)
if ! echo "$DATABASE_URL" | grep -qE '^postgres(ql)?://'; then if ! echo "$DATABASE_URL" | grep -qE '^postgres(ql)?://'; then
echo "❌ ERROR: DATABASE_URL must be a valid PostgreSQL connection string $(date '+%Y-%m-%d %H:%M:%S')" echo "❌ ERROR: DATABASE_URL must be a valid PostgreSQL connection string $(date '+%Y-%m-%d %H:%M:%S')"
exit 1 exit 1
fi fi
echo "✅ Environment validation passed $(date '+%Y-%m-%d %H:%M:%S')" echo "✅ Environment validation passed $(date '+%Y-%m-%d %H:%M:%S')"
fi fi
# Run database migrations if DATABASE_URL is set # Allow skipping health check database validation for debugging
if [ -n "$DATABASE_URL" ]; then if [ "$SKIP_HEALTH_DB_CHECK" = "1" ]; then
echo "🔄 Running database migrations... $(date '+%Y-%m-%d %H:%M:%S')" echo "⚠️ Skipping database check in health endpoint (SKIP_HEALTH_DB_CHECK=1) $(date '+%Y-%m-%d %H:%M:%S')"
migration_output=$(npx prisma migrate deploy 2>&1) fi
migration_status=$?
echo "$migration_output" # Run database migrations if DATABASE_URL is set and not skipped
if [ $migration_status -ne 0 ] && ! echo "$migration_output" | grep -q "All migrations are already applied"; then if [ -n "$DATABASE_URL" ] && [ "$SKIP_MIGRATIONS" != "1" ]; then
echo "❌ ERROR: Prisma migrations failed $(date '+%Y-%m-%d %H:%M:%S')" echo "🔄 Testing database connectivity... $(date '+%Y-%m-%d %H:%M:%S')"
exit 1
# Test basic database connectivity
echo "SELECT 1;" > /tmp/test.sql
if npx prisma db execute --file /tmp/test.sql >/dev/null 2>&1; then
echo "✅ Database connection successful $(date '+%Y-%m-%d %H:%M:%S')"
rm -f /tmp/test.sql
else
echo "❌ ERROR: Cannot connect to database $(date '+%Y-%m-%d %H:%M:%S')"
echo "Database URL: $(echo "$DATABASE_URL" | sed 's|//.*@|//***:***@|')" # Hide credentials in logs
echo "⚠️ Continuing without migrations..."
rm -f /tmp/test.sql
fi fi
echo "✅ Migrations complete $(date '+%Y-%m-%d %H:%M:%S')"
echo "🔄 Running database migrations... $(date '+%Y-%m-%d %H:%M:%S')"
# Run migrations with timeout to prevent hanging
timeout 120 sh -c '
echo "Running: npx prisma migrate deploy"
npx prisma migrate deploy 2>&1
exit_code=$?
echo "Migration command completed with exit code: $exit_code"
exit $exit_code
' || {
migration_status=$?
echo "❌ ERROR: Prisma migrations failed or timed out (status: $migration_status) $(date '+%Y-%m-%d %H:%M:%S')"
if [ $migration_status -eq 124 ]; then
echo "⚠️ Migration command timed out after 120 seconds"
fi
# Continue anyway - don't fail the startup for migration issues in production
echo "⚠️ Continuing startup despite migration issues..."
}
echo "✅ Migrations step complete $(date '+%Y-%m-%d %H:%M:%S')"
elif [ "$SKIP_MIGRATIONS" = "1" ]; then
echo "⏭️ Skipping database migrations (SKIP_MIGRATIONS=1) $(date '+%Y-%m-%d %H:%M:%S')"
fi fi
# Optional: Run seeding if RUN_SEED=1 (set in env vars) # Optional: Run seeding if RUN_SEED=1 (set in env vars)