Enhance entrypoint script for migration handling: Improve error resolution process by adding direct database updates for missing migration files, and refine output messages for clarity during deployment.
All checks were successful
Deploy with Docker Compose / deploy (push) Successful in 3m6s

This commit is contained in:
Julien Froidefond
2025-12-15 17:58:40 +01:00
parent d45475fb5a
commit 9b9cc3885a
8 changed files with 78 additions and 56 deletions

View File

@@ -45,34 +45,65 @@ deploy_migrations() {
echo "Resolving failed migration: $MIGRATION_NAME"
# Try to resolve as applied (migration might have partially succeeded)
# First, try the standard Prisma resolve command
RESOLVE_OUTPUT=$(pnpm dlx prisma migrate resolve --applied "$MIGRATION_NAME" 2>&1)
RESOLVE_EXIT=$?
echo "$RESOLVE_OUTPUT"
if [ $RESOLVE_EXIT -eq 0 ]; then
echo "Migration resolved successfully, retrying deploy..."
# Retry migration deploy
if pnpm dlx prisma migrate deploy 2>&1; then
echo "Migrations deployed successfully after resolution"
return 0
else
echo "Migration deploy still failed after resolution, but continuing..."
return 0
fi
else
# Check if migration is already marked as applied
if echo "$RESOLVE_OUTPUT" | grep -q "already recorded as applied"; then
echo "Migration is already marked as applied, retrying deploy..."
pnpm dlx prisma migrate deploy 2>&1 || true
return 0
# If Prisma resolve fails (e.g., migration files not found), resolve directly in database
if [ $RESOLVE_EXIT -ne 0 ]; then
if echo "$RESOLVE_OUTPUT" | grep -q "P3017\|could not be found"; then
echo "Migration files not found, resolving directly in database..."
# Use sqlite3 to mark migration as applied directly in _prisma_migrations table
# Check if sqlite3 is available
if command -v sqlite3 >/dev/null 2>&1; then
# Extract database path from DATABASE_URL (format: file:/app/data/dev.db)
DB_PATH=$(echo "$DATABASE_URL" | sed 's|file:||')
if [ -z "$DB_PATH" ] || [ "$DB_PATH" = "$DATABASE_URL" ]; then
DB_PATH="/app/data/dev.db"
fi
# Ensure database file exists
if [ ! -f "$DB_PATH" ]; then
echo "Database file not found at $DB_PATH"
else
echo "Updating migration status in database: $DB_PATH"
# Get current timestamp in milliseconds
TIMESTAMP=$(date +%s)000
# Update the migration record
sqlite3 "$DB_PATH" "UPDATE _prisma_migrations SET finished_at = $TIMESTAMP, rolled_back_at = NULL WHERE migration_name = '$MIGRATION_NAME' AND finished_at IS NULL;" 2>&1
if [ $? -eq 0 ]; then
echo "Migration marked as applied in database"
# Verify the update
RESULT=$(sqlite3 "$DB_PATH" "SELECT migration_name, finished_at FROM _prisma_migrations WHERE migration_name = '$MIGRATION_NAME';" 2>&1)
echo "Migration status: $RESULT"
else
echo "Failed to update database directly"
fi
fi
else
echo "sqlite3 not available, cannot resolve directly in database"
echo "Install sqlite3 in Dockerfile: RUN apk add --no-cache sqlite"
fi
elif echo "$RESOLVE_OUTPUT" | grep -q "already recorded as applied"; then
echo "Migration is already marked as applied"
else
echo "Failed to resolve migration: $RESOLVE_OUTPUT"
echo "Continuing anyway - app might still work if migration was partially applied"
return 0
fi
fi
# Retry migration deploy after resolution attempt
echo "Retrying migration deploy..."
if pnpm dlx prisma migrate deploy 2>&1; then
echo "Migrations deployed successfully after resolution"
return 0
else
echo "Migration deploy still failed after resolution, but continuing..."
return 0
fi
}
# Deploy migrations (don't fail if it errors - app might still work)