File size: 2,887 Bytes
09801ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/bin/bash

# DataVision AI — Production Startup script for Hugging Face Spaces & Containers

echo "🚀 Starting DataVision AI Platform (Fresh Deployment)..."

cd /app/backend

# Initialize fresh database migrations and seed default roles/admin user if DATABASE_URL is present
if [ -n "$DATABASE_URL" ]; then
    echo "📦 [1/2] Initializing fresh database schema..."
    
    # Auto-healing check for legacy v1 schema or orphan tables
    python -c "
import asyncio
from sqlalchemy import text
from database.db import engine

async def auto_clean_legacy_schema():
    try:
        async with engine.begin() as conn:
            # Check if old v1 'profiles' table exists
            res = await conn.execute(text(\"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'profiles')\"))
            has_profiles = res.scalar()
            if has_profiles:
                print('⚠️ Legacy v1 database schema detected. Performing automated fresh schema reset...')
                await conn.execute(text('DROP SCHEMA public CASCADE'))
                await conn.execute(text('CREATE SCHEMA public'))
                await conn.execute(text('GRANT ALL ON SCHEMA public TO public'))
    except Exception as e:
        print(f'DB verification notice: {e}')

asyncio.run(auto_clean_legacy_schema())
" 2>/dev/null || true

    # Heal alembic version if tables exist but version was dropped
    python -c "
import asyncio
from sqlalchemy import text
from database.db import engine

async def heal_alembic():
    try:
        async with engine.begin() as conn:
            res = await conn.execute(text(\"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'audit_logs')\"))
            if res.scalar():
                res_ver = await conn.execute(text(\"SELECT EXISTS (SELECT FROM information_schema.tables WHERE table_name = 'alembic_version')\"))
                if not res_ver.scalar():
                    print('Healing missing alembic_version...')
                    await conn.execute(text('CREATE TABLE alembic_version (version_num VARCHAR(32) NOT NULL, CONSTRAINT alembic_version_pkc PRIMARY KEY (version_num))'))
                    await conn.execute(text(\"INSERT INTO alembic_version (version_num) VALUES ('008079ed9ce8')\"))
    except Exception as e:
        print(f'Alembic heal error: {e}')

asyncio.run(heal_alembic())
" 2>/dev/null || true

    # Run migrations to create any missing tables
    alembic upgrade head || echo "⚠️ Migration warning: check DB logs"

    echo "🌱 [2/2] Seeding fresh system roles, permissions, and super admin user..."
    python -m app.database.seed || echo "⚠️ Seed warning: check DB logs"
else
    echo "ℹ️ DATABASE_URL not set. Running with default configuration."
fi

echo "🟢 Starting FastAPI Uvicorn server via python main.py..."
export PORT=${PORT:-7860}
exec python main.py