Repository navigation
Expand file tree
/
Copy pathschema.sql
More file actions
175 lines (161 loc) · 13.3 KB
/
Copy pathschema.sql
File metadata and controls
175 lines (161 loc) · 13.3 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
-- Baseline for a NEW Interview Questions project. Never run over existing tables.
-- Private answers, accounts, and recordings are migrated separately.
begin;
create table public."interview_sets" (
"id" bigint generated by default as identity not null,
"owner_id" uuid not null,
"slug" text not null,
"company" text not null,
"position" text not null,
"location" text default ''::text not null,
"job_url" text default ''::text not null,
"is_public" boolean default false not null,
"is_archived" boolean default false not null,
"sort_order" integer default 0 not null,
"created_at" timestamp with time zone default now() not null,
"updated_at" timestamp with time zone default now() not null
);
alter table public."interview_sets" enable row level security;
create table public."interview_questions" (
"id" integer generated by default as identity not null,
"set_id" bigint not null,
"category" text not null,
"question" text not null,
"question_zh" text default ''::text not null,
"sort_order" integer default 0 not null,
"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
);
alter table public."interview_questions" enable row level security;
create table public."interview_private_content" (
"user_id" uuid not null,
"question_id" integer not null,
"answer" text default ''::text not null,
"outline" text default ''::text not null,
"tags" text[] default '{}'::text[] not null,
"updated_at" timestamp with time zone default now() not null,
"answer_variants" jsonb default '{}'::jsonb not null,
"audio_text_hash" text,
"duration_seconds" numeric,
"audio_variants" jsonb default '{}'::jsonb not null
);
alter table public."interview_private_content" enable row level security;
create table public."interview_user_state" (
"user_id" uuid not null,
"question_id" integer not null,
"favorite" boolean default false not null,
"practiced" boolean default false not null,
"mastery" text,
"own_answer" text default ''::text not null,
"updated_at" timestamp with time zone default now() not null,
"last_practiced_at" timestamp with time zone
);
alter table public."interview_user_state" enable row level security;
alter table public."interview_private_content" add constraint "interview_private_content_pkey" PRIMARY KEY (user_id, question_id);
alter table public."interview_user_state" add constraint "interview_user_state_pkey" PRIMARY KEY (user_id, question_id);
alter table public."interview_sets" add constraint "interview_sets_pkey" PRIMARY KEY (id);
alter table public."interview_questions" add constraint "interview_questions_pkey" PRIMARY KEY (id);
alter table public."interview_private_content" add constraint "interview_private_content_audio_variants_check" CHECK ((jsonb_typeof(audio_variants) = 'object'::text));
alter table public."interview_private_content" add constraint "interview_private_content_duration_seconds_check" CHECK (((duration_seconds > (0)::numeric) AND (duration_seconds < 'Infinity'::numeric)));
alter table public."interview_private_content" add constraint "interview_private_content_question_id_check" CHECK ((question_id > 0));
alter table public."interview_private_content" add constraint "interview_private_content_question_id_fkey" FOREIGN KEY (question_id) REFERENCES public.interview_questions(id) ON DELETE CASCADE;
alter table public."interview_private_content" add constraint "interview_private_content_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;
alter table public."interview_user_state" add constraint "interview_user_state_mastery_check" CHECK (((mastery IS NULL) OR (mastery = ANY (ARRAY['learning'::text, 'okay'::text, 'confident'::text]))));
alter table public."interview_user_state" add constraint "interview_user_state_own_answer_check" CHECK ((char_length(own_answer) <= 20000));
alter table public."interview_user_state" add constraint "interview_user_state_question_id_check" CHECK ((question_id > 0));
alter table public."interview_user_state" add constraint "interview_user_state_question_id_fkey" FOREIGN KEY (question_id) REFERENCES public.interview_questions(id) ON DELETE CASCADE;
alter table public."interview_user_state" add constraint "interview_user_state_user_id_fkey" FOREIGN KEY (user_id) REFERENCES auth.users(id) ON DELETE CASCADE;
alter table public."interview_sets" add constraint "interview_sets_company_check" CHECK (((char_length(company) >= 1) AND (char_length(company) <= 200)));
alter table public."interview_sets" add constraint "interview_sets_job_url_check" CHECK ((char_length(job_url) <= 2000));
alter table public."interview_sets" add constraint "interview_sets_location_check" CHECK ((char_length(location) <= 200));
alter table public."interview_sets" add constraint "interview_sets_owner_id_fkey" FOREIGN KEY (owner_id) REFERENCES auth.users(id) ON DELETE CASCADE;
alter table public."interview_sets" add constraint "interview_sets_owner_id_slug_key" UNIQUE (owner_id, slug);
alter table public."interview_sets" add constraint "interview_sets_position_check" CHECK (((char_length("position") >= 1) AND (char_length("position") <= 200)));
alter table public."interview_questions" add constraint "interview_questions_category_check" CHECK (((char_length(category) >= 1) AND (char_length(category) <= 200)));
alter table public."interview_questions" add constraint "interview_questions_question_check" CHECK (((char_length(question) >= 1) AND (char_length(question) <= 4000)));
alter table public."interview_questions" add constraint "interview_questions_question_zh_check" CHECK ((char_length(question_zh) <= 4000));
alter table public."interview_questions" add constraint "interview_questions_set_id_fkey" FOREIGN KEY (set_id) REFERENCES public.interview_sets(id) ON DELETE CASCADE;
alter table public."interview_questions" add constraint "interview_questions_set_id_sort_order_key" UNIQUE (set_id, sort_order);
CREATE INDEX interview_private_content_question_id_idx ON public.interview_private_content USING btree (question_id);
CREATE INDEX interview_questions_set_sort_idx ON public.interview_questions USING btree (set_id, is_active, sort_order, id);
CREATE INDEX interview_sets_owner_sort_idx ON public.interview_sets USING btree (owner_id, is_archived, sort_order, id);
CREATE INDEX interview_user_state_question_id_idx ON public.interview_user_state USING btree (question_id);
create policy "interview_private_content_select_own" on public."interview_private_content" for select to "authenticated" using ((( SELECT auth.uid() AS uid) = user_id));
create policy "interview_private_content_insert_own" on public."interview_private_content" for insert to "authenticated" with check ((( SELECT auth.uid() AS uid) = user_id));
create policy "interview_private_content_update_own" on public."interview_private_content" for update to "authenticated" using ((( SELECT auth.uid() AS uid) = user_id)) with check ((( SELECT auth.uid() AS uid) = user_id));
create policy "interview_private_content_delete_own" on public."interview_private_content" for delete to "authenticated" using ((( SELECT auth.uid() AS uid) = user_id));
create policy "interview_user_state_select_own" on public."interview_user_state" for select to "authenticated" using ((( SELECT auth.uid() AS uid) = user_id));
create policy "interview_user_state_insert_own" on public."interview_user_state" for insert to "authenticated" with check ((( SELECT auth.uid() AS uid) = user_id));
create policy "interview_user_state_update_own" on public."interview_user_state" for update to "authenticated" using ((( SELECT auth.uid() AS uid) = user_id)) with check ((( SELECT auth.uid() AS uid) = user_id));
create policy "interview_user_state_delete_own" on public."interview_user_state" for delete to "authenticated" using ((( SELECT auth.uid() AS uid) = user_id));
create policy "interview_sets_select_visible" on public."interview_sets" for select to "anon", "authenticated" using ((is_public OR (owner_id = ( SELECT auth.uid() AS uid))));
create policy "interview_sets_insert_own" on public."interview_sets" for insert to "authenticated" with check ((owner_id = ( SELECT auth.uid() AS uid)));
create policy "interview_sets_update_own" on public."interview_sets" for update to "authenticated" using ((owner_id = ( SELECT auth.uid() AS uid))) with check ((owner_id = ( SELECT auth.uid() AS uid)));
create policy "interview_sets_delete_own" on public."interview_sets" for delete to "authenticated" using ((owner_id = ( SELECT auth.uid() AS uid)));
create policy "interview_questions_select_visible" on public."interview_questions" for select to "anon", "authenticated" using ((EXISTS ( SELECT 1
FROM interview_sets s
WHERE ((s.id = interview_questions.set_id) AND (s.is_public OR (s.owner_id = ( SELECT auth.uid() AS uid)))))));
create policy "interview_questions_insert_own" on public."interview_questions" for insert to "authenticated" with check ((EXISTS ( SELECT 1
FROM interview_sets s
WHERE ((s.id = interview_questions.set_id) AND (s.owner_id = ( SELECT auth.uid() AS uid))))));
create policy "interview_questions_update_own" on public."interview_questions" for update to "authenticated" using ((EXISTS ( SELECT 1
FROM interview_sets s
WHERE ((s.id = interview_questions.set_id) AND (s.owner_id = ( SELECT auth.uid() AS uid)))))) with check ((EXISTS ( SELECT 1
FROM interview_sets s
WHERE ((s.id = interview_questions.set_id) AND (s.owner_id = ( SELECT auth.uid() AS uid))))));
create policy "interview_questions_delete_own" on public."interview_questions" for delete to "authenticated" using ((EXISTS ( SELECT 1
FROM interview_sets s
WHERE ((s.id = interview_questions.set_id) AND (s.owner_id = ( SELECT auth.uid() AS uid))))));
CREATE OR REPLACE FUNCTION public.interview_set_updated_at()
RETURNS trigger
LANGUAGE plpgsql
SET search_path TO ''
AS $function$
begin
new.updated_at = now();
return new;
end;
$function$;
revoke all on function public."interview_set_updated_at"() from public, anon, authenticated;
CREATE OR REPLACE FUNCTION public.version_interview_private_content()
RETURNS trigger
LANGUAGE plpgsql
SET search_path TO ''
AS $function$
begin
new.updated_at := greatest(pg_catalog.clock_timestamp(), old.updated_at + interval '1 microsecond');
return new;
end;
$function$;
revoke all on function public."version_interview_private_content"() from public, anon, authenticated;
CREATE OR REPLACE FUNCTION public.version_interview_question_audio()
RETURNS trigger
LANGUAGE plpgsql
SET search_path TO ''
AS $function$
begin
-- Retains the caller's existing RLS permissions; no private text is changed.
update public.interview_private_content
set updated_at = updated_at
where question_id = new.id;
return new;
end;
$function$;
revoke all on function public."version_interview_question_audio"() from public, anon, authenticated;
CREATE TRIGGER interview_sets_touch_updated_at BEFORE UPDATE ON public.interview_sets FOR EACH ROW EXECUTE FUNCTION public."interview_set_updated_at"();
CREATE TRIGGER interview_questions_touch_updated_at BEFORE UPDATE ON public.interview_questions FOR EACH ROW EXECUTE FUNCTION public."interview_set_updated_at"();
CREATE TRIGGER version_interview_private_content BEFORE UPDATE ON public.interview_private_content FOR EACH ROW EXECUTE FUNCTION public."version_interview_private_content"();
CREATE TRIGGER version_interview_question_audio AFTER UPDATE OF question ON public.interview_questions FOR EACH ROW WHEN ((old.question IS DISTINCT FROM new.question)) EXECUTE FUNCTION public."version_interview_question_audio"();
revoke all on public."interview_sets", public."interview_questions", public."interview_private_content", public."interview_user_state" from public, anon, authenticated;
grant select on public.interview_sets, public.interview_questions to anon;
grant select, insert, update, delete on public."interview_sets", public."interview_questions", public."interview_private_content", public."interview_user_state" to authenticated;
grant all on public."interview_sets", public."interview_questions", public."interview_private_content", public."interview_user_state" to service_role;
revoke all on sequence public.interview_sets_id_seq, public.interview_questions_id_seq from public, anon, authenticated;
grant usage, select on sequence public.interview_sets_id_seq, public.interview_questions_id_seq to authenticated;
grant all on sequence public.interview_sets_id_seq, public.interview_questions_id_seq to service_role;
create policy "interview audio select own" on storage.objects for select to authenticated using (((bucket_id = 'interview-audio'::text) AND ((storage.foldername(name))[1] = ( SELECT (auth.uid())::text AS uid))));
create policy "interview audio insert own" on storage.objects for insert to authenticated with check (((bucket_id = 'interview-audio'::text) AND ((storage.foldername(name))[1] = ( SELECT (auth.uid())::text AS uid))));
create policy "interview audio update own" on storage.objects for update to authenticated using (((bucket_id = 'interview-audio'::text) AND ((storage.foldername(name))[1] = ( SELECT (auth.uid())::text AS uid)))) with check (((bucket_id = 'interview-audio'::text) AND ((storage.foldername(name))[1] = ( SELECT (auth.uid())::text AS uid))));
create policy "interview audio delete own" on storage.objects for delete to authenticated using (((bucket_id = 'interview-audio'::text) AND ((storage.foldername(name))[1] = ( SELECT (auth.uid())::text AS uid))));
commit;