Spaces:
Sleeping
Sleeping
| """Database initialization script""" | |
| import asyncio | |
| from database import init_db, engine | |
| from db_models import Base | |
| from rich.console import Console | |
| console = Console() | |
| async def create_tables(): | |
| """Create all database tables""" | |
| console.print("\n[bold cyan]Initializing Trinka Database...[/bold cyan]") | |
| try: | |
| # Create all tables | |
| async with engine.begin() as conn: | |
| await conn.run_sync(Base.metadata.create_all) | |
| console.print("[bold green]β Database tables created successfully![/bold green]") | |
| console.print("\n[bold]Tables created:[/bold]") | |
| console.print(" β’ conversations") | |
| console.print(" β’ messages") | |
| console.print(" β’ agent_actions") | |
| console.print(" β’ alert_groups") | |
| console.print(" β’ users (optional)") | |
| console.print(" β’ documents (optional)") | |
| except Exception as e: | |
| console.print(f"[bold red]β Error creating tables: {e}[/bold red]") | |
| raise | |
| finally: | |
| await engine.dispose() | |
| async def drop_tables(): | |
| """Drop all database tables (use with caution!)""" | |
| console.print("\n[bold yellow]β Dropping all database tables...[/bold yellow]") | |
| try: | |
| async with engine.begin() as conn: | |
| await conn.run_sync(Base.metadata.drop_all) | |
| console.print("[bold green]β All tables dropped successfully![/bold green]") | |
| except Exception as e: | |
| console.print(f"[bold red]β Error dropping tables: {e}[/bold red]") | |
| raise | |
| finally: | |
| await engine.dispose() | |
| if __name__ == "__main__": | |
| import sys | |
| if len(sys.argv) > 1 and sys.argv[1] == "--drop": | |
| console.print("[bold red]WARNING: This will delete all data![/bold red]") | |
| response = input("Are you sure you want to drop all tables? (yes/no): ") | |
| if response.lower() == "yes": | |
| asyncio.run(drop_tables()) | |
| else: | |
| console.print("[yellow]Operation cancelled.[/yellow]") | |
| else: | |
| asyncio.run(create_tables()) | |