Spaces:
Paused

File size: 2,915 Bytes
5eb4857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4405ca9
5eb4857
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4405ca9
5eb4857
 
 
 
 
 
 
 
 
 
 
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
71
72
73
74
75
76
77
78
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())