-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathschema_min.sql
More file actions
1210 lines (1210 loc) · 59.2 KB
/
Copy pathschema_min.sql
File metadata and controls
1210 lines (1210 loc) · 59.2 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
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
CREATE TYPE public.attempt_status AS ENUM (
'in_progress',
'submitted',
'abandoned',
'auto_submitted'
);
CREATE TYPE public.question_type AS ENUM (
'single_correct',
'multiple_correct',
'true_false'
);
CREATE TYPE public.test_status AS ENUM (
'draft',
'published',
'archived'
);
CREATE OR REPLACE FUNCTION public.check_username_available(p_username text, p_user_id uuid) RETURNS boolean
LANGUAGE sql STABLE SECURITY DEFINER
SET search_path TO 'public'
AS $$
select not exists (
select 1
from public.profiles
where username = p_username
and id <> p_user_id
);
$$;
CREATE OR REPLACE FUNCTION public.get_candidate_home_stats(p_profile_id uuid) RETURNS jsonb
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'public'
AS $$
DECLARE
v_profile RECORD;
v_now TIMESTAMPTZ := NOW();
v_total_tests BIGINT := 0;
v_live_tests BIGINT := 0;
v_upcoming_tests BIGINT := 0;
v_completed_tests BIGINT := 0;
BEGIN
SELECT * INTO v_profile
FROM public.candidate_profiles
WHERE profile_id = p_profile_id;
IF v_profile.profile_id IS NOT NULL AND v_profile.recruiter_id IS NOT NULL THEN
SELECT count(*) INTO v_total_tests
FROM public.tests
WHERE status = 'published' AND recruiter_id = v_profile.recruiter_id;
SELECT count(*) INTO v_live_tests
FROM public.tests
WHERE status = 'published'
AND recruiter_id = v_profile.recruiter_id
AND (available_from IS NULL OR available_from <= v_now)
AND (available_until IS NULL OR available_until >= v_now);
SELECT count(*) INTO v_upcoming_tests
FROM public.tests
WHERE status = 'published'
AND recruiter_id = v_profile.recruiter_id
AND available_from > v_now;
SELECT count(*) INTO v_completed_tests
FROM public.test_attempts
WHERE student_id = p_profile_id AND status = 'submitted';
END IF;
RETURN jsonb_build_object(
'profile', (CASE WHEN v_profile.profile_id IS NOT NULL THEN row_to_json(v_profile) ELSE NULL END),
'stats', jsonb_build_object(
'total_tests', v_total_tests,
'live_tests', v_live_tests,
'upcoming_tests', v_upcoming_tests,
'completed_tests', v_completed_tests
)
);
END;
$$;
CREATE OR REPLACE FUNCTION public.get_recruiter_home_stats(p_profile_id uuid) RETURNS jsonb
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'public'
AS $$
DECLARE
v_profile RECORD;
v_now TIMESTAMPTZ := NOW();
v_total_tests BIGINT := 0;
v_live_tests BIGINT := 0;
v_upcoming_tests BIGINT := 0;
v_past_tests BIGINT := 0;
v_draft_tests BIGINT := 0;
v_total_attempts BIGINT := 0;
BEGIN
SELECT * INTO v_profile
FROM public.recruiter_profiles
WHERE profile_id = p_profile_id;
SELECT count(*) INTO v_total_tests
FROM public.tests
WHERE recruiter_id = p_profile_id;
SELECT count(*) INTO v_live_tests
FROM public.tests
WHERE recruiter_id = p_profile_id
AND status = 'published'
AND (available_from IS NULL OR available_from <= v_now)
AND (available_until IS NULL OR available_until >= v_now);
SELECT count(*) INTO v_upcoming_tests
FROM public.tests
WHERE recruiter_id = p_profile_id
AND status = 'published'
AND available_from > v_now;
SELECT count(*) INTO v_past_tests
FROM public.tests
WHERE recruiter_id = p_profile_id
AND status = 'published'
AND available_until < v_now;
SELECT count(*) INTO v_draft_tests
FROM public.tests
WHERE recruiter_id = p_profile_id
AND status = 'draft';
SELECT count(*) INTO v_total_attempts
FROM public.test_attempts ta
JOIN public.tests t ON ta.test_id = t.id
WHERE t.recruiter_id = p_profile_id;
RETURN jsonb_build_object(
'profile', (CASE WHEN v_profile.profile_id IS NOT NULL THEN row_to_json(v_profile) ELSE NULL END),
'stats', jsonb_build_object(
'total_tests', v_total_tests,
'live_tests', v_live_tests,
'upcoming_tests', v_upcoming_tests,
'past_tests', v_past_tests,
'draft_tests', v_draft_tests,
'total_attempts', v_total_attempts
)
);
END;
$$;
CREATE OR REPLACE FUNCTION public.grade_attempt(p_attempt_id uuid) RETURNS void
LANGUAGE plpgsql
AS $$
DECLARE
v_test_pass_pct numeric(5,2);
v_test_id uuid;
v_answer record;
v_correct_ids uuid[];
v_selected_ids uuid[];
v_is_correct boolean;
v_marks numeric(5,2);
v_total numeric(7,2) := 0;
v_scored numeric(7,2) := 0;
v_passed boolean := null;
BEGIN
SELECT test_id INTO v_test_id
FROM public.test_attempts
WHERE id = p_attempt_id AND status IN ('in_progress', 'auto_submitted');
IF v_test_id IS NULL THEN
RAISE EXCEPTION 'Attempt not found or already submitted'
USING errcode = 'no_data_found';
END IF;
SELECT
t.pass_percentage,
COALESCE((SELECT sum(marks) FROM public.questions WHERE test_id = t.id), 0)
INTO v_test_pass_pct, v_total
FROM public.tests t
WHERE t.id = v_test_id;
FOR v_answer IN
SELECT aa.id, aa.question_id, aa.selected_option_ids, q.marks, q.negative_marks
FROM public.attempt_answers aa
JOIN public.questions q ON q.id = aa.question_id
WHERE aa.attempt_id = p_attempt_id
LOOP
SELECT array_agg(id ORDER BY id) INTO v_correct_ids
FROM public.options
WHERE question_id = v_answer.question_id AND is_correct = TRUE;
SELECT array_agg(x ORDER BY x) INTO v_selected_ids
FROM UNNEST(v_answer.selected_option_ids) x;
v_is_correct := (COALESCE(v_selected_ids, '{}') = COALESCE(v_correct_ids, '{}'));
IF v_is_correct THEN
v_marks := v_answer.marks;
ELSIF ARRAY_LENGTH(v_selected_ids, 1) > 0 THEN
v_marks := -ABS(v_answer.negative_marks);
ELSE
v_marks := 0;
END IF;
UPDATE public.attempt_answers
SET is_correct = v_is_correct,
marks_awarded = v_marks,
updated_at = NOW()
WHERE id = v_answer.id;
v_scored := v_scored + v_marks;
END LOOP;
IF v_test_pass_pct IS NOT NULL AND v_total > 0 THEN
v_passed := (v_scored / v_total) * 100 >= v_test_pass_pct;
END IF;
UPDATE public.test_attempts
SET status = CASE WHEN status = 'in_progress' THEN 'submitted'::attempt_status ELSE status END,
submitted_at = COALESCE(submitted_at, NOW()),
score = v_scored,
total_marks = v_total,
passed = v_passed,
updated_at = NOW()
WHERE id = p_attempt_id;
END;
$$;
CREATE OR REPLACE FUNCTION public.grade_attempt_v2(p_attempt_id uuid, p_final_time_spent integer) RETURNS jsonb
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'public'
AS $$
DECLARE
v_test_pass_pct numeric(5,2);
v_test_id uuid;
v_answer record;
v_correct_ids uuid[];
v_selected_ids uuid[];
v_is_correct boolean;
v_marks numeric(5,2);
v_total numeric(7,2) := 0;
v_scored numeric(7,2) := 0;
v_passed boolean := null;
BEGIN
UPDATE public.test_attempts
SET time_spent_seconds = p_final_time_spent
WHERE id = p_attempt_id
AND student_id = auth.uid()
AND status IN ('in_progress', 'auto_submitted')
RETURNING test_id INTO v_test_id;
IF v_test_id IS NULL THEN
RETURN jsonb_build_object('error', 'Attempt not found or already submitted');
END IF;
SELECT
t.pass_percentage,
COALESCE((SELECT sum(marks) FROM public.questions WHERE test_id = t.id), 0)
INTO v_test_pass_pct, v_total
FROM public.tests t
WHERE t.id = v_test_id;
FOR v_answer IN
SELECT aa.id, aa.question_id, aa.selected_option_ids, q.marks, q.negative_marks
FROM public.attempt_answers aa
JOIN public.questions q ON q.id = aa.question_id
WHERE aa.attempt_id = p_attempt_id
LOOP
SELECT array_agg(id ORDER BY id) INTO v_correct_ids
FROM public.options
WHERE question_id = v_answer.question_id AND is_correct = TRUE;
SELECT array_agg(x ORDER BY x) INTO v_selected_ids
FROM UNNEST(v_answer.selected_option_ids) x;
v_is_correct := (COALESCE(v_selected_ids, '{}') = COALESCE(v_correct_ids, '{}'));
IF v_is_correct THEN
v_marks := v_answer.marks;
ELSIF ARRAY_LENGTH(v_selected_ids, 1) > 0 THEN
v_marks := -ABS(v_answer.negative_marks);
ELSE
v_marks := 0;
END IF;
UPDATE public.attempt_answers
SET is_correct = v_is_correct,
marks_awarded = v_marks,
updated_at = NOW()
WHERE id = v_answer.id;
v_scored := v_scored + v_marks;
END LOOP;
IF v_test_pass_pct IS NOT NULL AND v_total > 0 THEN
v_passed := (v_scored / v_total) * 100 >= v_test_pass_pct;
END IF;
UPDATE public.test_attempts
SET status = CASE WHEN status = 'in_progress' THEN 'submitted'::attempt_status ELSE status END,
submitted_at = COALESCE(submitted_at, NOW()),
score = v_scored,
total_marks = v_total,
passed = v_passed,
updated_at = NOW()
WHERE id = p_attempt_id;
RETURN jsonb_build_object(
'status', 'submitted',
'test_id', v_test_id,
'score', v_scored,
'total_marks', v_total
);
END;
$$;
CREATE OR REPLACE FUNCTION public.handle_new_user() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'public'
AS $$
begin
insert into public.profiles (id, email, account_type, display_name)
values (
new.id,
new.email,
'candidate',
new.raw_user_meta_data->>'display_name'
)
on conflict (id) do nothing;
return new;
end;
$$;
CREATE OR REPLACE FUNCTION public.handle_session_sync() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'public'
AS $$
BEGIN
IF (TG_OP = 'INSERT' OR TG_OP = 'UPDATE') THEN
INSERT INTO public.user_sessions (id, user_id, created_at, updated_at, not_after, ip, user_agent, tag)
VALUES (NEW.id, NEW.user_id, NEW.created_at, NEW.updated_at, NEW.not_after, NEW.ip, NEW.user_agent, NEW.tag)
ON CONFLICT (id) DO UPDATE SET
updated_at = EXCLUDED.updated_at,
not_after = EXCLUDED.not_after,
ip = EXCLUDED.ip,
user_agent = EXCLUDED.user_agent;
ELSIF (TG_OP = 'DELETE') THEN
DELETE FROM public.user_sessions WHERE id = OLD.id;
END IF;
RETURN NULL;
END;
$$;
CREATE OR REPLACE FUNCTION public.init_test_attempt(p_test_id uuid) RETURNS jsonb
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'public'
AS $$
DECLARE
v_user_id UUID := auth.uid();
v_profile RECORD;
v_test RECORD;
v_existing_attempt RECORD;
v_completed_count INT;
v_saved_answers JSONB;
BEGIN
SELECT recruiter_id, profile_complete, profile_updated
INTO v_profile FROM public.candidate_profiles WHERE profile_id = v_user_id;
IF v_profile IS NULL OR NOT COALESCE(v_profile.profile_complete, FALSE) OR NOT COALESCE(v_profile.profile_updated, FALSE) THEN
RETURN jsonb_build_object('error', 'Profile incomplete');
END IF;
SELECT id, status, recruiter_id, time_limit_seconds, max_attempts, title
INTO v_test FROM public.tests WHERE id = p_test_id;
IF v_test IS NULL OR v_test.status != 'published' OR v_test.recruiter_id != v_profile.recruiter_id THEN
RETURN jsonb_build_object('error', 'Test not available or invalid recruiter');
END IF;
SELECT id, started_at, expires_at, tab_switch_count
INTO v_existing_attempt
FROM public.test_attempts
WHERE test_id = p_test_id AND student_id = v_user_id AND status = 'in_progress'
ORDER BY created_at DESC LIMIT 1;
IF v_existing_attempt IS NOT NULL THEN
IF v_existing_attempt.expires_at IS NOT NULL AND v_existing_attempt.expires_at < NOW() THEN
PERFORM public.grade_attempt(v_existing_attempt.id);
RETURN jsonb_build_object('status', 'expired', 'test_id', p_test_id);
END IF;
SELECT jsonb_agg(jsonb_build_object(
'question_id', question_id,
'selected_option_ids', selected_option_ids
)) INTO v_saved_answers
FROM public.attempt_answers
WHERE attempt_id = v_existing_attempt.id;
RETURN jsonb_build_object(
'status', 'resumed',
'attempt', jsonb_build_object(
'id', v_existing_attempt.id,
'started_at', v_existing_attempt.started_at,
'expires_at', v_existing_attempt.expires_at,
'tab_switch_count', COALESCE(v_existing_attempt.tab_switch_count, 0)
),
'saved_answers', COALESCE(v_saved_answers, '[]'::jsonb)
);
END IF;
SELECT count(*) INTO v_completed_count
FROM public.test_attempts
WHERE test_id = p_test_id AND student_id = v_user_id
AND status IN ('submitted', 'auto_submitted');
IF v_completed_count >= v_test.max_attempts THEN
RETURN jsonb_build_object('error', 'Max attempts reached (' || v_completed_count || '/' || v_test.max_attempts || ')');
END IF;
RETURN jsonb_build_object(
'status', 'ready',
'completed_count', v_completed_count,
'max_attempts', v_test.max_attempts
);
END;
$$;
CREATE OR REPLACE FUNCTION public.revoke_session(p_session_id uuid) RETURNS void
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'public', 'auth'
AS $$
begin
if not exists (
select 1 from auth.sessions
where id = p_session_id
and user_id = auth.uid()
) then
raise exception 'Session not found or does not belong to the current user'
using errcode = 'insufficient_privilege';
end if;
delete from auth.sessions where id = p_session_id;
end;
$$;
CREATE OR REPLACE FUNCTION public.revoke_sessions_batch(p_session_ids uuid[]) RETURNS void
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'public', 'auth'
AS $$
BEGIN
DELETE FROM auth.sessions
WHERE id = ANY(p_session_ids)
AND user_id = auth.uid();
END;
$$;
CREATE OR REPLACE FUNCTION public.save_answer(p_attempt_id uuid, p_question_id uuid, p_selected_option_ids uuid[], p_time_spent_seconds integer DEFAULT 0) RETURNS void
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'public'
AS $$
begin
if not exists (
select 1 from public.test_attempts
where id = p_attempt_id
and student_id = auth.uid()
and status = 'in_progress'
and (expires_at is null or expires_at > now() - interval '10 seconds')
) then
raise exception 'Invalid, expired, or already-submitted attempt'
using errcode = 'insufficient_privilege';
end if;
insert into public.attempt_answers (
attempt_id,
question_id,
selected_option_ids,
time_spent_seconds
)
values (
p_attempt_id,
p_question_id,
p_selected_option_ids,
p_time_spent_seconds
)
on conflict (attempt_id, question_id)
do update set
selected_option_ids = excluded.selected_option_ids,
time_spent_seconds = attempt_answers.time_spent_seconds + excluded.time_spent_seconds,
answered_at = now(),
updated_at = now();
end;
$$;
CREATE OR REPLACE FUNCTION public.save_test_v2(p_test_id uuid, p_settings jsonb, p_questions jsonb[], p_status text) RETURNS void
LANGUAGE plpgsql SECURITY DEFINER
AS $$
DECLARE
v_user_id uuid := auth.uid();
v_q_json jsonb;
v_q_ids uuid[];
v_tag_id uuid;
v_tag_name text;
v_opt jsonb;
v_opt_idx int;
v_opt_ids uuid[];
BEGIN
IF EXISTS (SELECT 1 FROM public.tests WHERE id = p_test_id AND recruiter_id <> v_user_id) THEN
RAISE EXCEPTION 'Access denied';
END IF;
INSERT INTO public.tests (
id, recruiter_id, title, description, instructions,
time_limit_seconds, available_from, available_until, status,
shuffle_questions, shuffle_options, strict_mode
) VALUES (
p_test_id,
v_user_id,
p_settings->>'title',
p_settings->>'description',
p_settings->>'instructions',
(p_settings->>'time_limit_seconds')::int,
(p_settings->>'available_from')::timestamptz,
(p_settings->>'available_until')::timestamptz,
p_status::test_status,
COALESCE((p_settings->>'shuffle_questions')::boolean, false),
COALESCE((p_settings->>'shuffle_options')::boolean, false),
COALESCE((p_settings->>'strict_mode')::boolean, false)
)
ON CONFLICT (id) DO UPDATE SET
title = EXCLUDED.title,
description = EXCLUDED.description,
instructions = EXCLUDED.instructions,
time_limit_seconds = EXCLUDED.time_limit_seconds,
available_from = EXCLUDED.available_from,
available_until = EXCLUDED.available_until,
status = EXCLUDED.status,
shuffle_questions = EXCLUDED.shuffle_questions,
shuffle_options = EXCLUDED.shuffle_options,
strict_mode = EXCLUDED.strict_mode;
v_q_ids := ARRAY(SELECT (q->>'id')::uuid FROM unnest(p_questions) AS q);
DELETE FROM public.questions
WHERE test_id = p_test_id
AND id <> ALL(COALESCE(v_q_ids, ARRAY[]::uuid[]));
FOR i IN 1..cardinality(p_questions) LOOP
v_q_json := p_questions[i];
INSERT INTO public.questions (
id, test_id, question_text, question_type, marks, order_index, explanation
) VALUES (
(v_q_json->>'id')::uuid,
p_test_id,
v_q_json->>'question_text',
(v_q_json->>'question_type')::question_type,
(v_q_json->>'marks')::numeric,
i,
v_q_json->>'explanation'
)
ON CONFLICT (id) DO UPDATE SET
question_text = EXCLUDED.question_text,
question_type = EXCLUDED.question_type,
marks = EXCLUDED.marks,
order_index = EXCLUDED.order_index,
explanation = EXCLUDED.explanation;
v_opt_ids := ARRAY(SELECT (o->>'id')::uuid FROM jsonb_array_elements(v_q_json->'options') AS o WHERE o ? 'id');
DELETE FROM public.options
WHERE question_id = (v_q_json->>'id')::uuid
AND id <> ALL(COALESCE(v_opt_ids, ARRAY[]::uuid[]));
v_opt_idx := 1;
FOR v_opt IN SELECT jsonb_array_elements(v_q_json->'options') LOOP
INSERT INTO public.options (id, question_id, option_text, is_correct, order_index)
VALUES (
COALESCE((v_opt->>'id')::uuid, gen_random_uuid()),
(v_q_json->>'id')::uuid,
v_opt->>'option_text',
(v_opt->>'is_correct')::boolean,
v_opt_idx
)
ON CONFLICT (id) DO UPDATE SET
option_text = EXCLUDED.option_text,
is_correct = EXCLUDED.is_correct,
order_index = EXCLUDED.order_index;
v_opt_idx := v_opt_idx + 1;
END LOOP;
DELETE FROM public.question_tags WHERE question_id = (v_q_json->>'id')::uuid;
IF v_q_json ? 'tag_names' AND jsonb_array_length(v_q_json->'tag_names') > 0 THEN
FOR v_tag_name IN SELECT jsonb_array_elements_text(v_q_json->'tag_names') LOOP
INSERT INTO public.tags (name) VALUES (v_tag_name)
ON CONFLICT (name) DO NOTHING;
SELECT id INTO v_tag_id FROM public.tags WHERE name = v_tag_name;
INSERT INTO public.question_tags (question_id, tag_id)
VALUES ((v_q_json->>'id')::uuid, v_tag_id)
ON CONFLICT DO NOTHING;
END LOOP;
END IF;
END LOOP;
END;
$$;
CREATE OR REPLACE FUNCTION public.set_updated_at() RETURNS trigger
LANGUAGE plpgsql
SET search_path TO 'public'
AS $$
begin
new.updated_at = now();
return new;
end;
$$;
CREATE OR REPLACE FUNCTION public.sync_candidate_profile() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'public'
AS $$
begin
update public.profiles
set
display_name = nullif(new.full_name, ''),
avatar_path = new.profile_image_path
where id = new.profile_id;
return null;
end;
$$;
CREATE OR REPLACE FUNCTION public.sync_recruiter_profile() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'public'
AS $$
begin
update public.profiles
set
display_name = nullif(trim(new.recruiter_name), ''),
avatar_path = new.logo_path
where id = new.profile_id;
return null;
end;
$$;
CREATE OR REPLACE FUNCTION public.sync_user_session() RETURNS trigger
LANGUAGE plpgsql SECURITY DEFINER
SET search_path TO 'public'
AS $$
begin
if tg_op = 'INSERT' then
insert into public.user_sessions (id, user_id, created_at, updated_at, not_after, ip, user_agent, tag)
values (new.id, new.user_id, new.created_at, new.updated_at, new.not_after, new.ip, new.user_agent, new.tag)
on conflict (id) do nothing;
elsif tg_op = 'UPDATE' then
update public.user_sessions set
updated_at = new.updated_at,
not_after = new.not_after,
ip = new.ip,
user_agent = new.user_agent,
tag = new.tag
where id = new.id;
elsif tg_op = 'DELETE' then
delete from public.user_sessions where id = old.id;
end if;
return null;
end;
$$;
SET default_tablespace = '';
SET default_table_access_method = heap;
CREATE TABLE public.attempt_answers (
id uuid DEFAULT gen_random_uuid() NOT NULL,
attempt_id uuid NOT NULL,
question_id uuid NOT NULL,
selected_option_ids uuid[] DEFAULT '{}'::uuid[] NOT NULL,
time_spent_seconds integer DEFAULT 0 NOT NULL,
is_correct boolean,
marks_awarded numeric(5,2),
answered_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.profiles (
id uuid NOT NULL,
email text NOT NULL,
username text,
display_name text,
avatar_path text,
account_type text DEFAULT 'candidate'::text NOT NULL,
account_subtype text,
is_active boolean DEFAULT true NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
CONSTRAINT profiles_account_type_check CHECK ((account_type = ANY (ARRAY['admin'::text, 'recruiter'::text, 'recruiter'::text, 'candidate'::text, 'tpo'::text]))),
CONSTRAINT profiles_username_check CHECK ((username ~* '^[a-zA-Z0-9_]{3,20}$'::text))
);
CREATE TABLE public.test_attempts (
id uuid DEFAULT gen_random_uuid() NOT NULL,
test_id uuid NOT NULL,
student_id uuid NOT NULL,
status public.attempt_status DEFAULT 'in_progress'::public.attempt_status NOT NULL,
attempt_number integer DEFAULT 1 NOT NULL,
ip_address inet,
user_agent text,
tab_switch_count integer DEFAULT 0 NOT NULL,
score numeric(7,2),
total_marks numeric(7,2),
percentage numeric(5,2) GENERATED ALWAYS AS (
CASE
WHEN (total_marks > (0)::numeric) THEN round(((score / total_marks) * (100)::numeric), 2)
ELSE NULL::numeric
END) STORED,
passed boolean,
started_at timestamp with time zone DEFAULT now() NOT NULL,
expires_at timestamp with time zone,
submitted_at timestamp with time zone,
time_spent_seconds integer,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE OR REPLACE VIEW public.attempt_details AS
SELECT ta.id,
ta.test_id,
ta.student_id,
p.display_name AS student_name,
p.email AS student_email,
ta.status,
ta.attempt_number,
ta.score,
ta.total_marks,
ta.percentage,
ta.passed,
ta.time_spent_seconds,
ta.tab_switch_count,
ta.started_at,
ta.submitted_at
FROM (public.test_attempts ta
JOIN public.profiles p ON ((p.id = ta.student_id)));
CREATE TABLE public.candidate_profiles (
profile_id uuid NOT NULL,
first_name text,
middle_name text,
last_name text,
full_name text GENERATED ALWAYS AS (TRIM(BOTH FROM ((COALESCE((NULLIF(TRIM(BOTH FROM first_name), ''::text) || ' '::text), ''::text) || COALESCE((NULLIF(TRIM(BOTH FROM middle_name), ''::text) || ' '::text), ''::text)) || COALESCE(NULLIF(TRIM(BOTH FROM last_name), ''::text), ''::text)))) STORED,
gender character(1),
phone_number text,
date_of_birth date,
aadhaar_number text,
current_address text,
permanent_address text,
recruiter_id uuid,
recruiter_verified boolean,
university_prn text,
course_name text,
passout_year smallint,
ssc_percentage numeric(5,2),
ssc_pass_year smallint,
hsc_percentage numeric(5,2),
hsc_pass_year smallint,
diploma_percentage numeric(5,2),
diploma_pass_year smallint,
sgpa_sem1 numeric(4,2),
sgpa_sem2 numeric(4,2),
sgpa_sem3 numeric(4,2),
sgpa_sem4 numeric(4,2),
sgpa_sem5 numeric(4,2),
sgpa_sem6 numeric(4,2),
sgpa_sem7 numeric(4,2),
sgpa_sem8 numeric(4,2),
sgpa_sem9 numeric(4,2),
sgpa_sem10 numeric(4,2),
cgpa numeric(4,2) GENERATED ALWAYS AS (round(((((((((((COALESCE(sgpa_sem1, (0)::numeric) + COALESCE(sgpa_sem2, (0)::numeric)) + COALESCE(sgpa_sem3, (0)::numeric)) + COALESCE(sgpa_sem4, (0)::numeric)) + COALESCE(sgpa_sem5, (0)::numeric)) + COALESCE(sgpa_sem6, (0)::numeric)) + COALESCE(sgpa_sem7, (0)::numeric)) + COALESCE(sgpa_sem8, (0)::numeric)) + COALESCE(sgpa_sem9, (0)::numeric)) + COALESCE(sgpa_sem10, (0)::numeric)) / (NULLIF((((((((((((sgpa_sem1 IS NOT NULL))::integer + ((sgpa_sem2 IS NOT NULL))::integer) + ((sgpa_sem3 IS NOT NULL))::integer) + ((sgpa_sem4 IS NOT NULL))::integer) + ((sgpa_sem5 IS NOT NULL))::integer) + ((sgpa_sem6 IS NOT NULL))::integer) + ((sgpa_sem7 IS NOT NULL))::integer) + ((sgpa_sem8 IS NOT NULL))::integer) + ((sgpa_sem9 IS NOT NULL))::integer) + ((sgpa_sem10 IS NOT NULL))::integer), 0))::numeric), 2)) STORED,
is_hsc boolean,
is_diploma boolean,
profile_updated boolean DEFAULT false NOT NULL,
profile_image_path text,
skills text[],
linkedin_url text,
github_url text,
portfolio_links text[],
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
profile_complete boolean GENERATED ALWAYS AS (((first_name IS NOT NULL) AND (TRIM(BOTH FROM first_name) <> ''::text) AND (middle_name IS NOT NULL) AND (TRIM(BOTH FROM middle_name) <> ''::text) AND (last_name IS NOT NULL) AND (TRIM(BOTH FROM last_name) <> ''::text) AND (phone_number IS NOT NULL) AND (date_of_birth IS NOT NULL) AND (gender IS NOT NULL) AND (recruiter_id IS NOT NULL) AND (course_name IS NOT NULL) AND (passout_year IS NOT NULL) AND (ssc_percentage IS NOT NULL))) STORED,
CONSTRAINT candidate_profiles_aadhaar_number_check CHECK ((aadhaar_number ~ '^[0-9]{12}$'::text)),
CONSTRAINT candidate_profiles_cgpa_check CHECK ((cgpa <= (10)::numeric)),
CONSTRAINT candidate_profiles_date_of_birth_check CHECK ((date_of_birth <= CURRENT_DATE)),
CONSTRAINT candidate_profiles_diploma_percentage_check CHECK ((diploma_percentage <= (100)::numeric)),
CONSTRAINT candidate_profiles_gender_check CHECK ((gender = ANY (ARRAY['M'::bpchar, 'F'::bpchar, 'O'::bpchar]))),
CONSTRAINT candidate_profiles_hsc_percentage_check CHECK ((hsc_percentage <= (100)::numeric)),
CONSTRAINT candidate_profiles_phone_number_check CHECK ((phone_number ~ '^[0-9]{10}$'::text)),
CONSTRAINT candidate_profiles_sgpa_sem10_check CHECK ((sgpa_sem10 <= (10)::numeric)),
CONSTRAINT candidate_profiles_sgpa_sem1_check CHECK ((sgpa_sem1 <= (10)::numeric)),
CONSTRAINT candidate_profiles_sgpa_sem2_check CHECK ((sgpa_sem2 <= (10)::numeric)),
CONSTRAINT candidate_profiles_sgpa_sem3_check CHECK ((sgpa_sem3 <= (10)::numeric)),
CONSTRAINT candidate_profiles_sgpa_sem4_check CHECK ((sgpa_sem4 <= (10)::numeric)),
CONSTRAINT candidate_profiles_sgpa_sem5_check CHECK ((sgpa_sem5 <= (10)::numeric)),
CONSTRAINT candidate_profiles_sgpa_sem6_check CHECK ((sgpa_sem6 <= (10)::numeric)),
CONSTRAINT candidate_profiles_sgpa_sem7_check CHECK ((sgpa_sem7 <= (10)::numeric)),
CONSTRAINT candidate_profiles_sgpa_sem8_check CHECK ((sgpa_sem8 <= (10)::numeric)),
CONSTRAINT candidate_profiles_sgpa_sem9_check CHECK ((sgpa_sem9 <= (10)::numeric)),
CONSTRAINT candidate_profiles_ssc_percentage_check CHECK ((ssc_percentage <= (100)::numeric))
);
CREATE TABLE public.recruiter_profiles (
profile_id uuid NOT NULL,
recruiter_name text NOT NULL,
recruiter_code text,
established_year smallint,
affiliation text,
address text,
city text,
state text,
pincode text,
country text DEFAULT 'India'::text,
phone_number text,
email text,
website_url text,
principal_name text,
principal_email text,
principal_phone text,
courses text[],
social_links text[],
logo_path text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
profile_updated boolean DEFAULT false NOT NULL,
profile_complete boolean GENERATED ALWAYS AS (((recruiter_name IS NOT NULL) AND (TRIM(BOTH FROM recruiter_name) <> ''::text) AND ((address IS NOT NULL) AND (TRIM(BOTH FROM address) <> ''::text)) AND ((city IS NOT NULL) AND (TRIM(BOTH FROM city) <> ''::text)) AND ((state IS NOT NULL) AND (TRIM(BOTH FROM state) <> ''::text)) AND ((phone_number IS NOT NULL) AND (TRIM(BOTH FROM phone_number) <> ''::text)) AND ((email IS NOT NULL) AND (TRIM(BOTH FROM email) <> ''::text)))) STORED
);
CREATE TABLE public.options (
id uuid DEFAULT gen_random_uuid() NOT NULL,
question_id uuid NOT NULL,
option_text text NOT NULL,
media_url text,
is_correct boolean DEFAULT false NOT NULL,
order_index integer NOT NULL
);
CREATE TABLE public.question_tags (
question_id uuid NOT NULL,
tag_id uuid NOT NULL
);
CREATE TABLE public.questions (
id uuid DEFAULT gen_random_uuid() NOT NULL,
test_id uuid NOT NULL,
question_text text NOT NULL,
question_type public.question_type NOT NULL,
media_url text,
order_index integer NOT NULL,
marks numeric(5,2) DEFAULT 1 NOT NULL,
negative_marks numeric(5,2) DEFAULT 0 NOT NULL,
explanation text,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.tags (
id uuid DEFAULT gen_random_uuid() NOT NULL,
name text NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE OR REPLACE VIEW public.tag_performance AS
SELECT ta.student_id,
ta.test_id,
tg.id AS tag_id,
tg.name AS tag_name,
count(aa.id) AS total_questions,
sum(
CASE
WHEN aa.is_correct THEN 1
ELSE 0
END) AS correct_count,
round((((sum(
CASE
WHEN aa.is_correct THEN 1
ELSE 0
END))::numeric / (NULLIF(count(aa.id), 0))::numeric) * (100)::numeric), 1) AS accuracy_pct
FROM (((public.attempt_answers aa
JOIN public.test_attempts ta ON ((ta.id = aa.attempt_id)))
JOIN public.question_tags qt ON ((qt.question_id = aa.question_id)))
JOIN public.tags tg ON ((tg.id = qt.tag_id)))
WHERE (ta.status = ANY (ARRAY['submitted'::public.attempt_status, 'auto_submitted'::public.attempt_status]))
GROUP BY ta.student_id, ta.test_id, tg.id, tg.name;
CREATE TABLE public.tests (
id uuid DEFAULT gen_random_uuid() NOT NULL,
recruiter_id uuid NOT NULL,
title text NOT NULL,
description text,
instructions text,
time_limit_seconds integer,
pass_percentage numeric(5,2),
shuffle_questions boolean DEFAULT false NOT NULL,
shuffle_options boolean DEFAULT false NOT NULL,
strict_mode boolean DEFAULT false NOT NULL,
max_attempts integer DEFAULT 1 NOT NULL,
available_from timestamp with time zone,
available_until timestamp with time zone,
results_available boolean DEFAULT false NOT NULL,
status public.test_status DEFAULT 'draft'::public.test_status NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL
);
CREATE TABLE public.user_sessions (
id uuid NOT NULL,
user_id uuid NOT NULL,
created_at timestamp with time zone,
updated_at timestamp with time zone,
not_after timestamp with time zone,
ip inet,
user_agent text,
tag text
);
CREATE OR REPLACE VIEW public.view_question_analysis AS
SELECT q.id AS question_id,
q.test_id,
q.question_text,
q.marks,
count(aa.id) AS total_answers,
sum(
CASE
WHEN aa.is_correct THEN 1
ELSE 0
END) AS correct_answers,
round((((sum(
CASE
WHEN aa.is_correct THEN 1
ELSE 0
END))::numeric / (NULLIF(count(aa.id), 0))::numeric) * (100)::numeric), 1) AS success_rate_pct,
round(avg(aa.time_spent_seconds), 1) AS avg_time_spent
FROM (public.questions q
LEFT JOIN public.attempt_answers aa ON ((aa.question_id = q.id)))
GROUP BY q.id, q.test_id, q.question_text, q.marks;
CREATE OR REPLACE VIEW public.view_test_results_detailed AS
SELECT ta.id AS attempt_id,
ta.test_id,
ta.student_id,
p.display_name AS student_name,
p.email AS student_email,
cp.course_name AS branch,
cp.passout_year,
ta.status,
ta.attempt_number,
ta.score,
ta.total_marks,
ta.percentage,
ta.passed,
ta.time_spent_seconds,
ta.tab_switch_count,
ta.started_at,
ta.submitted_at
FROM ((public.test_attempts ta
JOIN public.profiles p ON ((p.id = ta.student_id)))
LEFT JOIN public.candidate_profiles cp ON ((cp.profile_id = ta.student_id)));
CREATE OR REPLACE VIEW public.view_test_summary AS
SELECT t.id,
t.title,
t.description,
t.recruiter_id,
ip.recruiter_name,
t.status,
t.available_from,
t.available_until,
t.time_limit_seconds,
t.results_available,
( SELECT count(*) AS count
FROM public.questions q
WHERE (q.test_id = t.id)) AS question_count,
( SELECT COALESCE(sum(q.marks), (0)::numeric) AS "coalesce"
FROM public.questions q
WHERE (q.test_id = t.id)) AS total_marks,
( SELECT count(*) AS count
FROM public.test_attempts ta
WHERE (ta.test_id = t.id)) AS total_attempts,
( SELECT count(*) AS count
FROM public.test_attempts ta
WHERE ((ta.test_id = t.id) AND (ta.status = 'submitted'::public.attempt_status))) AS submitted_attempts,
( SELECT round(avg(ta.percentage), 1) AS round
FROM public.test_attempts ta
WHERE ((ta.test_id = t.id) AND (ta.status = 'submitted'::public.attempt_status))) AS avg_score_pct
FROM (public.tests t
LEFT JOIN public.recruiter_profiles ip ON ((t.recruiter_id = ip.profile_id)));
ALTER TABLE ONLY public.attempt_answers
ADD CONSTRAINT attempt_answers_attempt_id_question_id_key UNIQUE (attempt_id, question_id);
ALTER TABLE ONLY public.attempt_answers
ADD CONSTRAINT attempt_answers_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.candidate_profiles
ADD CONSTRAINT candidate_profiles_pkey PRIMARY KEY (profile_id);
ALTER TABLE ONLY public.recruiter_profiles
ADD CONSTRAINT recruiter_profiles_pkey PRIMARY KEY (profile_id);
ALTER TABLE ONLY public.options
ADD CONSTRAINT options_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.options
ADD CONSTRAINT options_question_id_order_index_key UNIQUE (question_id, order_index);
ALTER TABLE ONLY public.profiles
ADD CONSTRAINT profiles_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.profiles
ADD CONSTRAINT profiles_username_key UNIQUE (username);
ALTER TABLE ONLY public.question_tags
ADD CONSTRAINT question_tags_pkey PRIMARY KEY (question_id, tag_id);
ALTER TABLE ONLY public.questions
ADD CONSTRAINT questions_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.questions
ADD CONSTRAINT questions_test_id_order_index_key UNIQUE (test_id, order_index);
ALTER TABLE ONLY public.tags
ADD CONSTRAINT tags_name_key UNIQUE (name);
ALTER TABLE ONLY public.tags
ADD CONSTRAINT tags_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.test_attempts
ADD CONSTRAINT test_attempts_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.test_attempts
ADD CONSTRAINT test_attempts_test_id_student_id_attempt_number_key UNIQUE (test_id, student_id, attempt_number);
ALTER TABLE ONLY public.tests
ADD CONSTRAINT tests_pkey PRIMARY KEY (id);
ALTER TABLE ONLY public.user_sessions
ADD CONSTRAINT user_sessions_pkey PRIMARY KEY (id);
CREATE INDEX idx_attempt_answers_attempt_id ON public.attempt_answers USING btree (attempt_id);
CREATE INDEX idx_attempt_answers_question_id ON public.attempt_answers USING btree (question_id);
CREATE INDEX idx_attempts_student_id ON public.test_attempts USING btree (student_id);
CREATE INDEX idx_attempts_test_id ON public.test_attempts USING btree (test_id);
CREATE INDEX idx_options_question_id ON public.options USING btree (question_id);
CREATE INDEX idx_options_question_id_order ON public.options USING btree (question_id, order_index);
CREATE INDEX idx_question_tags_tag_id ON public.question_tags USING btree (tag_id);
CREATE INDEX idx_questions_test_id ON public.questions USING btree (test_id);
CREATE INDEX idx_questions_test_id_order ON public.questions USING btree (test_id, order_index);
CREATE INDEX idx_test_attempts_student_test_status ON public.test_attempts USING btree (student_id, test_id, status);
CREATE INDEX idx_tests_recruiter_id ON public.tests USING btree (recruiter_id);
CREATE INDEX idx_tests_status ON public.tests USING btree (status);
CREATE INDEX user_sessions_user_id_created_at_idx ON public.user_sessions USING btree (user_id, created_at DESC);
CREATE OR REPLACE TRIGGER trg_attempt_answers_updated_at BEFORE UPDATE ON public.attempt_answers FOR EACH ROW EXECUTE FUNCTION extensions.moddatetime('updated_at');
CREATE OR REPLACE TRIGGER trg_attempts_updated_at BEFORE UPDATE ON public.test_attempts FOR EACH ROW EXECUTE FUNCTION extensions.moddatetime('updated_at');
CREATE OR REPLACE TRIGGER trg_candidate_profiles_sync AFTER INSERT OR UPDATE OF first_name, middle_name, last_name, profile_image_path ON public.candidate_profiles FOR EACH ROW EXECUTE FUNCTION public.sync_candidate_profile();
CREATE OR REPLACE TRIGGER trg_candidate_profiles_updated_at BEFORE UPDATE ON public.candidate_profiles FOR EACH ROW EXECUTE FUNCTION public.set_updated_at();
CREATE OR REPLACE TRIGGER trg_recruiter_profiles_sync AFTER INSERT OR UPDATE OF recruiter_name, logo_path ON public.recruiter_profiles FOR EACH ROW EXECUTE FUNCTION public.sync_recruiter_profile();
CREATE OR REPLACE TRIGGER trg_recruiter_profiles_updated_at BEFORE UPDATE ON public.recruiter_profiles FOR EACH ROW EXECUTE FUNCTION public.set_updated_at();
CREATE OR REPLACE TRIGGER trg_profiles_updated_at BEFORE UPDATE ON public.profiles FOR EACH ROW EXECUTE FUNCTION public.set_updated_at();
CREATE OR REPLACE TRIGGER trg_questions_updated_at BEFORE UPDATE ON public.questions FOR EACH ROW EXECUTE FUNCTION extensions.moddatetime('updated_at');
CREATE OR REPLACE TRIGGER trg_tests_updated_at BEFORE UPDATE ON public.tests FOR EACH ROW EXECUTE FUNCTION extensions.moddatetime('updated_at');
ALTER TABLE ONLY public.attempt_answers
ADD CONSTRAINT attempt_answers_attempt_id_fkey FOREIGN KEY (attempt_id) REFERENCES public.test_attempts(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.attempt_answers
ADD CONSTRAINT attempt_answers_question_id_fkey FOREIGN KEY (question_id) REFERENCES public.questions(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.candidate_profiles
ADD CONSTRAINT candidate_profiles_profile_id_fkey FOREIGN KEY (profile_id) REFERENCES public.profiles(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.recruiter_profiles
ADD CONSTRAINT recruiter_profiles_profile_id_fkey FOREIGN KEY (profile_id) REFERENCES public.profiles(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.options
ADD CONSTRAINT options_question_id_fkey FOREIGN KEY (question_id) REFERENCES public.questions(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.profiles
ADD CONSTRAINT profiles_id_fkey FOREIGN KEY (id) REFERENCES auth.users(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.question_tags
ADD CONSTRAINT question_tags_question_id_fkey FOREIGN KEY (question_id) REFERENCES public.questions(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.question_tags
ADD CONSTRAINT question_tags_tag_id_fkey FOREIGN KEY (tag_id) REFERENCES public.tags(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.questions
ADD CONSTRAINT questions_test_id_fkey FOREIGN KEY (test_id) REFERENCES public.tests(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.test_attempts
ADD CONSTRAINT test_attempts_student_id_fkey FOREIGN KEY (student_id) REFERENCES public.profiles(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.test_attempts
ADD CONSTRAINT test_attempts_test_id_fkey FOREIGN KEY (test_id) REFERENCES public.tests(id) ON DELETE CASCADE;
ALTER TABLE ONLY public.tests
ADD CONSTRAINT tests_recruiter_id_fkey FOREIGN KEY (recruiter_id) REFERENCES public.recruiter_profiles(profile_id) ON DELETE CASCADE;
ALTER TABLE ONLY public.user_sessions
ADD CONSTRAINT user_sessions_user_id_fkey FOREIGN KEY (user_id) REFERENCES public.profiles(id) ON DELETE CASCADE;
CREATE POLICY "Attempt answers are viewable by student and recruiter" ON public.attempt_answers FOR SELECT TO authenticated USING ((EXISTS ( SELECT 1
FROM public.test_attempts
WHERE ((test_attempts.id = attempt_answers.attempt_id) AND ((test_attempts.student_id = ( SELECT auth.uid() AS uid)) OR (EXISTS ( SELECT 1
FROM public.tests
WHERE ((tests.id = test_attempts.test_id) AND (tests.recruiter_id = ( SELECT auth.uid() AS uid))))))))));
CREATE POLICY "Authenticated users can create tags" ON public.tags FOR INSERT TO authenticated WITH CHECK (true);
CREATE POLICY "Candidate profiles are viewable by authenticated users" ON public.candidate_profiles FOR SELECT TO authenticated USING (true);
CREATE POLICY "Recruiter profiles are viewable by authenticated users" ON public.recruiter_profiles FOR SELECT TO authenticated USING (true);
CREATE POLICY "Recruiters can delete their own tests" ON public.tests FOR DELETE TO authenticated USING ((recruiter_id = ( SELECT auth.uid() AS uid)));