Spaces:
Sleeping
Sleeping
File size: 2,733 Bytes
35fbb15 c6aa66a 35fbb15 | 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 | -- ============================================================================
-- Docent AI β Supabase schema for authentication-backed chat history
-- Run this ONCE in your Supabase project:
-- Dashboard β SQL Editor β New query β paste this file β Run
-- ============================================================================
-- ββ Conversations (one row per chat) βββββββββββββββββββββββββββββββββββββββ
create table if not exists public.chat_sessions (
id uuid primary key default gen_random_uuid(),
user_id uuid not null references auth.users (id) on delete cascade,
title text not null default 'New chat',
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
-- ββ Messages (user + assistant turns) ββββββββββββββββββββββββββββββββββββββ
create table if not exists public.messages (
id uuid primary key default gen_random_uuid(),
session_id uuid not null references public.chat_sessions (id) on delete cascade,
role text not null check (role in ('user', 'assistant')),
content text not null,
sources jsonb not null default '[]'::jsonb,
created_at timestamptz not null default now()
);
-- User feedback on assistant answers: 'up', 'down', or null
alter table public.messages add column if not exists feedback text;
create index if not exists idx_sessions_user on public.chat_sessions (user_id, updated_at desc);
create index if not exists idx_messages_session on public.messages (session_id, created_at);
-- ββ Row Level Security ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
-- The backend uses the service-role key (which bypasses RLS), but we still
-- enable RLS so the public anon key can never read another user's data.
alter table public.chat_sessions enable row level security;
alter table public.messages enable row level security;
drop policy if exists "own sessions" on public.chat_sessions;
create policy "own sessions" on public.chat_sessions
for all
using (auth.uid() = user_id)
with check (auth.uid() = user_id);
drop policy if exists "own messages" on public.messages;
create policy "own messages" on public.messages
for all
using (exists (
select 1 from public.chat_sessions s
where s.id = messages.session_id and s.user_id = auth.uid()
))
with check (exists (
select 1 from public.chat_sessions s
where s.id = messages.session_id and s.user_id = auth.uid()
));
|