-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathschema.sql
More file actions
168 lines (167 loc) · 6.78 KB
/
Copy pathschema.sql
File metadata and controls
168 lines (167 loc) · 6.78 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
-- WARNING: This schema is for context only and is not meant to be run.
-- Table order and constraints may not be valid for execution.
CREATE TABLE public.comments (
id uuid NOT NULL DEFAULT gen_random_uuid(),
exercise_id uuid NOT NULL,
user_id uuid NOT NULL,
content text NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT comments_pkey PRIMARY KEY (id),
CONSTRAINT comments_exercise_id_fkey FOREIGN KEY (exercise_id) REFERENCES public.exercises(id),
CONSTRAINT comments_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.profiles(id)
);
CREATE TABLE public.duel_questions (
id uuid NOT NULL DEFAULT gen_random_uuid(),
duel_id uuid NOT NULL,
question_index integer NOT NULL,
num1 integer NOT NULL,
num2 integer NOT NULL,
operation text NOT NULL,
correct_answer integer NOT NULL,
player1_answer integer,
player1_time_ms integer,
player2_answer integer,
player2_time_ms integer,
CONSTRAINT duel_questions_pkey PRIMARY KEY (id),
CONSTRAINT duel_questions_duel_id_fkey FOREIGN KEY (duel_id) REFERENCES public.duels(id)
);
CREATE TABLE public.duels (
id uuid NOT NULL DEFAULT gen_random_uuid(),
player1_id uuid NOT NULL,
player2_id uuid,
category text,
status text NOT NULL DEFAULT 'waiting'::text,
player1_score integer NOT NULL DEFAULT 0,
player2_score integer NOT NULL DEFAULT 0,
winner_id uuid,
current_question integer NOT NULL DEFAULT 0,
total_questions integer NOT NULL DEFAULT 10,
invite_code text UNIQUE,
created_at timestamp with time zone NOT NULL DEFAULT now(),
finished_at timestamp with time zone,
CONSTRAINT duels_pkey PRIMARY KEY (id),
CONSTRAINT duels_player1_id_fkey FOREIGN KEY (player1_id) REFERENCES public.profiles(id),
CONSTRAINT duels_player2_id_fkey FOREIGN KEY (player2_id) REFERENCES public.profiles(id),
CONSTRAINT duels_winner_id_fkey FOREIGN KEY (winner_id) REFERENCES public.profiles(id)
);
CREATE TABLE public.exercise_votes (
id uuid NOT NULL DEFAULT gen_random_uuid(),
exercise_id uuid NOT NULL,
user_id uuid NOT NULL,
created_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT exercise_votes_pkey PRIMARY KEY (id),
CONSTRAINT exercise_votes_exercise_id_fkey FOREIGN KEY (exercise_id) REFERENCES public.exercises(id),
CONSTRAINT exercise_votes_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.profiles(id)
);
CREATE TABLE public.exercises (
id uuid NOT NULL DEFAULT gen_random_uuid(),
author_id uuid NOT NULL,
title text NOT NULL,
category text NOT NULL,
statement text NOT NULL,
solution text NOT NULL,
image_url text,
status text NOT NULL DEFAULT 'pending'::text,
rejection_reason text,
verified_by uuid,
votes_count integer NOT NULL DEFAULT 0,
is_preloaded boolean NOT NULL DEFAULT false,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT exercises_pkey PRIMARY KEY (id),
CONSTRAINT exercises_author_id_fkey FOREIGN KEY (author_id) REFERENCES public.profiles(id),
CONSTRAINT exercises_verified_by_fkey FOREIGN KEY (verified_by) REFERENCES public.profiles(id)
);
CREATE TABLE public.flashcards (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id uuid NOT NULL,
exercise_id uuid,
front text NOT NULL,
back text NOT NULL,
category text,
easiness_factor double precision NOT NULL DEFAULT 2.5,
interval integer NOT NULL DEFAULT 1,
repetitions integer NOT NULL DEFAULT 0,
next_review_date timestamp with time zone NOT NULL DEFAULT now(),
last_review_date timestamp with time zone,
is_preloaded boolean NOT NULL DEFAULT false,
created_at timestamp with time zone NOT NULL DEFAULT now(),
updated_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT flashcards_pkey PRIMARY KEY (id),
CONSTRAINT flashcards_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.profiles(id),
CONSTRAINT flashcards_exercise_id_fkey FOREIGN KEY (exercise_id) REFERENCES public.exercises(id)
);
CREATE TABLE public.notifications (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id uuid NOT NULL,
type text NOT NULL,
title text NOT NULL,
body text,
reference_id uuid,
is_read boolean NOT NULL DEFAULT false,
created_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT notifications_pkey PRIMARY KEY (id),
CONSTRAINT notifications_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.profiles(id)
);
CREATE TABLE public.profiles (
id uuid NOT NULL,
email text NOT NULL UNIQUE,
username text NOT NULL UNIQUE,
display_name text,
avatar_url text,
bio text,
university text,
reputation integer NOT NULL DEFAULT 0,
points integer NOT NULL DEFAULT 0,
study_streak integer NOT NULL DEFAULT 0,
is_moderator boolean NOT NULL DEFAULT false,
created_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT profiles_pkey PRIMARY KEY (id),
CONSTRAINT profiles_id_fkey FOREIGN KEY (id) REFERENCES auth.users(id)
);
CREATE TABLE public.tournament_participants (
id uuid NOT NULL DEFAULT gen_random_uuid(),
tournament_id uuid NOT NULL,
user_id uuid NOT NULL,
score integer NOT NULL DEFAULT 0,
duels_won integer NOT NULL DEFAULT 0,
duels_played integer NOT NULL DEFAULT 0,
CONSTRAINT tournament_participants_pkey PRIMARY KEY (id),
CONSTRAINT tournament_participants_tournament_id_fkey FOREIGN KEY (tournament_id) REFERENCES public.tournaments(id),
CONSTRAINT tournament_participants_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.profiles(id)
);
CREATE TABLE public.tournaments (
id uuid NOT NULL DEFAULT gen_random_uuid(),
name text NOT NULL,
category text,
start_date timestamp with time zone NOT NULL,
end_date timestamp with time zone NOT NULL,
status text NOT NULL DEFAULT 'active'::text,
created_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT tournaments_pkey PRIMARY KEY (id)
);
CREATE TABLE public.usage_logs (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id uuid,
action text NOT NULL,
tokens_used integer NOT NULL DEFAULT 0,
cost_usd numeric NOT NULL DEFAULT 0.0,
created_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT usage_logs_pkey PRIMARY KEY (id),
CONSTRAINT usage_logs_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.profiles(id)
);
CREATE TABLE public.user_badges (
id uuid NOT NULL DEFAULT gen_random_uuid(),
user_id uuid NOT NULL,
badge_key text NOT NULL,
earned_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT user_badges_pkey PRIMARY KEY (id),
CONSTRAINT user_badges_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.profiles(id)
);
CREATE TABLE public.user_credits (
user_id uuid NOT NULL,
credits integer NOT NULL DEFAULT 100,
updated_at timestamp with time zone NOT NULL DEFAULT now(),
CONSTRAINT user_credits_pkey PRIMARY KEY (user_id),
CONSTRAINT user_credits_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.profiles(id)
);