-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigration_fix_everything.sql
More file actions
36 lines (32 loc) · 1.46 KB
/
Copy pathmigration_fix_everything.sql
File metadata and controls
36 lines (32 loc) · 1.46 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
-- 1. Ensure last_seen column exists in participants
ALTER TABLE public.participants ADD COLUMN IF NOT EXISTS last_seen TIMESTAMPTZ DEFAULT NOW();
-- 2. Ensure Realtime is enabled for all critical tables
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_publication_tables WHERE pubname = 'supabase_realtime' AND tablename = 'chat_messages') THEN
ALTER PUBLICATION supabase_realtime ADD TABLE chat_messages;
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_publication_tables WHERE pubname = 'supabase_realtime' AND tablename = 'participants') THEN
ALTER PUBLICATION supabase_realtime ADD TABLE participants;
END IF;
END $$;
-- 3. Fix Chat RLS Policies (Drop and Recreate to be safe)
DROP POLICY IF EXISTS "Participants can view messages" ON chat_messages;
CREATE POLICY "Participants can view messages" ON chat_messages FOR SELECT USING (
EXISTS (
SELECT 1 FROM participants
WHERE participants.room_id = chat_messages.room_id
AND participants.user_id = auth.uid()
)
);
DROP POLICY IF EXISTS "Participants can insert messages" ON chat_messages;
CREATE POLICY "Participants can insert messages" ON chat_messages FOR INSERT WITH CHECK (
EXISTS (
SELECT 1 FROM participants
WHERE participants.room_id = chat_messages.room_id
AND participants.user_id = auth.uid()
)
);
-- 4. Clean up any stuck/duplicate participants just in case
-- (Optional: removing this to avoid deleting valid users, but "last_seen" update will fix them)
-- TRUNCATE TABLE participants;