File size: 1,564 Bytes
3b5d2e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6927d56
 
3b5d2e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4cab845
6927d56
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
#!/bin/bash

# Database initialization script for Knowledge Assistant RAG
# This script runs database migrations and starts the FastAPI server

set -e

echo "Starting database initialization..."

# Create data directory if it doesn't exist
mkdir -p /app/data

# Set proper permissions for data directory
chmod 755 /app/data

# Validate required environment variables
if [ -z "$DATABASE_URL" ]; then
    echo "Warning: DATABASE_URL not set, using default SQLite path"
    export DATABASE_URL="sqlite+aiosqlite:///./data/knowledge_assistant.db"
fi

if [ -z "$JWT_SECRET" ]; then
    echo "Error: JWT_SECRET environment variable is required"
    exit 1
fi

echo "Database URL: $DATABASE_URL"

# Try to run database migrations
echo "Running database migrations..."
cd /app

# Always use python -m for robustness
ALEMBIC_CMD="python -m alembic"

# Check if alembic.ini exists
if [ ! -f "alembic.ini" ]; then
    echo "Error: alembic.ini not found. Database migrations cannot proceed."
    echo "Skipping migrations and starting server..."
else
    # Try to run migrations with timeout
    echo "Attempting to run migrations..."
    if timeout 60 $ALEMBIC_CMD upgrade head; then
        echo "Database migrations completed successfully."
    else
        echo "Database migrations failed or timed out. Continuing anyway..."
        echo "The application will create tables automatically if needed."
    fi
fi

# Start the FastAPI server
echo "Starting FastAPI server..."
PORT=${PORT:-8000}
exec python -m uvicorn src.main:app --host 0.0.0.0 --port $PORT --log-level info