External DB init changes

This commit is contained in:
James Pattinson
2025-12-04 18:29:09 +00:00
parent b6ad496cf0
commit 3780b3cf2f
4 changed files with 47 additions and 16 deletions

View File

@@ -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"]
ENTRYPOINT ["/bin/bash", "/app/entrypoint.sh"]

View File

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

View File

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

View File

@@ -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: