Spaces:
Configuration error
Configuration error
Download database/01_schema.sql from rockysreenivasa/MathTutor: direct link, hf CLI and curl.
- Browser
- Download file 5.96 kB
-
https://huggingface.co/spaces/rockysreenivasa/MathTutor/resolve/main/database/01_schema.sql
- Command line
-
hf download hf://spaces/rockysreenivasa/MathTutor/database/01_schema.sql
-
curl -L -o 01_schema.sql https://huggingface.co/spaces/rockysreenivasa/MathTutor/resolve/main/database/01_schema.sql
5.96 kB
| -- ============================================================ | |
| -- MathMind AI β Database Schema | |
| -- Run this in: Supabase Dashboard β SQL Editor | |
| -- Order: 01_schema.sql β 02_rls.sql β 03_seed.sql | |
| -- ============================================================ | |
| -- Enable UUID generation | |
| create extension if not exists "pgcrypto"; | |
| -- ββ 1. profiles ββββββββββββββββββββββββββββββββββββββββββββ | |
| -- Extends auth.users β one row per student | |
| create table if not exists public.profiles ( | |
| id uuid primary key references auth.users(id) on delete cascade, | |
| grade smallint not null check (grade between 6 and 12), | |
| streak_count integer not null default 0, | |
| last_active date, | |
| created_at timestamptz not null default now(), | |
| updated_at timestamptz not null default now() | |
| ); | |
| comment on table public.profiles is 'Student profile β extends Supabase auth.users'; | |
| -- Auto-update updated_at | |
| create or replace function public.set_updated_at() | |
| returns trigger language plpgsql as $$ | |
| begin | |
| new.updated_at = now(); | |
| return new; | |
| end; | |
| $$; | |
| create trigger profiles_updated_at | |
| before update on public.profiles | |
| for each row execute function public.set_updated_at(); | |
| -- ββ 2. sessions ββββββββββββββββββββββββββββββββββββββββββββ | |
| -- One row per tutor session (question + explanation) | |
| create table if not exists public.sessions ( | |
| id uuid primary key default gen_random_uuid(), | |
| user_id uuid not null references public.profiles(id) on delete cascade, | |
| topic text not null, | |
| grade smallint not null check (grade between 6 and 12), | |
| explanation text not null, | |
| created_at timestamptz not null default now() | |
| ); | |
| comment on table public.sessions is 'One tutor session per student question'; | |
| create index if not exists sessions_user_id_idx on public.sessions(user_id); | |
| create index if not exists sessions_created_at_idx on public.sessions(created_at desc); | |
| -- ββ 3. practice_attempts βββββββββββββββββββββββββββββββββββ | |
| -- Practice problem responses within a session | |
| create table if not exists public.practice_attempts ( | |
| id uuid primary key default gen_random_uuid(), | |
| session_id uuid not null references public.sessions(id) on delete cascade, | |
| question text not null, | |
| student_answer text not null default '', | |
| is_correct boolean not null default false, | |
| created_at timestamptz not null default now() | |
| ); | |
| comment on table public.practice_attempts is 'Student answers to practice problems'; | |
| create index if not exists practice_attempts_session_idx on public.practice_attempts(session_id); | |
| -- ββ 4. test_results ββββββββββββββββββββββββββββββββββββββββ | |
| -- Mini-test outcomes per session | |
| create table if not exists public.test_results ( | |
| id uuid primary key default gen_random_uuid(), | |
| session_id uuid not null references public.sessions(id) on delete cascade, | |
| user_id uuid not null references public.profiles(id) on delete cascade, | |
| score smallint not null check (score between 0 and 100), | |
| passed boolean not null default false, | |
| attempts smallint not null default 1, | |
| created_at timestamptz not null default now() | |
| ); | |
| comment on table public.test_results is 'Mini-test results β 80% threshold for pass'; | |
| create index if not exists test_results_user_id_idx on public.test_results(user_id); | |
| create index if not exists test_results_session_idx on public.test_results(session_id); | |
| -- ββ 5. badges ββββββββββββββββββββββββββββββββββββββββββββββ | |
| -- Badge definitions (populated by 03_seed.sql) | |
| create table if not exists public.badges ( | |
| id uuid primary key default gen_random_uuid(), | |
| name text not null unique, | |
| emoji text not null, | |
| condition_key text not null unique, -- matched in Python badge engine | |
| description text not null, | |
| created_at timestamptz not null default now() | |
| ); | |
| comment on table public.badges is 'Badge definitions β condition_key matched by badge engine'; | |
| -- ββ 6. student_badges ββββββββββββββββββββββββββββββββββββββ | |
| -- Badges earned by each student | |
| create table if not exists public.student_badges ( | |
| id uuid primary key default gen_random_uuid(), | |
| user_id uuid not null references public.profiles(id) on delete cascade, | |
| badge_id uuid not null references public.badges(id) on delete cascade, | |
| earned_at timestamptz not null default now(), | |
| unique(user_id, badge_id) -- a student earns each badge only once | |
| ); | |
| comment on table public.student_badges is 'Badges awarded to students'; | |
| create index if not exists student_badges_user_idx on public.student_badges(user_id); | |
| -- ββ Auto-create profile on signup ββββββββββββββββββββββββββ | |
| -- Triggered when a new user signs up via Supabase Auth | |
| -- Grade defaults to 8 until the student sets it in the app | |
| create or replace function public.handle_new_user() | |
| returns trigger language plpgsql security definer as $$ | |
| begin | |
| insert into public.profiles (id, grade) | |
| values (new.id, 8) | |
| 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 public.handle_new_user(); | |