-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
95 lines (76 loc) · 3.82 KB
/
schema.sql
File metadata and controls
95 lines (76 loc) · 3.82 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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
-- ============================================================
-- CodeBeats — Supabase Database Schema
-- ============================================================
-- Run this ONCE in your Supabase SQL Editor:
-- https://supabase.com/dashboard/project/_/sql
-- ============================================================
-- 1. Tables ───────────────────────────────────────────────────
CREATE TABLE IF NOT EXISTS public.projects (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
remote_url TEXT UNIQUE,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS public.heartbeats (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL,
project_id UUID REFERENCES public.projects(id) ON DELETE SET NULL,
language TEXT,
file_path TEXT,
branch TEXT,
editor TEXT,
os TEXT,
created_at TIMESTAMPTZ DEFAULT now()
);
CREATE TABLE IF NOT EXISTS public.daily_stats (
user_id UUID NOT NULL,
date DATE NOT NULL,
total_seconds INTEGER DEFAULT 0,
primary_language TEXT,
updated_at TIMESTAMPTZ DEFAULT now(),
PRIMARY KEY (user_id, date)
);
CREATE TABLE IF NOT EXISTS public.status_messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL,
message TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT now()
);
-- 2. Indexes (query performance) ─────────────────────────────
CREATE INDEX IF NOT EXISTS idx_heartbeats_user_created
ON public.heartbeats (user_id, created_at DESC);
CREATE INDEX IF NOT EXISTS idx_heartbeats_project
ON public.heartbeats (project_id);
CREATE INDEX IF NOT EXISTS idx_daily_stats_user_date
ON public.daily_stats (user_id, date DESC);
CREATE INDEX IF NOT EXISTS idx_status_messages_user_created
ON public.status_messages (user_id, created_at DESC);
-- 3. Row Level Security ──────────────────────────────────────
-- Since this is a PRIVATE tracker, we allow all operations.
-- If you want per-user isolation later, replace these with
-- policies that check auth.uid().
ALTER TABLE public.projects ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.heartbeats ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.daily_stats ENABLE ROW LEVEL SECURITY;
ALTER TABLE public.status_messages ENABLE ROW LEVEL SECURITY;
-- Drop existing policies if re-running this script
DO $$ BEGIN
DROP POLICY IF EXISTS "codebeats_all" ON public.projects;
DROP POLICY IF EXISTS "codebeats_all" ON public.heartbeats;
DROP POLICY IF EXISTS "codebeats_all" ON public.daily_stats;
DROP POLICY IF EXISTS "codebeats_all" ON public.status_messages;
END $$;
CREATE POLICY "codebeats_all" ON public.projects
FOR ALL USING (true) WITH CHECK (true);
CREATE POLICY "codebeats_all" ON public.heartbeats
FOR ALL USING (true) WITH CHECK (true);
CREATE POLICY "codebeats_all" ON public.daily_stats
FOR ALL USING (true) WITH CHECK (true);
CREATE POLICY "codebeats_all" ON public.status_messages
FOR ALL USING (true) WITH CHECK (true);
-- 4. Permissions ─────────────────────────────────────────────
GRANT USAGE ON SCHEMA public TO anon, authenticated, service_role;
GRANT ALL ON TABLE public.projects TO anon, authenticated, service_role;
GRANT ALL ON TABLE public.heartbeats TO anon, authenticated, service_role;
GRANT ALL ON TABLE public.daily_stats TO anon, authenticated, service_role;
GRANT ALL ON TABLE public.status_messages TO anon, authenticated, service_role;