File size: 3,725 Bytes
4198b8d
 
 
 
 
 
 
 
 
0a2ed98
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4198b8d
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
set -e

echo "=== Lumea Health Platform - Starting ==="

# Run database migrations (best-effort)
echo "Running database migrations..."
python -m alembic upgrade head 2>&1 || echo "Warning: Alembic migrations failed (tables may already exist or DB not ready). Continuing..."

# ---------------------------------------------------------------------------
# Schema patch: ensure all columns the app expects actually exist in the DB.
# Alembic may fail silently (e.g. pgcrypto extension, SSL issues), leaving
# the schema out of date. This idempotent patch uses ADD COLUMN IF NOT EXISTS.
# ---------------------------------------------------------------------------
echo "Running schema patch (ADD COLUMN IF NOT EXISTS)..."
python -c "
import os, sys
sys.path.insert(0, '.')

db_url = os.environ.get('DATABASE_URL', '')
if not db_url:
    print('No DATABASE_URL set — skipping schema patch')
    sys.exit(0)

# Convert asyncpg URL to sync psycopg2 URL
sync_url = db_url.replace('postgresql+asyncpg://', 'postgresql://').replace('ssl=require', 'sslmode=require')

try:
    import sqlalchemy
    engine = sqlalchemy.create_engine(sync_url, pool_pre_ping=True)
    with engine.connect() as conn:
        # -- reports table: columns added by model but possibly missing from DB --
        patches = [
            'ALTER TABLE reports ADD COLUMN IF NOT EXISTS category VARCHAR(50)',
            'ALTER TABLE reports ADD COLUMN IF NOT EXISTS doc_type VARCHAR(50)',
            'ALTER TABLE reports ADD COLUMN IF NOT EXISTS classification_confidence FLOAT',
            'ALTER TABLE reports ADD COLUMN IF NOT EXISTS classification_rules_matched JSONB',
            'ALTER TABLE reports ADD COLUMN IF NOT EXISTS raw_text TEXT',
            'ALTER TABLE reports ADD COLUMN IF NOT EXISTS extraction_method VARCHAR',
            'ALTER TABLE reports ADD COLUMN IF NOT EXISTS extraction_source VARCHAR(20)',
            'ALTER TABLE reports ADD COLUMN IF NOT EXISTS page_stats JSON',
            'ALTER TABLE reports ADD COLUMN IF NOT EXISTS extracted_data JSON',
            'ALTER TABLE reports ADD COLUMN IF NOT EXISTS extraction_confidence FLOAT',
            'ALTER TABLE reports ADD COLUMN IF NOT EXISTS error_message TEXT',
            # -- observations table --
            'ALTER TABLE observations ADD COLUMN IF NOT EXISTS source VARCHAR(20)',
            'ALTER TABLE observations ADD COLUMN IF NOT EXISTS confidence FLOAT',
            'ALTER TABLE observations ADD COLUMN IF NOT EXISTS user_corrected BOOLEAN DEFAULT FALSE',
            'ALTER TABLE observations ADD COLUMN IF NOT EXISTS display_name VARCHAR',
        ]
        for sql in patches:
            try:
                conn.execute(sqlalchemy.text(sql))
            except Exception as col_err:
                print(f'  patch skipped: {col_err}')
        conn.commit()
        print('Schema patch applied successfully')
except Exception as e:
    print(f'Warning: Schema patch failed: {e}. Continuing...')
" 2>&1 || echo "Warning: Schema patch skipped. Continuing..."

# Seed generic medicine catalog (Jan Aushadhi + generics) — idempotent, skips if already seeded
echo "Seeding generic medicine catalog..."
python -c "
import asyncio, os, sys
sys.path.insert(0, '.')
try:
    from scripts.seed_generic_catalog import seed_database
    db_url = os.environ.get('DATABASE_URL', 'postgresql+asyncpg://ggw:ggwpassword@localhost:5432/ggwdb')
    asyncio.run(seed_database(db_url))
except Exception as e:
    print(f'Warning: Medicine catalog seeding failed: {e}. Continuing...')
" 2>&1 || echo "Warning: Medicine catalog seeding skipped. Continuing..."

echo "Starting FastAPI server on port 7860..."
exec uvicorn app.main:app --host 0.0.0.0 --port 7860