File size: 2,363 Bytes
ba2fc46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import asyncio
from sqlalchemy import text
from backend.src.db.session import engine

async def upgrade_database():
    print("⚙️ Updating Database Schema (Without Deleting Data)...")
    
    async with engine.begin() as conn:
        # --- 1. Users Table mein 'api_key' add karna ---
        try:
            print("   -> Adding 'api_key' column to users...")
            await conn.execute(text("ALTER TABLE users ADD COLUMN api_key VARCHAR;"))
        except Exception as e:
            print("      (Skipped: Column shayad pehle se hai)")

        # --- 2. Users Table mein 'allowed_domains' add karna ---
        try:
            print("   -> Adding 'allowed_domains' column to users...")
            await conn.execute(text("ALTER TABLE users ADD COLUMN allowed_domains VARCHAR DEFAULT '*';"))
        except Exception as e:
            print("      (Skipped: Column shayad pehle se hai)")

        # --- 3. Integrations Table mein 'profile_description' add karna ---
        try:
            print("   -> Adding 'profile_description' column to user_integrations...")
            await conn.execute(text("ALTER TABLE user_integrations ADD COLUMN profile_description TEXT;"))
        except Exception as e:
            print("      (Skipped: Column shayad pehle se hai)")

    print("✅ Database Update Complete! Aapka purana data safe hai.")

    # --- 4. Purane Users ke liye API Key Generate karna ---
    # Kyunki purane users ki API Key NULL hogi, unhein nayi key deni padegi.
    from backend.src.utils.auth import generate_api_key
    from sqlalchemy.future import select
    from backend.src.models.user import User
    from backend.src.db.session import AsyncSessionLocal

    print("🔑 Generating API Keys for existing users...")
    async with AsyncSessionLocal() as db:
        result = await db.execute(select(User))
        users = result.scalars().all()
        
        count = 0
        for user in users:
            if not user.api_key: # Agar key nahi hai
                user.api_key = generate_api_key()
                user.allowed_domains = "*"
                db.add(user)
                count += 1
                print(f"   -> Key generated for: {user.email}")
        
        await db.commit()
        print(f"✅ {count} Users updated with new API Keys.")

if __name__ == "__main__":
    asyncio.run(upgrade_database())