-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_tables_python.py
More file actions
108 lines (90 loc) · 4 KB
/
create_tables_python.py
File metadata and controls
108 lines (90 loc) · 4 KB
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
#!/usr/bin/env python3
"""
Create the missing tables directly using Supabase Python client
"""
import os
from dotenv import load_dotenv
from supabase import create_client
load_dotenv()
def create_missing_tables():
"""Create missing tables using direct SQL execution"""
# Get Supabase credentials
url = os.getenv('SUPABASE_URL')
key = os.getenv('SUPABASE_SERVICE_ROLE_KEY')
if not url or not key:
print("❌ Missing Supabase credentials")
return False
try:
# Create Supabase client
client = create_client(url, key)
print("📋 Creating missing tables...")
# SQL statements to create missing tables
sql_statements = [
# Create training_documents table
"""
CREATE TABLE IF NOT EXISTS training_documents (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
filename TEXT NOT NULL,
file_path TEXT NOT NULL,
file_type TEXT NOT NULL,
file_size INTEGER NOT NULL,
subject TEXT,
status TEXT DEFAULT 'uploaded',
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
""",
# Create user_knowledge table
"""
CREATE TABLE IF NOT EXISTS user_knowledge (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
tenant_id UUID NOT NULL REFERENCES tenants(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
subject TEXT NOT NULL,
content TEXT NOT NULL,
source_type TEXT NOT NULL,
source_id UUID,
confidence_score FLOAT DEFAULT 0.8,
tags TEXT[],
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW()
);
""",
# Enable RLS
"ALTER TABLE training_documents ENABLE ROW LEVEL SECURITY;",
"ALTER TABLE user_knowledge ENABLE ROW LEVEL SECURITY;",
# Create indexes
"CREATE INDEX IF NOT EXISTS idx_training_documents_tenant_id ON training_documents(tenant_id);",
"CREATE INDEX IF NOT EXISTS idx_user_knowledge_tenant_id ON user_knowledge(tenant_id);"
]
# Execute each SQL statement
for i, sql in enumerate(sql_statements, 1):
try:
print(f"Executing statement {i}...")
# Use the postgrest client to execute SQL
result = client.postgrest.rpc('exec', {'sql': sql.strip()})
print(f"✅ Statement {i} executed successfully")
except Exception as e:
print(f"⚠️ Statement {i} result: {e}")
# Continue with other statements even if one fails
# Test if tables were created
print("\n🧪 Testing table creation...")
try:
result = client.table('training_documents').select('id').limit(1).execute()
print("✅ training_documents table created successfully")
except Exception as e:
print(f"❌ training_documents table still missing: {e}")
try:
result = client.table('user_knowledge').select('id').limit(1).execute()
print("✅ user_knowledge table created successfully")
except Exception as e:
print(f"❌ user_knowledge table still missing: {e}")
print("\n🎯 Table creation completed!")
return True
except Exception as e:
print(f"❌ Error creating tables: {e}")
return False
if __name__ == "__main__":
create_missing_tables()