From daf60d33e892d36a6333ef69c82e482975ef8e28 Mon Sep 17 00:00:00 2001 From: "n.tolstov" Date: Sun, 30 Nov 2025 20:27:35 +0300 Subject: [PATCH] feat: implement robust database migration via new entrypoint script with retry logic --- backend/Dockerfile | 6 +++--- backend/entrymigrate.sh | 19 +++++++++++++++++++ docker-compose.yml | 2 +- 3 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 backend/entrymigrate.sh diff --git a/backend/Dockerfile b/backend/Dockerfile index ac0cf55..f0d8d86 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -17,11 +17,11 @@ RUN pip install --no-cache-dir -r requirements.txt # Copy application code COPY . . -# Migrate database -RUN alembic upgrade head - # Expose port EXPOSE 8000 +# Use the entrypoint so migrations run before CMD +ENTRYPOINT ["/app/entrypoint.sh"] + # Run the application CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"] diff --git a/backend/entrymigrate.sh b/backend/entrymigrate.sh new file mode 100644 index 0000000..ded4b66 --- /dev/null +++ b/backend/entrymigrate.sh @@ -0,0 +1,19 @@ +#!/bin/sh +set -e + +MAX_RETRIES=10 +COUNT=0 + +echo "Waiting for DB and running alembic migrations..." +until alembic upgrade head; do + COUNT=$((COUNT+1)) + if [ "$COUNT" -ge "$MAX_RETRIES" ]; then + echo "Alembic migrations failed after $COUNT attempts." >&2 + exit 1 + fi + echo "Alembic failed, retrying in 3s... ($COUNT/$MAX_RETRIES)" + sleep 3 +done + +echo "Migrations applied." +exec "$@" diff --git a/docker-compose.yml b/docker-compose.yml index 5d7477e..c14a9ab 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -50,7 +50,7 @@ services: piston: condition: service_started restart: unless-stopped - + # Next.js frontend frontend: build: