20260203
This commit is contained in:
43
doc_mall/consumer/sql/add_footprints_table.sql
Normal file
43
doc_mall/consumer/sql/add_footprints_table.sql
Normal file
@@ -0,0 +1,43 @@
|
||||
-- 1. 创建足迹表
|
||||
CREATE TABLE IF NOT EXISTS ml_user_footprints (
|
||||
id UUID DEFAULT uuid_generate_v4() PRIMARY KEY,
|
||||
user_id UUID NOT NULL DEFAULT auth.uid(),
|
||||
product_id UUID NOT NULL, -- 如果 ml_products 表存在,可以改为: product_id UUID NOT NULL REFERENCES ml_products(id) ON DELETE CASCADE
|
||||
created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
|
||||
UNIQUE(user_id, product_id) -- 复合唯一索引,防止同一商品重复记录
|
||||
);
|
||||
|
||||
-- 2. 添加表注释
|
||||
COMMENT ON TABLE ml_user_footprints IS '用户浏览足迹表';
|
||||
COMMENT ON COLUMN ml_user_footprints.user_id IS '用户ID';
|
||||
COMMENT ON COLUMN ml_user_footprints.product_id IS '商品ID';
|
||||
COMMENT ON COLUMN ml_user_footprints.updated_at IS '最后访问时间';
|
||||
|
||||
-- 3. 启用行级安全策略 (RLS)
|
||||
ALTER TABLE ml_user_footprints ENABLE ROW LEVEL SECURITY;
|
||||
|
||||
-- 4. 添加安全策略 (增删改查仅限本人)
|
||||
-- 查看策略
|
||||
CREATE POLICY "Users can view their own footprints"
|
||||
ON ml_user_footprints FOR SELECT
|
||||
USING (auth.uid() = user_id);
|
||||
|
||||
-- 插入策略
|
||||
CREATE POLICY "Users can insert their own footprints"
|
||||
ON ml_user_footprints FOR INSERT
|
||||
WITH CHECK (auth.uid() = user_id);
|
||||
|
||||
-- 更新策略
|
||||
CREATE POLICY "Users can update their own footprints"
|
||||
ON ml_user_footprints FOR UPDATE
|
||||
USING (auth.uid() = user_id);
|
||||
|
||||
-- 删除策略
|
||||
CREATE POLICY "Users can delete their own footprints"
|
||||
ON ml_user_footprints FOR DELETE
|
||||
USING (auth.uid() = user_id);
|
||||
|
||||
-- 5. 建立索引以优化查询速度
|
||||
CREATE INDEX IF NOT EXISTS idx_footprints_user_updated
|
||||
ON ml_user_footprints (user_id, updated_at DESC);
|
||||
Reference in New Issue
Block a user