Spaces:
Configuration error
Configuration error
File size: 5,963 Bytes
e46d575 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 | -- ============================================================
-- 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();
|