NyayBot / src /backend /schema.sql
Udit Kumar
deploy: initial NyayBot production release
2da4138
Raw
History Blame Contribute Delete
3.44 kB
-- Enable UUID extension if not enabled (Supabase enables pgcrypto and uuid-ossp by default)
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- -------------------------------------------------------------
-- TABLE: conversations
-- -------------------------------------------------------------
CREATE TABLE IF NOT EXISTS conversations (
id TEXT PRIMARY KEY,
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
title TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- Index on user_id for fast user-scoped conversation lookups
CREATE INDEX IF NOT EXISTS idx_conversations_user_id ON conversations(user_id);
-- -------------------------------------------------------------
-- TABLE: messages
-- -------------------------------------------------------------
CREATE TABLE IF NOT EXISTS messages (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
conversation_id TEXT NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
user_id UUID NOT NULL REFERENCES auth.users(id) ON DELETE CASCADE,
role TEXT NOT NULL CHECK (role IN ('user', 'assistant')),
content TEXT NOT NULL,
sources JSONB NOT NULL DEFAULT '[]'::jsonb,
confidence DOUBLE PRECISION,
refused BOOLEAN NOT NULL DEFAULT FALSE,
namespace_searched TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
-- Indexes for optimal lookup and sorting
CREATE INDEX IF NOT EXISTS idx_messages_conversation_id ON messages(conversation_id);
CREATE INDEX IF NOT EXISTS idx_messages_user_id ON messages(user_id);
CREATE INDEX IF NOT EXISTS idx_messages_created_at ON messages(created_at);
-- -------------------------------------------------------------
-- ROW LEVEL SECURITY (RLS) POLICIES
-- -------------------------------------------------------------
-- Enable RLS on both tables
ALTER TABLE conversations ENABLE ROW LEVEL SECURITY;
ALTER TABLE messages ENABLE ROW LEVEL SECURITY;
-- Conversations access policies
CREATE POLICY "Users can manage their own conversations"
ON conversations
FOR ALL
TO authenticated
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
-- Messages access policies
CREATE POLICY "Users can manage their own messages"
ON messages
FOR ALL
TO authenticated
USING (auth.uid() = user_id)
WITH CHECK (auth.uid() = user_id);
-- -------------------------------------------------------------
-- TABLE: feedback
-- -------------------------------------------------------------
CREATE TABLE IF NOT EXISTS feedback (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
conversation_id TEXT REFERENCES conversations(id) ON DELETE CASCADE,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
message_id TEXT,
query TEXT,
rating TEXT NOT NULL, -- 'thumbs_up', 'thumbs_down', 'flag'
category TEXT, -- 'wrong_section', 'outdated_law', 'hallucination', 'incorrect_advice', 'other'
comment TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT now()
);
CREATE INDEX IF NOT EXISTS idx_feedback_created_at ON feedback(created_at);
ALTER TABLE feedback ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can insert their own feedback"
ON feedback
FOR INSERT
TO authenticated
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can view their own feedback"
ON feedback
FOR SELECT
TO authenticated
USING (auth.uid() = user_id);