#!/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