-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-setup.sql
More file actions
39 lines (32 loc) · 1.63 KB
/
supabase-setup.sql
File metadata and controls
39 lines (32 loc) · 1.63 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
-- 创建生成内容表
CREATE TABLE IF NOT EXISTS generated_content (
id TEXT PRIMARY KEY,
content TEXT NOT NULL,
type TEXT NOT NULL CHECK (type IN ('cover', 'card', 'diagram')),
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
template_id TEXT NOT NULL,
template_name TEXT NOT NULL,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
token_usage JSONB
);
-- 创建索引以提高查询性能
CREATE INDEX IF NOT EXISTS idx_generated_content_user_id ON generated_content(user_id);
CREATE INDEX IF NOT EXISTS idx_generated_content_created_at ON generated_content(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_generated_content_type ON generated_content(type);
-- 启用行级安全策略 (RLS)
ALTER TABLE generated_content ENABLE ROW LEVEL SECURITY;
-- 创建策略:用户只能查看自己的内容
CREATE POLICY "Users can view own content" ON generated_content
FOR SELECT USING (auth.uid() = user_id OR user_id IS NULL);
-- 创建策略:用户可以插入自己的内容
CREATE POLICY "Users can insert own content" ON generated_content
FOR INSERT WITH CHECK (auth.uid() = user_id OR user_id IS NULL);
-- 创建策略:用户可以更新自己的内容
CREATE POLICY "Users can update own content" ON generated_content
FOR UPDATE USING (auth.uid() = user_id);
-- 创建策略:用户可以删除自己的内容
CREATE POLICY "Users can delete own content" ON generated_content
FOR DELETE USING (auth.uid() = user_id);
-- 创建策略:允许匿名用户访问无用户ID的内容(向后兼容)
CREATE POLICY "Allow anonymous access to public content" ON generated_content
FOR ALL USING (user_id IS NULL);