-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathschema.sql
More file actions
699 lines (558 loc) · 22.7 KB
/
schema.sql
File metadata and controls
699 lines (558 loc) · 22.7 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
-- Cekatan Assessment Platform Database Schema
-- Spaced Repetition System with SM-2 Algorithm
-- Enable UUID extension
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Decks table
CREATE TABLE decks (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Cards table (supports both flashcards and MCQs)
CREATE TABLE cards (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
deck_id UUID NOT NULL REFERENCES decks(id) ON DELETE CASCADE,
-- Card type discriminator
card_type TEXT DEFAULT 'flashcard' CHECK (card_type IN ('flashcard', 'mcq')),
-- Flashcard fields
front TEXT NOT NULL,
back TEXT NOT NULL,
-- MCQ fields (nullable for flashcards)
stem TEXT,
options JSONB,
correct_index INTEGER,
explanation TEXT,
-- Shared fields
image_url TEXT,
interval INTEGER DEFAULT 0,
ease_factor REAL DEFAULT 2.5,
next_review TIMESTAMPTZ DEFAULT NOW(),
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Migration: Add MCQ fields to existing cards table
-- ALTER TABLE cards ADD COLUMN card_type TEXT DEFAULT 'flashcard' CHECK (card_type IN ('flashcard', 'mcq'));
-- ALTER TABLE cards ADD COLUMN stem TEXT;
-- ALTER TABLE cards ADD COLUMN options JSONB;
-- ALTER TABLE cards ADD COLUMN correct_index INTEGER;
-- ALTER TABLE cards ADD COLUMN explanation TEXT;
-- RLS Policies for decks
ALTER TABLE decks ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can view own decks" ON decks
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own decks" ON decks
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own decks" ON decks
FOR UPDATE USING (auth.uid() = user_id);
CREATE POLICY "Users can delete own decks" ON decks
FOR DELETE USING (auth.uid() = user_id);
-- RLS Policies for cards (via deck ownership)
ALTER TABLE cards ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can view cards in own decks" ON cards
FOR SELECT USING (
EXISTS (SELECT 1 FROM decks WHERE decks.id = cards.deck_id AND decks.user_id = auth.uid())
);
CREATE POLICY "Users can insert cards in own decks" ON cards
FOR INSERT WITH CHECK (
EXISTS (SELECT 1 FROM decks WHERE decks.id = cards.deck_id AND decks.user_id = auth.uid())
);
CREATE POLICY "Users can update cards in own decks" ON cards
FOR UPDATE USING (
EXISTS (SELECT 1 FROM decks WHERE decks.id = cards.deck_id AND decks.user_id = auth.uid())
);
CREATE POLICY "Users can delete cards in own decks" ON cards
FOR DELETE USING (
EXISTS (SELECT 1 FROM decks WHERE decks.id = cards.deck_id AND decks.user_id = auth.uid())
);
-- Performance indexes
CREATE INDEX idx_decks_user_id ON decks(user_id);
CREATE INDEX idx_cards_deck_id ON cards(deck_id);
CREATE INDEX idx_cards_next_review ON cards(next_review);
-- ============================================
-- Gamification Tables (V1)
-- ============================================
-- User stats table for streak tracking and gamification
CREATE TABLE user_stats (
user_id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
last_study_date DATE,
current_streak INTEGER DEFAULT 0,
longest_streak INTEGER DEFAULT 0,
total_reviews INTEGER DEFAULT 0,
daily_goal INTEGER DEFAULT 20,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- RLS Policies for user_stats
ALTER TABLE user_stats ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can view own stats" ON user_stats
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own stats" ON user_stats
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own stats" ON user_stats
FOR UPDATE USING (auth.uid() = user_id);
-- Index on user_id (already PK, but explicit for clarity in queries)
CREATE INDEX idx_user_stats_user_id ON user_stats(user_id);
-- Study logs table for daily activity tracking (heatmap)
CREATE TABLE study_logs (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
study_date DATE NOT NULL,
cards_reviewed INTEGER DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(user_id, study_date)
);
-- RLS Policies for study_logs
ALTER TABLE study_logs ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can view own logs" ON study_logs
FOR SELECT USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own logs" ON study_logs
FOR INSERT WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can update own logs" ON study_logs
FOR UPDATE USING (auth.uid() = user_id);
-- Index on (user_id, study_date) for efficient queries
CREATE INDEX idx_study_logs_user_date ON study_logs(user_id, study_date);
-- ============================================
-- Course Hierarchy Tables (V2)
-- ============================================
-- Courses table
CREATE TABLE courses (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
description TEXT,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- RLS Policies for courses
ALTER TABLE courses ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage own courses" ON courses
FOR ALL USING (auth.uid() = user_id);
-- Index on user_id
CREATE INDEX idx_courses_user_id ON courses(user_id);
-- Units table
CREATE TABLE units (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
course_id UUID NOT NULL REFERENCES courses(id) ON DELETE CASCADE,
title TEXT NOT NULL,
order_index INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- RLS Policies for units (via course ownership)
ALTER TABLE units ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage units in own courses" ON units
FOR ALL USING (
EXISTS (SELECT 1 FROM courses WHERE courses.id = units.course_id AND courses.user_id = auth.uid())
);
-- Index on course_id
CREATE INDEX idx_units_course_id ON units(course_id);
-- Lessons table
CREATE TABLE lessons (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
unit_id UUID NOT NULL REFERENCES units(id) ON DELETE CASCADE,
title TEXT NOT NULL,
order_index INTEGER NOT NULL DEFAULT 0,
target_item_count INTEGER DEFAULT 10,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- RLS Policies for lessons (via unit/course ownership)
ALTER TABLE lessons ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage lessons in own courses" ON lessons
FOR ALL USING (
EXISTS (
SELECT 1 FROM units
JOIN courses ON courses.id = units.course_id
WHERE units.id = lessons.unit_id AND courses.user_id = auth.uid()
)
);
-- Index on unit_id
CREATE INDEX idx_lessons_unit_id ON lessons(unit_id);
-- Lesson items table
CREATE TABLE lesson_items (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
lesson_id UUID NOT NULL REFERENCES lessons(id) ON DELETE CASCADE,
item_type TEXT NOT NULL CHECK (item_type IN ('mcq', 'card')),
item_id UUID NOT NULL,
order_index INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- RLS Policies for lesson_items (via lesson/unit/course ownership)
ALTER TABLE lesson_items ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage lesson_items in own courses" ON lesson_items
FOR ALL USING (
EXISTS (
SELECT 1 FROM lessons
JOIN units ON units.id = lessons.unit_id
JOIN courses ON courses.id = units.course_id
WHERE lessons.id = lesson_items.lesson_id AND courses.user_id = auth.uid()
)
);
-- Index on lesson_id
CREATE INDEX idx_lesson_items_lesson_id ON lesson_items(lesson_id);
-- Lesson progress table
CREATE TABLE lesson_progress (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
lesson_id UUID NOT NULL REFERENCES lessons(id) ON DELETE CASCADE,
last_completed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
best_score INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(user_id, lesson_id)
);
-- RLS Policies for lesson_progress (user ownership)
ALTER TABLE lesson_progress ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage own lesson_progress" ON lesson_progress
FOR ALL USING (auth.uid() = user_id);
-- Index on (user_id, lesson_id)
CREATE INDEX idx_lesson_progress_user_lesson ON lesson_progress(user_id, lesson_id);
-- ============================================
-- Source Document Tables (V2 - Bulk Import)
-- ============================================
-- Sources table for PDF/document tracking
CREATE TABLE sources (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
type TEXT NOT NULL DEFAULT 'pdf_book',
file_url TEXT NOT NULL,
metadata JSONB,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- RLS Policies for sources (user ownership)
ALTER TABLE sources ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage own sources" ON sources
FOR ALL USING (auth.uid() = user_id);
-- Index on user_id
CREATE INDEX idx_sources_user_id ON sources(user_id);
-- Deck sources join table (links decks to sources)
CREATE TABLE deck_sources (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
deck_id UUID NOT NULL REFERENCES decks(id) ON DELETE CASCADE,
source_id UUID NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(deck_id, source_id)
);
-- RLS Policies for deck_sources (via deck ownership)
ALTER TABLE deck_sources ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage deck_sources for own decks" ON deck_sources
FOR ALL USING (
EXISTS (SELECT 1 FROM decks WHERE decks.id = deck_sources.deck_id AND decks.user_id = auth.uid())
);
-- Index on deck_id
CREATE INDEX idx_deck_sources_deck_id ON deck_sources(deck_id);
-- ============================================
-- Tagging System Tables (V5)
-- ============================================
-- Tags table for card organization
CREATE TABLE tags (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
color TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(user_id, name)
);
-- RLS Policies for tags (user ownership)
ALTER TABLE tags ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage own tags" ON tags
FOR ALL USING (auth.uid() = user_id);
-- Index on user_id
CREATE INDEX idx_tags_user_id ON tags(user_id);
-- V6.1: Case-insensitive unique index for tag deduplication
-- Prevents 'Anatomy' vs 'anatomy' duplicates per user
CREATE UNIQUE INDEX IF NOT EXISTS tags_user_id_lower_name_idx
ON tags (user_id, LOWER(name));
-- Card tags join table (links cards to tags)
CREATE TABLE card_tags (
card_id UUID NOT NULL REFERENCES cards(id) ON DELETE CASCADE,
tag_id UUID NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ DEFAULT NOW(),
PRIMARY KEY (card_id, tag_id)
);
-- RLS Policies for card_tags (via card ownership)
ALTER TABLE card_tags ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage card_tags for own cards" ON card_tags
FOR ALL USING (
EXISTS (
SELECT 1 FROM cards
JOIN decks ON decks.id = cards.deck_id
WHERE cards.id = card_tags.card_id AND decks.user_id = auth.uid()
)
);
-- Indexes for efficient filtering
CREATE INDEX idx_card_tags_card_id ON card_tags(card_id);
CREATE INDEX idx_card_tags_tag_id ON card_tags(tag_id);
-- ============================================
-- MIGRATION GUIDE: V2 Tables
-- ============================================
--
-- If you're getting "Could not find the table 'public.courses'" error,
-- it means the V2 tables haven't been created in your Supabase database.
--
-- STEP-BY-STEP INSTRUCTIONS:
--
-- 1. Open your Supabase Dashboard
-- 2. Go to SQL Editor
-- 3. Copy and run the following SQL blocks IN ORDER:
--
-- ============================================
-- BLOCK 1: Course Hierarchy Tables
-- ============================================
/*
-- Courses table
CREATE TABLE IF NOT EXISTS courses (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
description TEXT,
created_at TIMESTAMPTZ DEFAULT NOW()
);
ALTER TABLE courses ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage own courses" ON courses
FOR ALL USING (auth.uid() = user_id);
CREATE INDEX IF NOT EXISTS idx_courses_user_id ON courses(user_id);
-- Units table
CREATE TABLE IF NOT EXISTS units (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
course_id UUID NOT NULL REFERENCES courses(id) ON DELETE CASCADE,
title TEXT NOT NULL,
order_index INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW()
);
ALTER TABLE units ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage units in own courses" ON units
FOR ALL USING (
EXISTS (SELECT 1 FROM courses WHERE courses.id = units.course_id AND courses.user_id = auth.uid())
);
CREATE INDEX IF NOT EXISTS idx_units_course_id ON units(course_id);
-- Lessons table
CREATE TABLE IF NOT EXISTS lessons (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
unit_id UUID NOT NULL REFERENCES units(id) ON DELETE CASCADE,
title TEXT NOT NULL,
order_index INTEGER NOT NULL DEFAULT 0,
target_item_count INTEGER DEFAULT 10,
created_at TIMESTAMPTZ DEFAULT NOW()
);
ALTER TABLE lessons ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage lessons in own courses" ON lessons
FOR ALL USING (
EXISTS (
SELECT 1 FROM units
JOIN courses ON courses.id = units.course_id
WHERE units.id = lessons.unit_id AND courses.user_id = auth.uid()
)
);
CREATE INDEX IF NOT EXISTS idx_lessons_unit_id ON lessons(unit_id);
-- Lesson items table
CREATE TABLE IF NOT EXISTS lesson_items (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
lesson_id UUID NOT NULL REFERENCES lessons(id) ON DELETE CASCADE,
item_type TEXT NOT NULL CHECK (item_type IN ('mcq', 'card')),
item_id UUID NOT NULL,
order_index INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW()
);
ALTER TABLE lesson_items ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage lesson_items in own courses" ON lesson_items
FOR ALL USING (
EXISTS (
SELECT 1 FROM lessons
JOIN units ON units.id = lessons.unit_id
JOIN courses ON courses.id = units.course_id
WHERE lessons.id = lesson_items.lesson_id AND courses.user_id = auth.uid()
)
);
CREATE INDEX IF NOT EXISTS idx_lesson_items_lesson_id ON lesson_items(lesson_id);
-- Lesson progress table
CREATE TABLE IF NOT EXISTS lesson_progress (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
lesson_id UUID NOT NULL REFERENCES lessons(id) ON DELETE CASCADE,
last_completed_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
best_score INTEGER NOT NULL DEFAULT 0,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(user_id, lesson_id)
);
ALTER TABLE lesson_progress ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage own lesson_progress" ON lesson_progress
FOR ALL USING (auth.uid() = user_id);
CREATE INDEX IF NOT EXISTS idx_lesson_progress_user_lesson ON lesson_progress(user_id, lesson_id);
*/
-- ============================================
-- BLOCK 2: Source Document Tables
-- ============================================
/*
-- Sources table
CREATE TABLE IF NOT EXISTS sources (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
title TEXT NOT NULL,
type TEXT NOT NULL DEFAULT 'pdf_book',
file_url TEXT NOT NULL,
metadata JSONB,
created_at TIMESTAMPTZ DEFAULT NOW()
);
ALTER TABLE sources ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage own sources" ON sources
FOR ALL USING (auth.uid() = user_id);
CREATE INDEX IF NOT EXISTS idx_sources_user_id ON sources(user_id);
-- Deck sources join table
CREATE TABLE IF NOT EXISTS deck_sources (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
deck_id UUID NOT NULL REFERENCES decks(id) ON DELETE CASCADE,
source_id UUID NOT NULL REFERENCES sources(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(deck_id, source_id)
);
ALTER TABLE deck_sources ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage deck_sources for own decks" ON deck_sources
FOR ALL USING (
EXISTS (SELECT 1 FROM decks WHERE decks.id = deck_sources.deck_id AND decks.user_id = auth.uid())
);
CREATE INDEX IF NOT EXISTS idx_deck_sources_deck_id ON deck_sources(deck_id);
*/
-- ============================================
-- HOW TO RUN:
-- ============================================
-- 1. Copy BLOCK 1 (everything between the /* and */ markers)
-- 2. Paste into Supabase SQL Editor and click "Run"
-- 3. Copy BLOCK 2 and run it the same way
-- 4. Refresh your app - the error should be gone!
-- ============================================
-- ============================================
-- V6.4: Shared Library Tables
-- ============================================
-- Deck Templates (Shared Content Layer)
CREATE TABLE deck_templates (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
title TEXT NOT NULL,
description TEXT,
visibility TEXT NOT NULL DEFAULT 'private' CHECK (visibility IN ('private', 'public')),
author_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
legacy_id UUID UNIQUE,
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_deck_templates_legacy_id ON deck_templates(legacy_id);
CREATE INDEX idx_deck_templates_author_id ON deck_templates(author_id);
ALTER TABLE deck_templates ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Authors can manage own deck_templates" ON deck_templates
FOR ALL USING (auth.uid() = author_id);
CREATE POLICY "Public deck_templates readable by all" ON deck_templates
FOR SELECT USING (visibility = 'public');
-- Card Templates (Shared Content Layer)
CREATE TABLE card_templates (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
deck_template_id UUID NOT NULL REFERENCES deck_templates(id) ON DELETE CASCADE,
stem TEXT NOT NULL,
options JSONB NOT NULL,
correct_index INTEGER NOT NULL,
explanation TEXT,
source_meta JSONB,
legacy_id UUID UNIQUE,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX idx_card_templates_legacy_id ON card_templates(legacy_id);
CREATE INDEX idx_card_templates_deck_template_id ON card_templates(deck_template_id);
ALTER TABLE card_templates ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Authors can manage card_templates in own decks" ON card_templates
FOR ALL USING (
EXISTS (
SELECT 1 FROM deck_templates
WHERE deck_templates.id = card_templates.deck_template_id
AND deck_templates.author_id = auth.uid()
)
);
CREATE POLICY "Public card_templates readable by all" ON card_templates
FOR SELECT USING (
EXISTS (
SELECT 1 FROM deck_templates
WHERE deck_templates.id = card_templates.deck_template_id
AND deck_templates.visibility = 'public'
)
);
-- Card Template Tags (Join Table)
CREATE TABLE card_template_tags (
card_template_id UUID NOT NULL REFERENCES card_templates(id) ON DELETE CASCADE,
tag_id UUID NOT NULL REFERENCES tags(id) ON DELETE CASCADE,
created_at TIMESTAMPTZ DEFAULT NOW(),
PRIMARY KEY (card_template_id, tag_id)
);
CREATE INDEX idx_card_template_tags_card_template_id ON card_template_tags(card_template_id);
CREATE INDEX idx_card_template_tags_tag_id ON card_template_tags(tag_id);
ALTER TABLE card_template_tags ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Authors can manage card_template_tags in own decks" ON card_template_tags
FOR ALL USING (
EXISTS (
SELECT 1 FROM card_templates
JOIN deck_templates ON deck_templates.id = card_templates.deck_template_id
WHERE card_templates.id = card_template_tags.card_template_id
AND deck_templates.author_id = auth.uid()
)
);
CREATE POLICY "Public card_template_tags readable by all" ON card_template_tags
FOR SELECT USING (
EXISTS (
SELECT 1 FROM card_templates
JOIN deck_templates ON deck_templates.id = card_templates.deck_template_id
WHERE card_templates.id = card_template_tags.card_template_id
AND deck_templates.visibility = 'public'
)
);
-- User Decks (Progress Layer - Subscriptions)
CREATE TABLE user_decks (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
deck_template_id UUID NOT NULL REFERENCES deck_templates(id) ON DELETE CASCADE,
is_active BOOLEAN DEFAULT true,
created_at TIMESTAMPTZ DEFAULT NOW(),
UNIQUE(user_id, deck_template_id)
);
CREATE INDEX idx_user_decks_user_id ON user_decks(user_id);
CREATE INDEX idx_user_decks_deck_template_id ON user_decks(deck_template_id);
ALTER TABLE user_decks ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage own user_decks" ON user_decks
FOR ALL USING (auth.uid() = user_id);
-- User Card Progress (Progress Layer - SRS State)
CREATE TABLE user_card_progress (
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
card_template_id UUID NOT NULL REFERENCES card_templates(id) ON DELETE CASCADE,
interval INTEGER DEFAULT 0,
ease_factor REAL DEFAULT 2.5,
repetitions INTEGER DEFAULT 0,
next_review TIMESTAMPTZ DEFAULT NOW(),
last_answered_at TIMESTAMPTZ,
suspended BOOLEAN DEFAULT false,
PRIMARY KEY (user_id, card_template_id)
);
CREATE INDEX idx_user_card_progress_user_card ON user_card_progress(user_id, card_template_id);
CREATE INDEX idx_user_card_progress_user_next_review ON user_card_progress(user_id, next_review);
ALTER TABLE user_card_progress ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can manage own user_card_progress" ON user_card_progress
FOR ALL USING (auth.uid() = user_id);
-- ============================================
-- V6.4: Migration Function
-- ============================================
-- Run SELECT migrate_v1_to_v2() to migrate existing data
-- The function is idempotent (safe to run multiple times)
-- ============================================
-- V9: Medical Ontology (3-Tier Taxonomy)
-- ============================================
-- Tag category enum for 3-tier taxonomy
-- - source: Textbook/reference origin (e.g., "Williams", "Lange")
-- - topic: Medical chapter/domain (e.g., "Anatomy", "Endocrinology")
-- - concept: Specific medical concept (e.g., "Preeclampsia", "GestationalDiabetes")
-- Migration: Add category enum and column
-- DO $$ BEGIN
-- CREATE TYPE tag_category AS ENUM ('source', 'topic', 'concept');
-- EXCEPTION
-- WHEN duplicate_object THEN null;
-- END $$;
--
-- ALTER TABLE tags ADD COLUMN IF NOT EXISTS category tag_category NOT NULL DEFAULT 'concept';
-- UPDATE tags SET category = 'concept' WHERE category IS NULL;
-- CREATE INDEX IF NOT EXISTS idx_tags_category ON tags(category);
-- Category-to-Color Mapping (enforced by application):
-- source → blue
-- topic → purple
-- concept → green