AnatoBot / supabase_schema.sql
stevafernandes's picture
Upload 10 files
191750c verified
Raw History Blame Contribute Delete
6.05 kB
-- ============================================================================
-- Creighton Anatomy Learning Platform - Supabase schema
--
-- Run this ONCE in your Supabase project:
-- Supabase Studio > SQL Editor > New query > paste > Run
-- It is idempotent: running it again is harmless.
-- ============================================================================
-- ----------------------------------------------------------------------------
-- 1. Accounts: one row per user ID. Sign-in checks user_id + email together.
-- ----------------------------------------------------------------------------
create table if not exists public.anatomy_users (
user_id text primary key, -- lower-case; letters, digits, . _ - (2-64 chars)
email text not null, -- lower-case
created_at timestamptz not null default now(),
last_login_at timestamptz
);
-- ----------------------------------------------------------------------------
-- 2. Chat history: every message shown in the chat, in order, per user.
-- Reloaded into the chat when the user signs in again.
-- ----------------------------------------------------------------------------
create table if not exists public.anatomy_messages (
id bigint generated always as identity primary key,
user_id text not null references public.anatomy_users (user_id),
session_id text,
role text not null check (role in ('user', 'assistant')),
content text not null,
mode text, -- ask, quiz, clinical, simplify, show
topic text,
source text, -- slides, web, slides+web, none, error
created_at timestamptz not null default now()
);
create index if not exists anatomy_messages_user_idx
on public.anatomy_messages (user_id, id);
-- ----------------------------------------------------------------------------
-- 3. Time on site: one row per visit (a sign-in, or a return after the tab lost
-- its connection). The open tab reports every 30 s, so
-- last_seen_at - started_at is the time the user spent on the site.
-- ----------------------------------------------------------------------------
create table if not exists public.anatomy_sessions (
session_id text primary key,
user_id text not null references public.anatomy_users (user_id),
started_at timestamptz not null default now(),
last_seen_at timestamptz not null default now(),
ended_at timestamptz -- set on sign-out, or when the tab closes or loses its connection
);
create index if not exists anatomy_sessions_user_idx
on public.anatomy_sessions (user_id, started_at);
-- ----------------------------------------------------------------------------
-- 4. Clicks: one row per button click.
-- event: sign_in, create_account, sign_out, resume, ask, quiz, clinical, simplify,
-- show_image, clear, previous, next, browse_lecture, browse_slide, example
-- detail: e.g. {"chars": 42} for ask, {"lecture": 7, "slide": 12} for browse_slide
-- ----------------------------------------------------------------------------
create table if not exists public.anatomy_events (
id bigint generated always as identity primary key,
user_id text not null references public.anatomy_users (user_id),
session_id text,
event text not null,
detail jsonb not null default '{}'::jsonb,
created_at timestamptz not null default now()
);
create index if not exists anatomy_events_user_idx
on public.anatomy_events (user_id, created_at);
create index if not exists anatomy_events_event_idx
on public.anatomy_events (event);
-- ----------------------------------------------------------------------------
-- 5. Row Level Security: ON with NO policies, so the public anon key can read
-- and write nothing. The app uses the service_role key, which bypasses RLS
-- and never leaves the Space's server-side Python process.
-- ----------------------------------------------------------------------------
alter table public.anatomy_users enable row level security;
alter table public.anatomy_messages enable row level security;
alter table public.anatomy_sessions enable row level security;
alter table public.anatomy_events enable row level security;
revoke all on public.anatomy_users from anon, authenticated;
revoke all on public.anatomy_messages from anon, authenticated;
revoke all on public.anatomy_sessions from anon, authenticated;
revoke all on public.anatomy_events from anon, authenticated;
-- ----------------------------------------------------------------------------
-- 6. Analytics views (Table Editor > anatomy_time_per_user / anatomy_button_clicks).
-- security_invoker = on keeps RLS in force for whoever queries the view;
-- the explicit revoke is needed because Supabase grants SELECT on new
-- objects in `public` to anon/authenticated by default.
-- ----------------------------------------------------------------------------
drop view if exists public.anatomy_time_per_user;
create view public.anatomy_time_per_user
with (security_invoker = on) as
select
s.user_id,
u.email,
count(*) as sessions,
sum(extract(epoch from (s.last_seen_at - s.started_at)))::bigint as seconds_on_site,
min(s.started_at) as first_visit,
max(s.last_seen_at) as last_visit
from public.anatomy_sessions s
join public.anatomy_users u on u.user_id = s.user_id
group by s.user_id, u.email
order by seconds_on_site desc;
drop view if exists public.anatomy_button_clicks;
create view public.anatomy_button_clicks
with (security_invoker = on) as
select user_id, event, count(*) as clicks
from public.anatomy_events
group by user_id, event
order by user_id, clicks desc;
revoke all on public.anatomy_time_per_user from anon, authenticated;
revoke all on public.anatomy_button_clicks from anon, authenticated;