From f9086d2d040be6947d06afbaf26e7a1830e225c0 Mon Sep 17 00:00:00 2001 From: zwitschi Date: Sat, 11 Oct 2025 21:52:30 +0200 Subject: [PATCH] feat: initialize database with demo data on first run and update README --- README.md | 9 +++++++++ backend/Dockerfile | 15 +++++++++++++-- docker-compose.yml | 3 +-- scripts/init_demo_db.py | 4 ++++ 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index c5fb210..e2cec91 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,15 @@ PowerShell / Bash docker compose up --build +This starts all services (Postgres, Redis, backend, frontend) and automatically initializes the database with demo data on first run. The backend waits for the database to be ready before running migrations and loading OSM fixtures. + +**Services:** + +- Backend API: `http://localhost:8000` +- Frontend: `http://localhost:8080` +- Postgres: `localhost:5432` +- Redis: `localhost:6379` + This expects a working Docker environment and may require you to set DB URLs to point to the containerized Postgres service if one is defined in `docker-compose.yml`. ## Troubleshooting diff --git a/backend/Dockerfile b/backend/Dockerfile index 369536a..5c833fa 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -8,15 +8,26 @@ ENV PYTHONDONTWRITEBYTECODE=1 \ WORKDIR /app RUN apt-get update \ - && apt-get install -y --no-install-recommends build-essential libpq-dev \ + && apt-get install -y --no-install-recommends build-essential libpq-dev postgresql-client \ && rm -rf /var/lib/apt/lists/* COPY backend/requirements/base.txt ./backend/requirements/base.txt RUN pip install --upgrade pip \ && pip install -r backend/requirements/base.txt +COPY scripts ./scripts +COPY .env.example ./.env.example +COPY .env* ./ + COPY backend ./backend EXPOSE 8000 -CMD ["uvicorn", "backend.app.main:app", "--host", "0.0.0.0", "--port", "8000"] +# Initialize database with demo data if INIT_DEMO_DB is set +CMD ["sh", "-c", "\ + export PYTHONPATH=/app && \ + echo 'Waiting for database...' && \ + while ! pg_isready -h db -p 5432 -U railgame >/dev/null 2>&1; do sleep 1; done && \ + echo 'Database is ready!' && \ + if [ \"$INIT_DEMO_DB\" = \"true\" ]; then python scripts/init_demo_db.py; fi && \ + uvicorn backend.app.main:app --host 0.0.0.0 --port 8000"] diff --git a/docker-compose.yml b/docker-compose.yml index 4ccae08..503005f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,5 +1,3 @@ -version: "3.9" - services: db: build: @@ -27,6 +25,7 @@ services: DATABASE_URL: postgresql+psycopg://railgame:railgame@db:5432/railgame_dev TEST_DATABASE_URL: postgresql+psycopg://railgame:railgame@db:5432/railgame_test REDIS_URL: redis://redis:6379/0 + INIT_DEMO_DB: "true" depends_on: - db - redis diff --git a/scripts/init_demo_db.py b/scripts/init_demo_db.py index 923eb5c..3582597 100644 --- a/scripts/init_demo_db.py +++ b/scripts/init_demo_db.py @@ -32,6 +32,10 @@ except ImportError: def check_virtualenv(): """Check if we're running in a virtual environment.""" + # Skip virtualenv check in Docker containers + if os.getenv('INIT_DEMO_DB') == 'true': + return + if not hasattr(sys, 'real_prefix') and not (hasattr(sys, 'base_prefix') and sys.base_prefix != sys.prefix): print("ERROR: Virtual environment not activated. Run:") print(" .venv\\Scripts\\Activate.ps1 (PowerShell)")