Files
107e8c5c08 docs(db): point this repo at its own database, and gate the way back (#143)
The schema fix landed in #138, but the repo still told you to operate a
Supabase we do not have. docs/SUPABASE_SETUP.md opened with "Completed Setup"
for a hosted project jkjmhtirxwhljpkcfxqe.supabase.co and said to apply
migrations by pasting SQL into a supabase.com dashboard. Two unreferenced
scripts printed the same instruction, one handing out a psql string against
pooler.supabase.com, and the CLI's .temp cache with the old project ref was
committed — twice, once nested under supabase/migrations/.

That is not stale clutter, it is the outage. A wrong pointer to a real-looking
place is worse than no pointer: no pointer makes someone ask, a plausible one
makes them assume it was handled. Nobody pasted anything, the eleven migrations
were never applied, and /api/health served PGRST205 for months while every
deploy went green.

So the doc now describes what is actually true — self-hosted on bitbaum at
supabase.orangecat.ch, our tables in the `botsmann` schema because one database
backs several apps and `public` is orangecat's, and migrations applied
automatically on deploy by fleetcrown's apply-schema.sh against the
botsmann._deploy_schema_history ledger. Nothing is pasted anywhere. 280 lines
became 100.

Fixing today's copies only clears today's copies, so check:selfhost joins
verify: it fails on supabase.com/dashboard, app.supabase.com, pooler URLs and
real project refs, allows supabase.com/docs because the product documentation
is still correct for us, and exempts docs/archive/ so history can stay honest.
Proven to fail before shipping — it catches an injected dashboard link and a
project ref, and leaves the your-project.supabase.co placeholder alone.


Claude-Session: https://claude.ai/code/session_018waGt1ieA9TjpscqrbrnGb

Co-authored-by: Mao Nakamoto <41178744+maonakamoto@users.noreply.github.com>
Co-authored-by: Claude Opus 5 <noreply@anthropic.com>
2026-08-28 08:08:06 +00:00

165 lines
4.9 KiB
PL/PgSQL

-- Botsmann Initial Schema
-- Applied automatically on deploy into the `botsmann` schema; see docs/SUPABASE_SETUP.md.
-- Enable pgvector extension for future vector search
CREATE EXTENSION IF NOT EXISTS vector;
-- ============================================
-- PHASE 1: Consultations (migrate from MongoDB)
-- ============================================
CREATE TABLE IF NOT EXISTS consultations (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
name TEXT NOT NULL,
email TEXT NOT NULL,
message TEXT NOT NULL,
status TEXT DEFAULT 'new' CHECK (status IN ('new', 'contacted', 'resolved')),
created_at TIMESTAMPTZ DEFAULT NOW(),
updated_at TIMESTAMPTZ DEFAULT NOW()
);
-- Index for listing consultations
CREATE INDEX IF NOT EXISTS idx_consultations_created_at ON consultations(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_consultations_status ON consultations(status);
-- ============================================
-- PHASE 2: User Settings (for model selection)
-- ============================================
CREATE TABLE IF NOT EXISTS user_settings (
id UUID PRIMARY KEY REFERENCES auth.users(id) ON DELETE CASCADE,
preferred_model TEXT DEFAULT 'groq' CHECK (preferred_model IN ('groq', 'openai', 'ollama')),
groq_api_key TEXT,
openai_api_key TEXT,
ollama_url TEXT,
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Row-level security for user settings
ALTER TABLE user_settings ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can view own settings"
ON user_settings FOR SELECT
USING (auth.uid() = id);
CREATE POLICY "Users can update own settings"
ON user_settings FOR UPDATE
USING (auth.uid() = id);
CREATE POLICY "Users can insert own settings"
ON user_settings FOR INSERT
WITH CHECK (auth.uid() = id);
-- ============================================
-- PHASE 3: Documents and Vector Search
-- ============================================
CREATE TABLE IF NOT EXISTS documents (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
name TEXT NOT NULL,
type TEXT NOT NULL,
size_bytes INTEGER,
storage_path TEXT NOT NULL,
created_at TIMESTAMPTZ DEFAULT NOW()
);
CREATE INDEX IF NOT EXISTS idx_documents_user_id ON documents(user_id);
-- Document chunks with vector embeddings
CREATE TABLE IF NOT EXISTS document_chunks (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
document_id UUID REFERENCES documents(id) ON DELETE CASCADE,
user_id UUID REFERENCES auth.users(id) ON DELETE CASCADE,
content TEXT NOT NULL,
embedding VECTOR(384), -- MiniLM-L6-v2 produces 384-dimensional vectors
metadata JSONB DEFAULT '{}',
created_at TIMESTAMPTZ DEFAULT NOW()
);
-- Vector similarity search index (IVFFlat for performance)
-- Only create if we have chunks (run separately after data exists)
-- CREATE INDEX IF NOT EXISTS idx_document_chunks_embedding
-- ON document_chunks
-- USING ivfflat (embedding vector_cosine_ops)
-- WITH (lists = 100);
CREATE INDEX IF NOT EXISTS idx_document_chunks_user_id ON document_chunks(user_id);
CREATE INDEX IF NOT EXISTS idx_document_chunks_document_id ON document_chunks(document_id);
-- Row-level security for documents
ALTER TABLE documents ENABLE ROW LEVEL SECURITY;
ALTER TABLE document_chunks ENABLE ROW LEVEL SECURITY;
CREATE POLICY "Users can view own documents"
ON documents FOR SELECT
USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own documents"
ON documents FOR INSERT
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can delete own documents"
ON documents FOR DELETE
USING (auth.uid() = user_id);
CREATE POLICY "Users can view own chunks"
ON document_chunks FOR SELECT
USING (auth.uid() = user_id);
CREATE POLICY "Users can insert own chunks"
ON document_chunks FOR INSERT
WITH CHECK (auth.uid() = user_id);
CREATE POLICY "Users can delete own chunks"
ON document_chunks FOR DELETE
USING (auth.uid() = user_id);
-- ============================================
-- Helper Functions
-- ============================================
-- Function to search for similar document chunks
CREATE OR REPLACE FUNCTION match_documents(
query_embedding VECTOR(384),
match_count INT DEFAULT 5,
filter_user_id UUID DEFAULT NULL
)
RETURNS TABLE (
id UUID,
document_id UUID,
content TEXT,
metadata JSONB,
similarity FLOAT
)
LANGUAGE plpgsql
AS $$
BEGIN
RETURN QUERY
SELECT
dc.id,
dc.document_id,
dc.content,
dc.metadata,
1 - (dc.embedding <=> query_embedding) AS similarity
FROM document_chunks dc
WHERE (filter_user_id IS NULL OR dc.user_id = filter_user_id)
ORDER BY dc.embedding <=> query_embedding
LIMIT match_count;
END;
$$;
-- Auto-update updated_at timestamp
CREATE OR REPLACE FUNCTION update_updated_at()
RETURNS TRIGGER AS $$
BEGIN
NEW.updated_at = NOW();
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
CREATE TRIGGER update_consultations_updated_at
BEFORE UPDATE ON consultations
FOR EACH ROW
EXECUTE FUNCTION update_updated_at();