-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDIAGNOSTIC_SQL_SCRIPTS.sql
More file actions
210 lines (168 loc) · 6.21 KB
/
DIAGNOSTIC_SQL_SCRIPTS.sql
File metadata and controls
210 lines (168 loc) · 6.21 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
-- =============================================================================
-- DIAGNOSTIC SQL SCRIPTS FOR SUPABASE DATABASE
-- =============================================================================
-- Run these scripts in Supabase SQL Editor to diagnose issues.
-- =============================================================================
-- ============================================================================
-- STEP 1: CHECK IF ALL REQUIRED TABLES EXIST
-- ============================================================================
-- This query lists all tables in the public schema
SELECT
table_name,
table_type
FROM information_schema.tables
WHERE table_schema = 'public'
ORDER BY table_name;
-- Expected tables for chat functionality:
-- - agent_chat_sessions
-- - agent_chat_events
-- - sys_user (or similar user table)
-- - agent_connectors (for Google Drive)
-- ============================================================================
-- STEP 2: CHECK agent_chat_sessions TABLE STRUCTURE
-- ============================================================================
SELECT
column_name,
data_type,
is_nullable,
column_default
FROM information_schema.columns
WHERE table_name = 'agent_chat_sessions'
ORDER BY ordinal_position;
-- ============================================================================
-- STEP 3: COUNT RECORDS IN KEY TABLES
-- ============================================================================
SELECT 'sys_user' as table_name, COUNT(*) as row_count FROM sys_user
UNION ALL
SELECT 'agent_chat_sessions', COUNT(*) FROM agent_chat_sessions
UNION ALL
SELECT 'agent_chat_events', COUNT(*) FROM agent_chat_events;
-- ============================================================================
-- STEP 4: VIEW ALL CHAT SESSIONS (MOST RECENT FIRST)
-- ============================================================================
SELECT
id,
uuid,
user_id,
name,
status,
agent_type,
message_count,
created_time,
updated_time,
deleted_at
FROM agent_chat_sessions
ORDER BY created_time DESC
LIMIT 20;
-- ============================================================================
-- STEP 5: VIEW YOUR SPECIFIC USER'S SESSIONS
-- ============================================================================
-- First, find your user_id by email or username
SELECT id, username, email, nickname FROM sys_user
WHERE email LIKE '%YOUR_EMAIL%'
OR username LIKE '%YOUR_USERNAME%'
LIMIT 5;
-- Then view sessions for that user (replace YOUR_USER_ID with actual ID)
-- Uncomment and modify the query below:
-- SELECT
-- uuid,
-- name,
-- status,
-- agent_type,
-- message_count,
-- created_time
-- FROM agent_chat_sessions
-- WHERE user_id = YOUR_USER_ID
-- AND deleted_at IS NULL
-- ORDER BY created_time DESC;
-- ============================================================================
-- STEP 6: CHECK IF agent_chat_sessions TABLE EXISTS (ALTERNATIVE)
-- ============================================================================
SELECT EXISTS (
SELECT FROM pg_tables
WHERE schemaname = 'public'
AND tablename = 'agent_chat_sessions'
) as table_exists;
-- ============================================================================
-- STEP 7: CHECK CONNECTORS TABLE (for Google Drive)
-- ============================================================================
SELECT
column_name,
data_type
FROM information_schema.columns
WHERE table_name = 'agent_connectors'
ORDER BY ordinal_position;
-- View all connectors
SELECT
id,
user_id,
connector_type,
token_expiry,
created_time
FROM agent_connectors
ORDER BY created_time DESC
LIMIT 10;
-- ============================================================================
-- STEP 8: CHECK IF SESSIONS ARE BEING CREATED PROPERLY
-- ============================================================================
-- This checks for any sessions created in the last 24 hours
SELECT
uuid,
user_id,
name,
status,
agent_type,
created_time
FROM agent_chat_sessions
WHERE created_time > NOW() - INTERVAL '24 hours'
ORDER BY created_time DESC;
-- ============================================================================
-- STEP 9: CHECK FOR ORPHANED/PROBLEMATIC SESSIONS
-- ============================================================================
-- Sessions without a valid user_id
SELECT
cs.uuid,
cs.user_id,
cs.name,
cs.created_time
FROM agent_chat_sessions cs
LEFT JOIN sys_user u ON cs.user_id = u.id
WHERE u.id IS NULL;
-- ============================================================================
-- STEP 10: CHECK USER TABLE STRUCTURE
-- ============================================================================
SELECT
column_name,
data_type,
is_nullable
FROM information_schema.columns
WHERE table_name = 'sys_user'
ORDER BY ordinal_position;
-- ============================================================================
-- STEP 11: DEBUG - Check a specific session UUID
-- ============================================================================
-- Replace 'YOUR_SESSION_UUID' with the actual UUID from your 404 error
-- SELECT * FROM agent_chat_sessions
-- WHERE uuid = 'YOUR_SESSION_UUID';
-- ============================================================================
-- STEP 12: Check if migrations have run properly
-- ============================================================================
SELECT version_num, installed_on
FROM alembic_version
ORDER BY installed_on DESC
LIMIT 5;
-- ============================================================================
-- QUICK SUMMARY QUERY - RUN THIS FIRST
-- ============================================================================
-- This gives you a quick overview of the database state
SELECT
'Tables' as category,
(SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = 'public')::text as count
UNION ALL
SELECT 'Users', COUNT(*)::text FROM sys_user
UNION ALL
SELECT 'Chat Sessions', COUNT(*)::text FROM agent_chat_sessions
UNION ALL
SELECT 'Chat Events', COUNT(*)::text FROM agent_chat_events
UNION ALL
SELECT 'Active Sessions', COUNT(*)::text FROM agent_chat_sessions WHERE status = 'active' AND deleted_at IS NULL;