📌 1. questions 테이블

질문 데이터를 저장하는 메인 테이블입니다.

create table if not exists questions (
  id uuid primary key default gen_random_uuid(),
  category text not null check (category in ('tech', 'cs', 'collab')),
  question_text text not null,
  model_answer text not null,
  created_at timestamp with time zone default timezone('utc'::text, now()),
  created_by uuid references auth.users(id) on delete set null
);

RLS 및 정책 설정

alter table questions enable row level security;
create policy "Public read access to questions" on questions for select using (true);
create policy "Only authenticated users can insert" on questions for insert with check (created_by is null or auth.uid() = created_by);

샘플 데이터 삽입

insert into questions (category, question_text, model_answer)
values
  ('tech', 'React의 Virtual DOM이란?', 'Virtual DOM은 실제 DOM의 가벼운 복사본으로, 변경사항을 메모리에서 먼저 처리한 후 실제 DOM에 최소한으로 반영합니다.'),
  ('cs', 'HTTP와 HTTPS의 차이점은?', 'HTTPS는 SSL/TLS를 이용해 데이터를 암호화하며, HTTP는 그렇지 않습니다.'),
  ('collab', '협업 시 코드 리뷰의 목적은?', '코드 리뷰는 버그를 사전에 방지하고, 팀 내 코드 품질을 높이며 지식을 공유하는 데 목적이 있습니다.');


📌 2. user_wrong_questions: 오답노트 테이블

create table if not exists user_wrong_questions (
  id uuid primary key default gen_random_uuid(),
  user_id uuid not null references auth.users(id) on delete cascade,
  question_id uuid not null references questions(id) on delete cascade,
  user_answer text,
  ai_score int,
  created_at timestamp with time zone default now()
);
create unique index if not exists uq_wrong on user_wrong_questions (user_id, question_id);

정책 분리 권장

alter table user_wrong_questions enable row level security;
create policy "오답노트 조회" on user_wrong_questions for select using (user_id = auth.uid());
create policy "오답노트 등록" on user_wrong_questions for insert with check (user_id = auth.uid());


📌 3. user_bookmarks: 북마크 테이블

create table if not exists user_bookmarks (
  id uuid primary key default gen_random_uuid(),
  user_id uuid not null references auth.users(id) on delete cascade,
  question_id uuid not null references questions(id) on delete cascade,
  created_at timestamp with time zone default now()
);
create unique index if not exists uq_bookmark on user_bookmarks (user_id, question_id);

정책 분리 권장

alter table user_bookmarks enable row level security;
create policy "북마크 조회" on user_bookmarks for select using (user_id = auth.uid());
create policy "북마크 등록" on user_bookmarks for insert with check (user_id = auth.uid());


📌 4. user_answers: 정답 기록 테이블

create table if not exists user_answers (
  id uuid primary key default gen_random_uuid(),
  user_id uuid not null references auth.users(id) on delete cascade,
  question_id uuid not null references questions(id) on delete cascade,
  user_answer text not null,
  ai_score int,
  ai_feedback text,
  created_at timestamp with time zone default now()
);