feat: implement robust database migration via new entrypoint script with retry logic

This commit is contained in:
n.tolstov 2025-11-30 20:27:35 +03:00
parent b67c39034c
commit daf60d33e8
3 changed files with 23 additions and 4 deletions

View File

@ -17,11 +17,11 @@ RUN pip install --no-cache-dir -r requirements.txt
# Copy application code # Copy application code
COPY . . COPY . .
# Migrate database
RUN alembic upgrade head
# Expose port # Expose port
EXPOSE 8000 EXPOSE 8000
# Use the entrypoint so migrations run before CMD
ENTRYPOINT ["/app/entrypoint.sh"]
# Run the application # Run the application
CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"] CMD ["uvicorn", "app.main:app", "--host", "0.0.0.0", "--port", "8000", "--reload"]

19
backend/entrymigrate.sh Normal file
View File

@ -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 "$@"

View File

@ -50,7 +50,7 @@ services:
piston: piston:
condition: service_started condition: service_started
restart: unless-stopped restart: unless-stopped
# Next.js frontend # Next.js frontend
frontend: frontend:
build: build: