Spaces:
Running

File size: 3,461 Bytes
727ec8d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
"""
Notification tables migration using psycopg2 (sync)
"""

import psycopg2
from urllib.parse import urlparse

# πŸ”΄ EDIT THIS: Your production Supabase URL
db_url = "postgresql://postgres.qmzkpahvztlbovkpzsij:AbRpHNxBrdbw42IH@aws-0-eu-central-1.pooler.supabase.com:5432/postgres"

# Parse URL
parsed = urlparse(db_url)

print("πŸ”„ Connecting to database...")
print(f"Host: {parsed.hostname}")

try:
    conn = psycopg2.connect(
        host=parsed.hostname,
        port=parsed.port or 5432,
        user=parsed.username,
        password=parsed.password,
        database=parsed.path[1:]
    )
    
    cursor = conn.cursor()
    print("βœ… Connected!")
    
    # Migration SQL
    migration_sql = """
    -- notification_settings table
    CREATE TABLE IF NOT EXISTS notification_settings (
        id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
        tenant_id UUID NOT NULL UNIQUE REFERENCES tenants(id) ON DELETE CASCADE,
        telegram_enabled BOOLEAN DEFAULT TRUE,
        telegram_chat_id VARCHAR(255),
        telegram_bot_token VARCHAR(255),
        dashboard_enabled BOOLEAN DEFAULT TRUE,
        email_enabled BOOLEAN DEFAULT FALSE,
        email_address VARCHAR(255),
        notify_hot_leads BOOLEAN DEFAULT TRUE,
        notify_inactive_leads BOOLEAN DEFAULT FALSE,
        notify_new_leads BOOLEAN DEFAULT FALSE,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
        updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );

    -- notifications table
    CREATE TABLE IF NOT EXISTS notifications (
        id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
        tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
        lead_id UUID NOT NULL REFERENCES leads(id) ON DELETE CASCADE,
        type VARCHAR(50) NOT NULL,
        title VARCHAR(255) NOT NULL,
        message TEXT NOT NULL,
        is_read BOOLEAN DEFAULT FALSE,
        read_at TIMESTAMP,
        sent_telegram BOOLEAN DEFAULT FALSE,
        sent_dashboard BOOLEAN DEFAULT TRUE,
        created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
    );

    -- Indexes
    CREATE INDEX IF NOT EXISTS idx_notification_settings_tenant_id ON notification_settings(tenant_id);
    CREATE INDEX IF NOT EXISTS idx_notifications_tenant_id ON notifications(tenant_id);
    CREATE INDEX IF NOT EXISTS idx_notifications_is_read ON notifications(is_read);
    CREATE INDEX IF NOT EXISTS idx_notifications_created_at ON notifications(created_at DESC);
    CREATE INDEX IF NOT EXISTS idx_notifications_type ON notifications(type);

    -- Default settings
    INSERT INTO notification_settings (tenant_id, telegram_enabled, dashboard_enabled, notify_hot_leads)
    SELECT id, TRUE, TRUE, TRUE
    FROM tenants
    WHERE id NOT IN (SELECT tenant_id FROM notification_settings);
    """
    
    print("πŸ”„ Running migration...")
    cursor.execute(migration_sql)
    conn.commit()
    
    print("\nβœ… βœ… βœ… MIGRATION SUCCESS! βœ… βœ… βœ…\n")
    print("πŸ“Š Tables created:")
    print("   - notification_settings")
    print("   - notifications")
    print("πŸ“Œ Indexes created (5)")
    print("🎯 Default settings inserted for all tenants")
    
    # Verify
    cursor.execute("SELECT COUNT(*) FROM notification_settings")
    count = cursor.fetchone()[0]
    print(f"\nπŸ“ˆ Total notification_settings rows: {count}")
    
    cursor.close()
    conn.close()
    
except Exception as e:
    print(f"\n❌ Error: {e}")
    import traceback
    traceback.print_exc()