Repository navigation
Expand file tree
/
Copy pathinit.sql
More file actions
49 lines (42 loc) · 2.03 KB
/
Copy pathinit.sql
File metadata and controls
49 lines (42 loc) · 2.03 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
-- ============================================================
-- 「蛇途求生」排行榜 · Supabase 初始化脚本
-- 使用方法:supabase.com 注册免费账号 → 创建项目(区域建议 Singapore/Tokyo)
-- → 左侧 SQL Editor → 新建查询 → 整体粘贴执行 → Run
-- 执行后:建表 scores + 公开读写 RLS + 启用 Realtime 实时推送
-- ============================================================
-- 1. 建表:排行榜记录(score 服务端约束 0~1000000,防刷兜底)
create table if not exists public.scores (
id bigint generated always as identity primary key,
nickname text not null default '',
score int not null default 0 check (score >= 0 and score <= 1000000),
duration double precision not null default 0,
device_id text not null default '',
created_at timestamptz not null default now()
);
-- 常用排序索引(记录量小时可省略,不影响功能)
create index if not exists idx_scores_rank
on public.scores (score desc, duration desc);
-- 2. 行级安全(RLS):任何人可读、任何人可写
-- 游戏无登录体系,写入校验由客户端(分数上限/每日条数)+ 上面字段约束兜底
alter table public.scores enable row level security;
drop policy if exists "public read scores" on public.scores;
create policy "public read scores" on public.scores
for select using (true);
drop policy if exists "public insert scores" on public.scores;
create policy "public insert scores" on public.scores
for insert with check (true);
-- 3. 启用 Realtime:把 scores 表加入实时发布(支持新纪录 INSERT 推送)
do $$
begin
if not exists (
select 1 from pg_publication_tables
where pubname = 'supabase_realtime'
and schemaname = 'public'
and tablename = 'scores'
) then
alter publication supabase_realtime add table public.scores;
end if;
end $$;
-- 验证(可选):下面应各返回 1 行/无报错
-- select count(*) from public.scores;
-- select * from pg_publication_tables where tablename = 'scores';