-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupabase-setup.sql
More file actions
382 lines (345 loc) · 14.9 KB
/
supabase-setup.sql
File metadata and controls
382 lines (345 loc) · 14.9 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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
-- ============================================================
-- ZonePro Supabase Database Setup
-- Run these queries in your Supabase SQL Editor
-- (Dashboard > SQL Editor > New query)
-- ============================================================
-- 1. COURSES TABLE
-- Stores YouTube course/playlist progress per user
-- ============================================================
create table public.courses (
id bigint generated always as identity primary key,
user_id uuid not null references auth.users(id) on delete cascade,
local_id text not null,
title text not null default '',
url text not null default '',
video_id text not null default '',
current_seconds integer not null default 0,
duration integer not null default 0,
completed boolean not null default false,
last_watched timestamptz not null default now(),
playlist_id text,
playlist_index integer not null default 0,
playlist_progress jsonb not null default '{}'::jsonb,
playlist_metadata jsonb,
notes jsonb not null default '[]'::jsonb,
created_at timestamptz not null default now(),
unique (user_id, local_id)
);
-- 2. USER SETTINGS TABLE
-- Stores pomodoro config and app preferences per user
-- ============================================================
create table public.user_settings (
id bigint generated always as identity primary key,
user_id uuid not null references auth.users(id) on delete cascade unique,
pomodoro_work_time integer not null default 25,
pomodoro_break_time integer not null default 5,
pomodoro_total_sessions integer not null default 4,
auto_music_pause boolean not null default true,
created_at timestamptz not null default now()
);
-- 3. MUSIC TRACKS TABLE
-- Stores the user's music playlist (YouTube tracks)
-- ============================================================
create table public.music_tracks (
id bigint generated always as identity primary key,
user_id uuid not null references auth.users(id) on delete cascade,
local_id text not null,
title text not null default '',
video_id text not null default '',
duration integer not null default 0,
sort_order integer not null default 0,
created_at timestamptz not null default now(),
unique (user_id, local_id)
);
-- ============================================================
-- ROW LEVEL SECURITY (RLS)
-- Users can only access their own data
-- ============================================================
-- Enable RLS on all tables
alter table public.courses enable row level security;
alter table public.user_settings enable row level security;
alter table public.music_tracks enable row level security;
-- COURSES policies
create policy "Users can view their own courses"
on public.courses for select
using (auth.uid() = user_id);
create policy "Users can insert their own courses"
on public.courses for insert
with check (auth.uid() = user_id);
create policy "Users can update their own courses"
on public.courses for update
using (auth.uid() = user_id);
create policy "Users can delete their own courses"
on public.courses for delete
using (auth.uid() = user_id);
-- USER SETTINGS policies
create policy "Users can view their own settings"
on public.user_settings for select
using (auth.uid() = user_id);
create policy "Users can insert their own settings"
on public.user_settings for insert
with check (auth.uid() = user_id);
create policy "Users can update their own settings"
on public.user_settings for update
using (auth.uid() = user_id);
create policy "Users can delete their own settings"
on public.user_settings for delete
using (auth.uid() = user_id);
-- MUSIC TRACKS policies
create policy "Users can view their own music tracks"
on public.music_tracks for select
using (auth.uid() = user_id);
create policy "Users can insert their own music tracks"
on public.music_tracks for insert
with check (auth.uid() = user_id);
create policy "Users can update their own music tracks"
on public.music_tracks for update
using (auth.uid() = user_id);
create policy "Users can delete their own music tracks"
on public.music_tracks for delete
using (auth.uid() = user_id);
-- ============================================================
-- INDEXES for query performance
-- ============================================================
create index idx_courses_user_id on public.courses(user_id);
create index idx_courses_last_watched on public.courses(user_id, last_watched desc);
create index idx_music_tracks_user_id on public.music_tracks(user_id);
create index idx_music_tracks_sort on public.music_tracks(user_id, sort_order asc);
-- ============================================================
-- 4. TASKS TABLE
-- Stores task management data per user
-- ============================================================
create table public.tasks (
id bigint generated always as identity primary key,
user_id uuid not null references auth.users(id) on delete cascade,
local_id text not null,
title text not null default '',
description text,
status text not null default 'todo',
priority text not null default 'none',
labels jsonb not null default '[]'::jsonb,
due_date text,
parent_id text,
depends_on jsonb not null default '[]'::jsonb,
linked_note_ids jsonb not null default '[]'::jsonb,
linked_focus_session_ids jsonb not null default '[]'::jsonb,
recurrence jsonb,
estimated_minutes integer,
actual_minutes integer,
sort_order integer not null default 0,
created_at text not null,
completed_at text,
updated_at text not null,
unique (user_id, local_id)
);
-- 5. FOCUS SESSIONS TABLE
-- Stores focus/pomodoro session logs per user
-- ============================================================
create table public.focus_sessions (
id bigint generated always as identity primary key,
user_id uuid not null references auth.users(id) on delete cascade,
local_id text not null,
task_id text,
type text not null default 'pomodoro',
started_at text not null,
ended_at text,
planned_minutes integer not null default 0,
actual_minutes integer not null default 0,
completed boolean not null default false,
notes text,
date text not null,
unique (user_id, local_id)
);
-- 6. HABITS TABLE
-- Stores habit definitions per user
-- ============================================================
create table public.habits (
id bigint generated always as identity primary key,
user_id uuid not null references auth.users(id) on delete cascade,
local_id text not null,
title text not null default '',
description text,
frequency text not null default 'daily',
target_days jsonb,
target_count integer not null default 1,
color text not null default '#3b82f6',
icon text,
created_at text not null,
archived_at text,
sort_order integer not null default 0,
unique (user_id, local_id)
);
-- 7. HABIT LOGS TABLE
-- Stores daily habit completion logs per user
-- ============================================================
create table public.habit_logs (
id bigint generated always as identity primary key,
user_id uuid not null references auth.users(id) on delete cascade,
local_id text not null,
habit_id text not null,
date text not null,
count integer not null default 0,
note text,
unique (user_id, local_id)
);
-- 8. NOTES TABLE
-- Stores user notes with tags and links
-- ============================================================
create table public.notes (
id bigint generated always as identity primary key,
user_id uuid not null references auth.users(id) on delete cascade,
local_id text not null,
title text not null default '',
content text not null default '',
tags jsonb not null default '[]'::jsonb,
is_pinned boolean not null default false,
is_archived boolean not null default false,
linked_task_ids jsonb not null default '[]'::jsonb,
linked_course_id text,
created_at text not null,
updated_at text not null,
unique (user_id, local_id)
);
-- 9. GOALS TABLE
-- Stores goals with embedded milestones per user
-- ============================================================
create table public.goals (
id bigint generated always as identity primary key,
user_id uuid not null references auth.users(id) on delete cascade,
local_id text not null,
title text not null default '',
description text,
target_date text,
milestones jsonb not null default '[]'::jsonb,
status text not null default 'active',
created_at text not null,
updated_at text not null,
unique (user_id, local_id)
);
-- 10. DAILY PRIORITIES TABLE
-- Stores daily priority task ordering per user
-- ============================================================
create table public.daily_priorities (
id bigint generated always as identity primary key,
user_id uuid not null references auth.users(id) on delete cascade,
date text not null,
task_ids jsonb not null default '[]'::jsonb,
unique (user_id, date)
);
-- 11. TIME BLOCKS TABLE
-- Stores time block scheduling per user
-- ============================================================
create table public.time_blocks (
id bigint generated always as identity primary key,
user_id uuid not null references auth.users(id) on delete cascade,
local_id text not null,
date text not null,
start_hour integer not null default 0,
start_minute integer not null default 0,
duration_minutes integer not null default 30,
title text not null default '',
task_id text,
color text,
unique (user_id, local_id)
);
-- ============================================================
-- ROW LEVEL SECURITY (RLS) for new tables
-- ============================================================
alter table public.tasks enable row level security;
alter table public.focus_sessions enable row level security;
alter table public.habits enable row level security;
alter table public.habit_logs enable row level security;
alter table public.notes enable row level security;
alter table public.goals enable row level security;
alter table public.daily_priorities enable row level security;
alter table public.time_blocks enable row level security;
-- TASKS policies
create policy "Users can view their own tasks"
on public.tasks for select using (auth.uid() = user_id);
create policy "Users can insert their own tasks"
on public.tasks for insert with check (auth.uid() = user_id);
create policy "Users can update their own tasks"
on public.tasks for update using (auth.uid() = user_id);
create policy "Users can delete their own tasks"
on public.tasks for delete using (auth.uid() = user_id);
-- FOCUS SESSIONS policies
create policy "Users can view their own focus sessions"
on public.focus_sessions for select using (auth.uid() = user_id);
create policy "Users can insert their own focus sessions"
on public.focus_sessions for insert with check (auth.uid() = user_id);
create policy "Users can update their own focus sessions"
on public.focus_sessions for update using (auth.uid() = user_id);
create policy "Users can delete their own focus sessions"
on public.focus_sessions for delete using (auth.uid() = user_id);
-- HABITS policies
create policy "Users can view their own habits"
on public.habits for select using (auth.uid() = user_id);
create policy "Users can insert their own habits"
on public.habits for insert with check (auth.uid() = user_id);
create policy "Users can update their own habits"
on public.habits for update using (auth.uid() = user_id);
create policy "Users can delete their own habits"
on public.habits for delete using (auth.uid() = user_id);
-- HABIT LOGS policies
create policy "Users can view their own habit logs"
on public.habit_logs for select using (auth.uid() = user_id);
create policy "Users can insert their own habit logs"
on public.habit_logs for insert with check (auth.uid() = user_id);
create policy "Users can update their own habit logs"
on public.habit_logs for update using (auth.uid() = user_id);
create policy "Users can delete their own habit logs"
on public.habit_logs for delete using (auth.uid() = user_id);
-- NOTES policies
create policy "Users can view their own notes"
on public.notes for select using (auth.uid() = user_id);
create policy "Users can insert their own notes"
on public.notes for insert with check (auth.uid() = user_id);
create policy "Users can update their own notes"
on public.notes for update using (auth.uid() = user_id);
create policy "Users can delete their own notes"
on public.notes for delete using (auth.uid() = user_id);
-- GOALS policies
create policy "Users can view their own goals"
on public.goals for select using (auth.uid() = user_id);
create policy "Users can insert their own goals"
on public.goals for insert with check (auth.uid() = user_id);
create policy "Users can update their own goals"
on public.goals for update using (auth.uid() = user_id);
create policy "Users can delete their own goals"
on public.goals for delete using (auth.uid() = user_id);
-- DAILY PRIORITIES policies
create policy "Users can view their own daily priorities"
on public.daily_priorities for select using (auth.uid() = user_id);
create policy "Users can insert their own daily priorities"
on public.daily_priorities for insert with check (auth.uid() = user_id);
create policy "Users can update their own daily priorities"
on public.daily_priorities for update using (auth.uid() = user_id);
create policy "Users can delete their own daily priorities"
on public.daily_priorities for delete using (auth.uid() = user_id);
-- TIME BLOCKS policies
create policy "Users can view their own time blocks"
on public.time_blocks for select using (auth.uid() = user_id);
create policy "Users can insert their own time blocks"
on public.time_blocks for insert with check (auth.uid() = user_id);
create policy "Users can update their own time blocks"
on public.time_blocks for update using (auth.uid() = user_id);
create policy "Users can delete their own time blocks"
on public.time_blocks for delete using (auth.uid() = user_id);
-- ============================================================
-- INDEXES for new tables
-- ============================================================
create index idx_tasks_user_id on public.tasks(user_id);
create index idx_tasks_status on public.tasks(user_id, status);
create index idx_tasks_due_date on public.tasks(user_id, due_date);
create index idx_focus_sessions_user_id on public.focus_sessions(user_id);
create index idx_focus_sessions_date on public.focus_sessions(user_id, date);
create index idx_habits_user_id on public.habits(user_id);
create index idx_habit_logs_user_id on public.habit_logs(user_id);
create index idx_habit_logs_date on public.habit_logs(user_id, date);
create index idx_habit_logs_habit on public.habit_logs(user_id, habit_id);
create index idx_notes_user_id on public.notes(user_id);
create index idx_goals_user_id on public.goals(user_id);
create index idx_daily_priorities_user_id on public.daily_priorities(user_id);
create index idx_daily_priorities_date on public.daily_priorities(user_id, date);
create index idx_time_blocks_user_id on public.time_blocks(user_id);
create index idx_time_blocks_date on public.time_blocks(user_id, date);