From 3780b3cf2fea803e544d8cb86d8f8fa5fc97edc8 Mon Sep 17 00:00:00 2001 From: James Pattinson Date: Thu, 4 Dec 2025 18:29:09 +0000 Subject: [PATCH] External DB init changes --- backend/Dockerfile | 7 ++++--- backend/entrypoint.sh | 26 +++++++++++++++++++++++++- backend/seed_data.py | 28 ++++++++++++++++------------ docker-compose.prod.yml | 2 ++ 4 files changed, 47 insertions(+), 16 deletions(-) diff --git a/backend/Dockerfile b/backend/Dockerfile index 34e2bec..28fd1e9 100644 --- a/backend/Dockerfile +++ b/backend/Dockerfile @@ -19,11 +19,12 @@ RUN pip install --no-cache-dir -r requirements.txt # Copy application code COPY . . -# Make entrypoint executable -RUN chmod +x /app/entrypoint.sh +# Make scripts executable +RUN chmod +x /app/entrypoint.sh && \ + chmod +x /app/seed_data.py # Expose port EXPOSE 8000 # Use entrypoint script -ENTRYPOINT ["/app/entrypoint.sh"] \ No newline at end of file +ENTRYPOINT ["/bin/bash", "/app/entrypoint.sh"] \ No newline at end of file diff --git a/backend/entrypoint.sh b/backend/entrypoint.sh index 7f0c132..3c86422 100644 --- a/backend/entrypoint.sh +++ b/backend/entrypoint.sh @@ -2,7 +2,7 @@ # Docker entrypoint script for PPR API # Handles database migrations and data seeding automatically -set -e +# Note: We don't use 'set -e' here because we need to handle specific exit codes from Python scripts echo "=========================================" echo "PPR API Container Starting" @@ -139,6 +139,30 @@ elif [ $DB_STATE -eq 0 ]; then else echo "✓ Database is up to date" fi + + # Check if reference data needs to be loaded + python3 << EOF +from sqlalchemy import create_engine, text +from app.core.config import settings + +engine = create_engine(settings.database_url) +with engine.connect() as conn: + airport_count = conn.execute(text("SELECT COUNT(*) FROM airports")).fetchone()[0] + aircraft_count = conn.execute(text("SELECT COUNT(*) FROM aircraft")).fetchone()[0] + + if airport_count == 0 or aircraft_count == 0: + print("Reference data missing - will load") + exit(10) + else: + print(f"Reference data exists (airports: {airport_count}, aircraft: {aircraft_count})") + exit(0) +EOF + + if [ $? -eq 10 ]; then + echo "Loading reference data..." + python3 /app/seed_data.py + echo "✓ Reference data loaded" + fi else echo "✗ Database check failed" exit 1 diff --git a/backend/seed_data.py b/backend/seed_data.py index 655f3df..a478aba 100644 --- a/backend/seed_data.py +++ b/backend/seed_data.py @@ -29,13 +29,15 @@ def load_airports(db, csv_path): batch = [] with open(csv_path, 'r', encoding='utf-8') as f: - reader = csv.DictReader(f) + reader = csv.reader(f) # CSV has no headers for row in reader: + if len(row) < 4: + continue # Skip invalid rows airport = Airport( - icao=row['icao'], - iata=row.get('iata') if row.get('iata') else None, - name=row['name'], - country=row['country'] + icao=row[0].strip('"'), + iata=row[1].strip('"') if row[1].strip('"') else None, + name=row[2].strip('"'), + country=row[3].strip('"') ) batch.append(airport) loaded += 1 @@ -73,15 +75,17 @@ def load_aircraft(db, csv_path): batch = [] with open(csv_path, 'r', encoding='utf-8') as f: - reader = csv.DictReader(f) + reader = csv.reader(f) # CSV has no headers for row in reader: + if len(row) < 6: + continue # Skip invalid rows aircraft = Aircraft( - icao24=row.get('icao24') if row.get('icao24') else None, - registration=row.get('registration') if row.get('registration') else None, - manufacturer_icao=row.get('manufacturericao') if row.get('manufacturericao') else None, - type_code=row.get('typecode') if row.get('typecode') else None, - manufacturer_name=row.get('manufacturername') if row.get('manufacturername') else None, - model=row.get('model') if row.get('model') else None + icao24=row[0].strip('"') if row[0].strip('"') else None, + registration=row[1].strip('"') if row[1].strip('"') else None, + manufacturer_icao=row[2].strip('"') if row[2].strip('"') else None, + type_code=row[3].strip('"') if row[3].strip('"') else None, + manufacturer_name=row[4].strip('"') if row[4].strip('"') else None, + model=row[5].strip('"') if row[5].strip('"') else None ) batch.append(aircraft) loaded += 1 diff --git a/docker-compose.prod.yml b/docker-compose.prod.yml index 8d0cdf5..ebcb26a 100644 --- a/docker-compose.prod.yml +++ b/docker-compose.prod.yml @@ -36,6 +36,8 @@ services: - ./db-init:/db-init:ro # Mount CSV data for seeding networks: - app_network + extra_hosts: + - "host.docker.internal:host-gateway" deploy: resources: limits: