Spaces:
Running

math-tutor / db /007_mentoring.sql
seongui2030
deploy 2026-12-03 20:26
b146445
Raw History Blame Contribute Delete
2.58 kB
-- =====================================================================
-- 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;