-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebug-users-table.sql
More file actions
66 lines (60 loc) · 1.64 KB
/
debug-users-table.sql
File metadata and controls
66 lines (60 loc) · 1.64 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
-- Debug and fix users table issues
-- Run this in Supabase SQL Editor
-- 1. Check what's in the users table
SELECT
id,
email,
full_name,
role,
created_at,
updated_at
FROM users
ORDER BY created_at DESC;
-- 2. Check auth.users to see what users exist
SELECT
id,
email,
raw_user_meta_data,
user_metadata,
created_at
FROM auth.users
ORDER BY created_at DESC;
-- 3. Check if the trigger is working by looking for users in auth but not in public.users
SELECT
auth.id,
auth.email,
auth.created_at,
CASE WHEN public_users.id IS NULL THEN 'MISSING FROM PUBLIC.USERS' ELSE 'EXISTS' END as status
FROM auth.users auth
LEFT JOIN public.users public_users ON auth.id = public_users.id
WHERE auth.email != 'superuser@example.com'
ORDER BY auth.created_at DESC;
-- 4. Fix: Insert missing users into public.users table
INSERT INTO public.users (id, email, full_name, role, created_at, updated_at)
SELECT
auth.id,
auth.email,
COALESCE(auth.raw_user_meta_data->>'full_name', auth.email) as full_name,
COALESCE(auth.raw_user_meta_data->>'role', 'user') as role,
auth.created_at,
auth.created_at as updated_at
FROM auth.users auth
LEFT JOIN public.users public_users ON auth.id = public_users.id
WHERE
public_users.id IS NULL
AND auth.email != 'superuser@example.com'
ON CONFLICT (id) DO NOTHING;
-- 5. Update existing users to have correct role if missing
UPDATE public.users
SET role = COALESCE(role, 'user')
WHERE role IS NULL OR role = '';
-- 6. Verify the fix
SELECT
id,
email,
full_name,
role,
created_at
FROM users
WHERE role = 'user'
ORDER BY created_at DESC;