Spaces:
Paused
Paused
Download scripts/setup_db_cloud.py from 3SCError404/nexus: direct link, hf CLI and curl.
- Browser
- Download file 2.92 kB
-
https://huggingface.co/spaces/3SCError404/nexus/resolve/main/scripts/setup_db_cloud.py
- Command line
-
hf download hf://spaces/3SCError404/nexus/scripts/setup_db_cloud.py
-
curl -L -o setup_db_cloud.py https://huggingface.co/spaces/3SCError404/nexus/resolve/main/scripts/setup_db_cloud.py
2.92 kB
| import asyncio | |
| import sys | |
| import os | |
| from sqlalchemy import text | |
| # Add project root to path | |
| sys.path.append(os.getcwd()) | |
| from core.config import get_settings | |
| from core.database import engine, Base, async_session_factory | |
| from core.security import get_password_hash | |
| from models.workspace import Workspace | |
| from models.user import User, UserRole | |
| from models import * # Ensure all models are imported for Base.metadata | |
| async def setup_cloud_db(): | |
| """ | |
| Initializes a remote/cloud database (e.g. Supabase or Neon). | |
| 1. Creates pgvector extension. | |
| 2. Generates all tables. | |
| 3. Seeds initial Workspace and Admin. | |
| """ | |
| settings = get_settings() | |
| print("\n--- NEXUS: CLOUD DATABASE INITIALIZATION ---") | |
| print(f"Target: {settings.DATABASE_URL.split('@')[-1]}") # Log host only for safety | |
| async with engine.begin() as conn: | |
| # 1. Create Vector Extension | |
| print("[1/4] Enabling pgvector extension...") | |
| await conn.execute(text("CREATE EXTENSION IF NOT EXISTS vector")) | |
| # 2. Create Tables | |
| print("[2/4] Generating schema from metadata...") | |
| await conn.run_sync(Base.metadata.create_all) | |
| # 3. Seed Data | |
| print("[3/4] Seeding initial workspace and administrator...") | |
| async with async_session_factory() as session: | |
| # Check if workspace exists | |
| from sqlalchemy import select | |
| result = await session.execute(select(Workspace).limit(1)) | |
| existing_workspace = result.scalars().first() | |
| if not existing_workspace: | |
| print("Creating 'Snowball HQ' default workspace...") | |
| new_ws = Workspace( | |
| name="Snowball HQ", | |
| branding={"primary": "#1E40AF", "logo": "/logo.png"} | |
| ) | |
| session.add(new_ws) | |
| await session.flush() # Get the ID | |
| # Create Admin User | |
| print("Creating 'Nexus Admin' credentials...") | |
| admin_user = User( | |
| workspace_id=new_ws.id, | |
| email="admin@nexus.ai", | |
| full_name="Nexus System Admin", | |
| role=UserRole.SC_ADMIN, | |
| hashed_password=get_password_hash("NexusAdmin2024!"), # Default - should be changed | |
| is_active=True | |
| ) | |
| session.add(admin_user) | |
| await session.commit() | |
| print(f"✅ Initial Workspace ID: {new_ws.id}") | |
| print("✅ Admin Account created: admin@nexus.ai") | |
| else: | |
| print("Skipping seed: Workspace already exists.") | |
| print("[4/4] Cloud Database initialization COMPLETE.") | |
| print("\n--- NEXT STEPS ---") | |
| print("1. Update your .env with this DATABASE_URL.") | |
| print("2. Deploy your Docker image to Hugging Face Spaces.") | |
| print("3. Point your Vercel frontend to the Hugging Face App URL.\n") | |
| if __name__ == "__main__": | |
| asyncio.run(setup_cloud_db()) | |