File size: 4,322 Bytes
611e2c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
"""
Helper script to create or update .env file with Supabase credentials.
"""

import os
from pathlib import Path

def main():
    print("=" * 70)
    print("Supabase .env Setup Helper")
    print("=" * 70)
    print()
    
    env_file = Path(".env")
    env_example = Path("env.example")
    
    # Check if .env already exists
    if env_file.exists():
        print("⚠️  .env file already exists!")
        response = input("   Do you want to update it? (y/n): ").strip().lower()
        if response != 'y':
            print("   Skipping. Edit .env manually if needed.")
            return
        print()
    
    # Read existing .env if it exists
    existing_vars = {}
    if env_file.exists():
        with open(env_file, 'r') as f:
            for line in f:
                line = line.strip()
                if line and not line.startswith('#') and '=' in line:
                    key, value = line.split('=', 1)
                    existing_vars[key.strip()] = value.strip()
    
    print("Enter your Supabase credentials:")
    print("(You can find these at: https://app.supabase.com → Your Project → Settings → API)")
    print()
    
    # Get Supabase URL
    current_url = existing_vars.get('SUPABASE_URL', '')
    if current_url:
        print(f"Current SUPABASE_URL: {current_url[:50]}...")
        response = input("Keep current? (y/n): ").strip().lower()
        if response == 'y':
            supabase_url = current_url
        else:
            supabase_url = input("Enter SUPABASE_URL (https://xxxxx.supabase.co): ").strip()
    else:
        supabase_url = input("Enter SUPABASE_URL (https://xxxxx.supabase.co): ").strip()
    
    # Get Supabase Service Key
    current_key = existing_vars.get('SUPABASE_SERVICE_KEY', '')
    if current_key:
        print(f"Current SUPABASE_SERVICE_KEY: {current_key[:20]}...")
        response = input("Keep current? (y/n): ").strip().lower()
        if response == 'y':
            supabase_key = current_key
        else:
            supabase_key = input("Enter SUPABASE_SERVICE_KEY (service_role key): ").strip()
    else:
        supabase_key = input("Enter SUPABASE_SERVICE_KEY (service_role key): ").strip()
    
    # Validate
    if not supabase_url.startswith('https://'):
        print("⚠️  Warning: SUPABASE_URL should start with https://")
    if not supabase_key.startswith('eyJ'):
        print("⚠️  Warning: SUPABASE_SERVICE_KEY should start with 'eyJ' (JWT token)")
    
    print()
    print("📝 Creating/updating .env file...")
    
    # Read env.example as template
    lines = []
    if env_example.exists():
        with open(env_example, 'r') as f:
            lines = f.readlines()
    else:
        # Create basic template
        lines = [
            "# IntegraChat Environment Variables\n",
            "# Supabase Configuration\n",
            "SUPABASE_URL=\n",
            "SUPABASE_SERVICE_KEY=\n",
        ]
    
    # Update or add Supabase variables
    updated_lines = []
    url_found = False
    key_found = False
    
    for line in lines:
        if line.startswith('SUPABASE_URL='):
            updated_lines.append(f'SUPABASE_URL={supabase_url}\n')
            url_found = True
        elif line.startswith('SUPABASE_SERVICE_KEY='):
            updated_lines.append(f'SUPABASE_SERVICE_KEY={supabase_key}\n')
            key_found = True
        else:
            updated_lines.append(line)
    
    # Add if not found
    if not url_found:
        updated_lines.append(f'SUPABASE_URL={supabase_url}\n')
    if not key_found:
        updated_lines.append(f'SUPABASE_SERVICE_KEY={supabase_key}\n')
    
    # Write .env file
    with open(env_file, 'w') as f:
        f.writelines(updated_lines)
    
    print(f"✅ .env file created/updated at: {env_file.absolute()}")
    print()
    print("Next steps:")
    print("1. Make sure your Supabase project is active (not paused)")
    print("2. Create the tables in Supabase:")
    print("   - Run supabase_admin_rules_table.sql in SQL Editor")
    print("   - Run supabase_analytics_tables.sql in SQL Editor")
    print("3. Test the connection:")
    print("   python check_supabase_rules.py")
    print("4. Run the migration:")
    print("   python migrate_sqlite_to_supabase.py")

if __name__ == "__main__":
    main()