-- ===================================================================== -- 007_mentoring.sql : 태권도 띠 '멘토' 기능 (가르치며 배우기) -- -- 하는 일 -- 1) mentor_optin : 학생이 "이 단원 멘토로 나설게요"를 켰는지 (검은띠 단원이 있는 학생만 화면에 버튼이 보임) -- 2) class_settings : 학급별 멘토 연결 켜기/끄기 (담임·관리자가 교사 화면에서 바꿈, 기본은 켜짐) -- 3) mentoring_log : "이 친구에게 배웠어요" 한 번 = 한 줄 (배운 학생이 누름). 멘토의 '단' 계산에 씀 -- -- 개인정보: RLS 를 켜고 정책을 만들지 않습니다. 앱 서버(service_role)만 읽고 씁니다. (004 와 같은 방식) -- 여러 번 실행해도 안전합니다. (Supabase > SQL Editor 에서 실행) -- 이 SQL 을 실행하기 전에도 앱은 정상 동작합니다(멘토 기능만 꺼진 것으로 보임). -- ===================================================================== create table if not exists mentor_optin ( student_id uuid primary key references profiles(id) on delete cascade, opt_in boolean not null default false, updated_at timestamptz not null default now() ); create table if not exists class_settings ( class_id text primary key, -- "학년-반" (예: "1-10"), allowed_users 기준 mentoring_on boolean not null default true, updated_by uuid references profiles(id) on delete set null, updated_at timestamptz not null default now() ); create table if not exists mentoring_log ( id uuid primary key default gen_random_uuid(), mentor_id uuid not null references profiles(id) on delete cascade, mentee_id uuid not null references profiles(id) on delete cascade, unit_id text not null, qid text, -- 함께 푼 문항(source_ref) created_at timestamptz not null default now(), day date not null default ((now() at time zone 'Asia/Seoul')::date), check (mentor_id <> mentee_id) ); create index if not exists idx_mentoring_mentor on mentoring_log (mentor_id); create index if not exists idx_mentoring_mentee on mentoring_log (mentee_id); alter table mentor_optin enable row level security; alter table class_settings enable row level security; alter table mentoring_log enable row level security; grant select, insert, update, delete on mentor_optin to service_role; grant select, insert, update, delete on class_settings to service_role; grant select, insert, update, delete on mentoring_log to service_role;