Spaces:
Running

math-tutor / db /001_schema.sql
seongui2030
deploy 2026-12-03 20:26
b146445
Raw History Blame Contribute Delete
20.4 kB
-- =====================================================================
-- k12-math-rag-tutor : 001_schema.sql
-- Supabase(PostgreSQL 15+) SQL Editor ์—์„œ ํ†ต์งธ๋กœ ์‹คํ–‰ํ•ฉ๋‹ˆ๋‹ค. (์—ฌ๋Ÿฌ ๋ฒˆ ์‹คํ–‰ํ•ด๋„ ์•ˆ์ „)
-- ์ˆœ์„œ: 001_schema.sql -> 002_seed_units.sql
--
-- ๋ณด์•ˆ ์›์น™
-- * ํ•™์ƒ ๋ธŒ๋ผ์šฐ์ €/ํŒจ๋“œ๋Š” Supabase ์— ์ง์ ‘ ์ ‘์†ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
-- Gradio ์„œ๋ฒ„(HF Space)๋งŒ service_role ํ‚ค๋กœ ์ ‘์†ํ•ฉ๋‹ˆ๋‹ค. (ํ‚ค๋Š” HF Secrets ์—๋งŒ ๋ณด๊ด€)
-- * ๊ทธ๋ž˜๋„ ๋งŒ์ผ์„ ๋Œ€๋น„ํ•ด ๋ชจ๋“  ํ…Œ์ด๋ธ”์— RLS ๋ฅผ ์ผœ๊ณ , ์ •๋‹ต/ํ•ด์„ค์ด ๋“ 
-- questions ๋Š” service_role ์™ธ์—๋Š” ์ฝ์„ ์ˆ˜ ์—†๊ฒŒ ๋‘ก๋‹ˆ๋‹ค.
-- =====================================================================
-- ---------------------------------------------------------------------
-- 0. ๊ณตํ†ต: enum, updated_at ํŠธ๋ฆฌ๊ฑฐ
-- ---------------------------------------------------------------------
do $$ begin
create type user_role as enum ('student', 'teacher', 'admin');
exception when duplicate_object then null; end $$;
do $$ begin
create type question_type as enum ('multiple_choice', 'short_answer');
exception when duplicate_object then null; end $$;
do $$ begin
create type difficulty as enum ('low', 'mid', 'high'); -- ํ•˜ / ์ค‘ / ์ƒ
exception when duplicate_object then null; end $$;
do $$ begin
create type question_status as enum ('draft', 'review', 'published', 'retired');
exception when duplicate_object then null; end $$;
do $$ begin
create type rights_status as enum ('unreviewed', 'cleared', 'restricted'); -- ์ด์šฉ ํ—ˆ๋ฝ ํ™•์ธ ์ƒํƒœ
exception when duplicate_object then null; end $$;
do $$ begin
create type session_status as enum ('in_progress', 'completed', 'abandoned');
exception when duplicate_object then null; end $$;
do $$ begin
create type step_phase as enum ('ladder', 'retry'); -- ํ•˜โ†’์ค‘โ†’์ƒ ์‚ฌ๋‹ค๋ฆฌ / ์˜ค๋‹ต ์žฌ๋„์ „
exception when duplicate_object then null; end $$;
do $$ begin
create type step_outcome as enum ('pending', 'correct', 'wrong');
exception when duplicate_object then null; end $$;
create or replace function set_updated_at()
returns trigger language plpgsql as $$
begin
new.updated_at = now();
return new;
end $$;
-- ---------------------------------------------------------------------
-- 1. ๊ต์œก๊ณผ์ • ๋งˆ์Šคํ„ฐ (์˜์—ญ 5๊ฐœ / ๋‹จ์› 77๊ฐœ = ๊ณ ๋“ฑ 42 + ์ค‘ํ•™๊ต ๋ณต์Šต 35)
-- '์ˆœ์—ด๊ณผ ์กฐํ•ฉ'์€ ๊ณตํ†ต์ˆ˜ํ•™๊ณผ ํ™•๋ฅ ๊ณผ ํ†ต๊ณ„ ์–‘์ชฝ์— ๋ณด์ด์ง€๋งŒ ๋‹จ์›์€ 1๊ฐœ์ž…๋‹ˆ๋‹ค.
-- -> units(๋‹จ์› 1๊ฐœ) + unit_areas(์˜์—ญ ์†Œ์†, ๋‹ค๋Œ€๋‹ค)
-- ---------------------------------------------------------------------
create table if not exists areas (
area_id text primary key, -- common | math1 | math2 | stats | middle
name_ko text not null unique,
sort_order smallint not null
);
create table if not exists units (
unit_id text primary key, -- U01~U42(๊ณ ๋“ฑ), M01~M35(์ค‘ํ•™๊ต). ๋ฐ”๋€Œ์ง€ ์•Š๋Š” ์‹๋ณ„์ž
name_ko text not null unique,
keywords text[] not null default '{}',-- 04_tag_curriculum_meta.py ํ‚ค์›Œ๋“œ (1์ฐจ ํƒœ๊น…์šฉ)
sort_order smallint not null
);
-- ํ•™๊ต๊ธ‰๊ณผ ์‹œ๋ฒ” ์šด์˜ ๋ฒ”์œ„ (์ด๋ฏธ ๋งŒ๋“  DB ์—๋„ ์•ˆ์ „ํ•˜๊ฒŒ ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค)
alter table units add column if not exists level text not null default 'high';
alter table units add column if not exists pilot boolean not null default false;
do $$ begin
if not exists (select 1 from pg_constraint where conname = 'units_level_check') then
alter table units add constraint units_level_check check (level in ('high', 'middle'));
end if;
end $$;
create table if not exists unit_areas (
unit_id text not null references units(unit_id) on delete cascade,
area_id text not null references areas(area_id) on delete cascade,
sort_order smallint not null, -- ์˜์—ญ ์•ˆ์—์„œ์˜ ํ‘œ์‹œ ์ˆœ์„œ
primary key (unit_id, area_id)
);
create table if not exists unit_prerequisites (
unit_id text not null references units(unit_id) on delete cascade,
prerequisite_id text not null references units(unit_id) on delete cascade,
primary key (unit_id, prerequisite_id),
check (unit_id <> prerequisite_id)
);
-- ---------------------------------------------------------------------
-- 2. ์‚ฌ์šฉ์ž / ํ•™๊ธ‰
-- ---------------------------------------------------------------------
create table if not exists profiles (
id uuid primary key references auth.users(id) on delete cascade,
email text,
name text,
role user_role not null default 'student', -- ์—ญํ•  ๋ณ€๊ฒฝ์€ ๊ด€๋ฆฌ์ž(service_role)๋งŒ
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
drop trigger if exists trg_profiles_updated on profiles;
create trigger trg_profiles_updated before update on profiles
for each row execute function set_updated_at();
-- ๊ตฌ๊ธ€ ๋กœ๊ทธ์ธ์œผ๋กœ auth.users ๊ฐ€ ์ƒ๊ธฐ๋ฉด profiles ํ–‰์„ ์ž๋™ ์ƒ์„ฑ (๊ธฐ๋ณธ ์—ญํ• : student)
create or replace function handle_new_user()
returns trigger language plpgsql security definer set search_path = public as $$
begin
insert into public.profiles (id, email, name)
values (
new.id,
new.email,
coalesce(new.raw_user_meta_data ->> 'full_name', new.raw_user_meta_data ->> 'name')
)
on conflict (id) do nothing;
return new;
end $$;
drop trigger if exists on_auth_user_created on auth.users;
create trigger on_auth_user_created after insert on auth.users
for each row execute function handle_new_user();
create table if not exists classes (
id uuid primary key default gen_random_uuid(),
school_year smallint not null, -- ์˜ˆ: 2027
grade smallint not null check (grade between 1 and 3),
class_no smallint not null check (class_no >= 1),
homeroom_teacher_id uuid references profiles(id) on delete set null,
created_at timestamptz not null default now(),
unique (school_year, grade, class_no)
);
create table if not exists class_members (
class_id uuid not null references classes(id) on delete cascade,
student_id uuid not null references profiles(id) on delete cascade,
student_no smallint, -- ๋ฒˆํ˜ธ
primary key (class_id, student_id)
);
create index if not exists idx_class_members_student on class_members(student_id);
-- ---------------------------------------------------------------------
-- 3. ๋ฌธ์ œ์€ํ–‰
-- ETL(05 ๊ฒ€์ฆ, 06 ์ ์žฌ)์„ ํ†ต๊ณผํ•œ ๋ฌธํ•ญ๋งŒ published ๊ฐ€ ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
-- ---------------------------------------------------------------------
create table if not exists questions (
id uuid primary key default gen_random_uuid(),
-- ์ถœ์ฒ˜ ์ถ”์  (์žฌ์ ์žฌ/์ค‘๋ณต ๋ฐฉ์ง€)
source text not null check (source in ('aihub', 'ebsi', 'jsj50', 'teacher', 'other')),
source_ref text not null, -- ์›๋ณธ ๋ฌธํ•ญ ์‹๋ณ„์ž (์˜ˆ: 2026_10_hs1_q07)
source_meta jsonb not null default '{}'::jsonb,-- ์›๋ณธ ๋ฉ”ํƒ€(ai_hub_meta ๋“ฑ)
rights_status rights_status not null default 'unreviewed',
-- ๋ถ„๋ฅ˜
grade_code text not null check (grade_code in ('e3','e4','e5','e6','m1','m2','m3','h1','h2','h3')),
unit_id text references units(unit_id), -- ํƒœ๊น… ์ „์—๋Š” null
difficulty difficulty, -- ํƒœ๊น… ์ „์—๋Š” null
score smallint, -- EBSi ๋ฐฐ์  (๋‚œ์ด๋„ ์ž„์‹œ ๊ธฐ์ค€)
-- ๋ณธ๋ฌธ (์ˆ˜์‹์€ LaTeX ๋ฅผ $...$ ๋กœ ๊ฐ์‹ธ ์ €์žฅ)
qtype question_type not null,
stem_md text not null,
choices jsonb not null default '[]'::jsonb,-- ["โ‘ ...", "โ‘ก..."] ๊ฐ๊ด€์‹๋งŒ
figure_description text, -- VLM ๋„ํ˜• ๋ฌ˜์‚ฌ
figure_path text, -- Supabase Storage ๊ฒฝ๋กœ
-- ์ •๋‹ต/ํ•ด์„ค (ํ•™์ƒ์—๊ฒŒ ์ง์ ‘ ๋…ธ์ถœ ๊ธˆ์ง€)
answer text not null, -- ๊ฐ๊ด€์‹: '1'~'5', ์ฃผ๊ด€์‹: ์ •๋‹ต ๋ฌธ์ž์—ด
accepted_answers text[] not null default '{}', -- ๋™์น˜ ํ‘œ๊ธฐ ํ—ˆ์šฉ ๋ชฉ๋ก
solution_md text,
intent text, -- ์ถœ์ œ ์˜๋„
-- ํ’ˆ์งˆ ๊ด€๋ฆฌ
verified boolean not null default false, -- 05 ๊ฒ€์ฆ(์ •๋‹ตํ‘œยทํ•ด์„คยท์žฌ๊ณ„์‚ฐ) ํ†ต๊ณผ ์—ฌ๋ถ€
verify_note text,
status question_status not null default 'draft',
created_at timestamptz not null default now(),
updated_at timestamptz not null default now(),
unique (source, source_ref),
-- ๊ฐ๊ด€์‹์€ ์„ ํƒ์ง€๊ฐ€ ์žˆ์–ด์•ผ ํ•จ
check (qtype <> 'multiple_choice' or jsonb_array_length(choices) >= 2),
-- ๊ฒŒ์‹œ(published) ์กฐ๊ฑด: ๋‹จ์›ยท๋‚œ์ด๋„ ํƒœ๊น… + ๊ฒ€์ฆ + ์ด์šฉ ํ—ˆ๋ฝ ํ™•์ธ
-- (ํŒŒ์ผ๋Ÿฟ์—์„œ ์™„ํ™”ํ•˜๋ ค๋ฉด rights_status ์กฐ๊ฑด๋งŒ ์กฐ์ •ํ•˜์„ธ์š”)
check (
status <> 'published'
or (unit_id is not null and difficulty is not null
and verified and rights_status = 'cleared'
and solution_md is not null)
)
);
drop trigger if exists trg_questions_updated on questions;
create trigger trg_questions_updated before update on questions
for each row execute function set_updated_at();
-- ๋ฌธ์ œ ๋ฝ‘๊ธฐ์šฉ: ๋‹จ์› x ๋‚œ์ด๋„ x ๊ฒŒ์‹œ ์ƒํƒœ
create index if not exists idx_questions_pick
on questions (unit_id, difficulty) where status = 'published';
create index if not exists idx_questions_grade on questions (grade_code);
-- ํ•™์ƒ ์˜ค๋ฅ˜ ์‹ ๊ณ 
create table if not exists question_reports (
id uuid primary key default gen_random_uuid(),
question_id uuid not null references questions(id) on delete cascade,
reporter_id uuid references profiles(id) on delete set null,
reason text not null,
resolved boolean not null default false,
created_at timestamptz not null default now()
);
create index if not exists idx_reports_open on question_reports (resolved, created_at);
-- ---------------------------------------------------------------------
-- 4. ํ•™์Šต ์„ธ์…˜ / ์ถœ์ œ ๋ชฉ๋ก / ์‹œ๋„
-- study_sessions : ํ•™์ƒ์ด ์–ด๋А ๋‚  ์–ด๋А ๋‹จ์›์„ ๊ณจ๋ž๋Š”์ง€ (ํ•˜๋ฃจ 1์„ธ์…˜/๋‹จ์›)
-- session_questions: ๊ทธ ์„ธ์…˜์—์„œ ๋‚ธ ๋ฌธ์ œ ์ˆœ์„œ (์‚ฌ๋‹ค๋ฆฌ + ์˜ค๋‹ต ์žฌ๋„์ „)
-- attempts : ์ œ์ถœ ๊ธฐ๋ก (ํ•œ ๋ฌธ์ œ์— ์—ฌ๋Ÿฌ ๋ฒˆ ์ œ์ถœ ๊ฐ€๋Šฅ)
-- ---------------------------------------------------------------------
create table if not exists study_sessions (
id uuid primary key default gen_random_uuid(),
student_id uuid not null references profiles(id) on delete cascade,
session_date date not null,
unit_id text not null references units(unit_id),
status session_status not null default 'in_progress',
min_required smallint not null default 3, -- ํ•˜๋ฃจ ์ตœ์†Œ ๋ฌธ์ œ ์ˆ˜
time_limit_sec integer not null default 900, -- ์ ์‹ฌ 15๋ถ„
started_at timestamptz not null default now(),
ended_at timestamptz,
unique (student_id, session_date, unit_id)
);
create index if not exists idx_sessions_student_date on study_sessions (student_id, session_date);
create index if not exists idx_sessions_date on study_sessions (session_date);
create table if not exists session_questions (
id uuid primary key default gen_random_uuid(),
session_id uuid not null references study_sessions(id) on delete cascade,
seq smallint not null, -- ์ถœ์ œ ์ˆœ์„œ (์ด์–ดํ•˜๊ธฐ ๊ธฐ์ค€)
question_id uuid not null references questions(id),
phase step_phase not null default 'ladder',
stage difficulty not null, -- ์–ด๋А ๋‚œ์ด๋„ ๋‹จ๊ณ„์—์„œ ๋‚˜์™”๋Š”์ง€
retry_of uuid references session_questions(id), -- ์žฌ๋„์ „์ด๋ฉด ์›๋ž˜ ๋ฌธํ•ญ ํ–‰
outcome step_outcome not null default 'pending',
resolved_at timestamptz, -- ํ‹€๋ฆฐ ๋ฌธ์ œ๋ฅผ ์žฌ๋„์ „์œผ๋กœ ๋งžํžŒ ์‹œ๊ฐ
created_at timestamptz not null default now(),
unique (session_id, seq),
check (phase = 'ladder' or retry_of is not null)
);
create index if not exists idx_sq_session on session_questions (session_id);
create index if not exists idx_sq_question on session_questions (question_id);
create table if not exists attempts (
id uuid primary key default gen_random_uuid(),
session_question_id uuid not null references session_questions(id) on delete cascade,
student_id uuid not null references profiles(id) on delete cascade, -- ์กฐํšŒ ํŽธ์˜์šฉ ์ค‘๋ณต ์ €์žฅ
submitted_answer text,
is_correct boolean not null,
time_spent_sec integer,
scratch_path text, -- ํ’€์ด ์Šค์ผ€์น˜ ์ด๋ฏธ์ง€(Storage ๊ฒฝ๋กœ)
created_at timestamptz not null default now()
);
create index if not exists idx_attempts_sq on attempts (session_question_id);
create index if not exists idx_attempts_student on attempts (student_id, created_at);
-- ---------------------------------------------------------------------
-- 5. ์ƒ๋‹ด / ์ผ์ • ์˜ˆ์™ธ
-- ---------------------------------------------------------------------
create table if not exists counseling_notes (
id uuid primary key default gen_random_uuid(),
student_id uuid not null references profiles(id) on delete cascade,
teacher_id uuid not null references profiles(id),
reason text not null,
note text,
follow_up_on date,
created_at timestamptz not null default now()
);
create index if not exists idx_counseling_student on counseling_notes (student_id, created_at);
create table if not exists calendar_exceptions (
exception_date date primary key, -- ํ•™๊ต ํ–‰์‚ฌ ๋“ฑ ๋ฃจํ‹ด ๋ฉด์ œ์ผ
title text not null
);
-- ---------------------------------------------------------------------
-- 6. ๊ต์‚ฌ ๋Œ€์‹œ๋ณด๋“œ์šฉ ๋ทฐ: ํ•™์ƒ x ๋‚ ์งœ x ๋‹จ์› ์š”์•ฝ
-- unresolved_count = ํ‹€๋ ธ๊ณ  ์•„์ง ์žฌ๋„์ „์œผ๋กœ ํ•ด๊ฒฐํ•˜์ง€ ๋ชปํ•œ ๋ฌธ์ œ ์ˆ˜
-- ---------------------------------------------------------------------
create or replace view v_student_day_unit
with (security_invoker = true) as
select
s.id as session_id,
s.student_id,
s.session_date,
s.unit_id,
s.status,
count(*) filter (where sq.phase = 'ladder' and sq.outcome <> 'pending') as answered_count,
count(*) filter (where sq.phase = 'ladder' and sq.outcome = 'correct') as correct_count,
count(*) filter (where sq.phase = 'ladder' and sq.outcome = 'wrong' and sq.resolved_at is null) as unresolved_count
from study_sessions s
left join session_questions sq on sq.session_id = s.id
group by s.id;
-- ---------------------------------------------------------------------
-- 7. RLS (ํ–‰ ์ˆ˜์ค€ ๋ณด์•ˆ)
-- ---------------------------------------------------------------------
create or replace function my_role()
returns user_role language sql stable security definer set search_path = public as $$
select role from public.profiles where id = auth.uid()
$$;
create or replace function is_teacher_of(target_student uuid)
returns boolean language sql stable security definer set search_path = public as $$
select exists (
select 1
from public.class_members cm
join public.classes c on c.id = cm.class_id
where cm.student_id = target_student
and c.homeroom_teacher_id = auth.uid()
)
$$;
alter table areas enable row level security;
alter table units enable row level security;
alter table unit_areas enable row level security;
alter table unit_prerequisites enable row level security;
alter table profiles enable row level security;
alter table classes enable row level security;
alter table class_members enable row level security;
alter table questions enable row level security;
alter table question_reports enable row level security;
alter table study_sessions enable row level security;
alter table session_questions enable row level security;
alter table attempts enable row level security;
alter table counseling_notes enable row level security;
alter table calendar_exceptions enable row level security;
-- ๊ต์œก๊ณผ์ • ๋งˆ์Šคํ„ฐ/์ผ์ •: ๋กœ๊ทธ์ธํ•œ ์‚ฌ์šฉ์ž๋Š” ์ฝ๊ธฐ ๊ฐ€๋Šฅ
drop policy if exists read_areas on areas;
create policy read_areas on areas for select to authenticated using (true);
drop policy if exists read_units on units;
create policy read_units on units for select to authenticated using (true);
drop policy if exists read_unit_areas on unit_areas;
create policy read_unit_areas on unit_areas for select to authenticated using (true);
drop policy if exists read_unit_prereq on unit_prerequisites;
create policy read_unit_prereq on unit_prerequisites for select to authenticated using (true);
drop policy if exists read_cal_exc on calendar_exceptions;
create policy read_cal_exc on calendar_exceptions for select to authenticated using (true);
-- ํ”„๋กœํ•„: ๋ณธ์ธ / ๋‹ด์ž„๊ต์‚ฌ / ๊ด€๋ฆฌ์ž
drop policy if exists read_profiles on profiles;
create policy read_profiles on profiles for select to authenticated
using (id = auth.uid() or is_teacher_of(id) or my_role() = 'admin');
-- ํ•™๊ธ‰ (classes <-> class_members ๊ฐ€ ์„œ๋กœ ์ฐธ์กฐํ•˜๋ฉด ์ •์ฑ… ๋ฌดํ•œ ์žฌ๊ท€๊ฐ€ ๋‚˜๋ฏ€๋กœ
-- RLS ๋ฅผ ์šฐํšŒํ•˜๋Š” security definer ํ•จ์ˆ˜๋กœ ํŒ์ •ํ•ฉ๋‹ˆ๋‹ค)
create or replace function is_class_member(cid uuid)
returns boolean language sql stable security definer set search_path = public as $$
select exists (select 1 from public.class_members where class_id = cid and student_id = auth.uid())
$$;
create or replace function is_homeroom_of(cid uuid)
returns boolean language sql stable security definer set search_path = public as $$
select exists (select 1 from public.classes where id = cid and homeroom_teacher_id = auth.uid())
$$;
drop policy if exists read_classes on classes;
create policy read_classes on classes for select to authenticated
using (homeroom_teacher_id = auth.uid() or my_role() = 'admin' or is_class_member(id));
drop policy if exists read_class_members on class_members;
create policy read_class_members on class_members for select to authenticated
using (student_id = auth.uid() or my_role() = 'admin' or is_homeroom_of(class_id));
-- ๋ฌธ์ œ์€ํ–‰(questions): authenticated ์šฉ ์ •์ฑ…์„ ๋งŒ๋“ค์ง€ ์•Š์Œ => service_role ๋งŒ ์ ‘๊ทผ
-- ํ•™์Šต ๊ธฐ๋ก: ๋ณธ์ธ / ๋‹ด์ž„๊ต์‚ฌ / ๊ด€๋ฆฌ์ž ์ฝ๊ธฐ (์“ฐ๊ธฐ๋Š” Gradio ์„œ๋ฒ„์˜ service_role ๋งŒ)
drop policy if exists read_sessions on study_sessions;
create policy read_sessions on study_sessions for select to authenticated
using (student_id = auth.uid() or is_teacher_of(student_id) or my_role() = 'admin');
drop policy if exists read_session_questions on session_questions;
create policy read_session_questions on session_questions for select to authenticated
using (exists (
select 1 from study_sessions s
where s.id = session_questions.session_id
and (s.student_id = auth.uid() or is_teacher_of(s.student_id) or my_role() = 'admin')
));
drop policy if exists read_attempts on attempts;
create policy read_attempts on attempts for select to authenticated
using (student_id = auth.uid() or is_teacher_of(student_id) or my_role() = 'admin');
-- ์˜ค๋ฅ˜ ์‹ ๊ณ : ๋กœ๊ทธ์ธ ์‚ฌ์šฉ์ž๊ฐ€ ์ž‘์„ฑ, ๋ณธ์ธ/๊ด€๋ฆฌ์ž๊ฐ€ ์กฐํšŒ
drop policy if exists insert_reports on question_reports;
create policy insert_reports on question_reports for insert to authenticated
with check (reporter_id = auth.uid());
drop policy if exists read_reports on question_reports;
create policy read_reports on question_reports for select to authenticated
using (reporter_id = auth.uid() or my_role() = 'admin');
-- ์ƒ๋‹ด ๊ธฐ๋ก: ๋‹ด์ž„๊ต์‚ฌ(์ž๊ธฐ ํ•™์ƒ) / ๊ด€๋ฆฌ์ž. ํ•™์ƒ์—๊ฒŒ๋Š” ๋น„๊ณต๊ฐœ
drop policy if exists read_counseling on counseling_notes;
create policy read_counseling on counseling_notes for select to authenticated
using ((my_role() = 'teacher' and is_teacher_of(student_id)) or my_role() = 'admin');
drop policy if exists insert_counseling on counseling_notes;
create policy insert_counseling on counseling_notes for insert to authenticated
with check (teacher_id = auth.uid() and my_role() = 'teacher' and is_teacher_of(student_id));