-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmigration-notifications.sql
More file actions
34 lines (27 loc) · 1.31 KB
/
Copy pathmigration-notifications.sql
File metadata and controls
34 lines (27 loc) · 1.31 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
-- Milestone Notifications System
-- 1. Create notifications table
CREATE TABLE IF NOT EXISTS notifications (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
user_id UUID REFERENCES users(id) ON DELETE CASCADE,
type TEXT NOT NULL, -- 'badge', 'level', 'verification', 'milestone'
title TEXT NOT NULL,
content TEXT NOT NULL,
link TEXT, -- Optional link to navigate to
is_read BOOLEAN DEFAULT FALSE,
created_at TIMESTAMPTZ DEFAULT now()
);
-- 2. Enable Realtime
ALTER PUBLICATION supabase_realtime ADD TABLE notifications;
-- 3. Enable RLS
ALTER TABLE notifications ENABLE ROW LEVEL SECURITY;
-- 4. RLS Policies
CREATE POLICY "Users can view their own notifications" ON notifications
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can update their own notifications" ON notifications
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "System can insert notifications" ON notifications
FOR INSERT WITH CHECK (TRUE); -- Usually handled by service role or specific triggers
-- 5. Indexes for performance
CREATE INDEX IF NOT EXISTS idx_notifications_user_id ON notifications(user_id);
CREATE INDEX IF NOT EXISTS idx_notifications_is_read ON notifications(is_read) WHERE is_read = FALSE;
CREATE INDEX IF NOT EXISTS idx_notifications_created_at ON notifications(created_at DESC);