Skip to content

Supabase (Relational DB) State

Database Schema Overview

The relational database is hosted on Supabase (PostgreSQL). The schema is managed via SQL migrations.

Core Tables

  1. profiles: Maps Supabase Auth users to roles.
  2. wallet: Stores token_balance and subscription_tier.
  3. wallet_ledger: Immutable audit log of all token transactions.
  4. usage_logs: Logs metadata for every RAG interaction (Question, Namespace, Sources used).
  5. references: Catalog of curriculum PDFs discovered by scrapers.
  6. scrape_runs: History of scraper executions.
  7. documents: Tracked PDFs and their metadata.
  8. chunks: Text chunks from PDFs (linked to Pinecone vectors).
  9. personas: AI Teacher persona definitions.

Migration History

  • ...001_curriculum_document_tracking.sql: Basic doc/chunk tracking.
  • ...002_usage_logs.sql: Interaction logging.
  • ...003_wallet_system.sql: Ledger and balances.
  • ...004_create_scrape_runs.sql: Scraping audit.
  • ...005_create_references.sql: Portals catalog.
  • ...006_rls_phase_1.sql: RLS activation and retrieval query logging.
  • ...007_profiles_trigger.sql: Auth trigger for new users.
  • ...008_pedagogical_tables.sql: Pedagogy and personas.
  • ...009_rls_phase_2_public_tables.sql: RLS enabled on all remaining public tables.
  • ...010_secure_functions.sql: Security hardening for functions (search_path).
  • ...011_indexes.sql: Performance indexes for FKs.

RLS (Row Level Security) Status

✅ CURRENT STATUS: PHASE 2 COMPLETE (STRICT)

As of Feb 16, 2026, RLS is enabled on ALL public tables.

User-Specific Tables (Authenticated Access)

  • Tables: profiles, wallet, wallet_ledger, usage_logs.
  • Policy:
  • Users can SELECT their own rows (based on auth.uid()).
  • No public/anonymous access allowed.

System/Admin Tables (Service Role Only)

  • Tables: documents, chunks, scrape_runs, references, personas.
  • Policy:
  • No public/anonymous access.
  • No authenticated user access (students cannot read these directly; they interact via the API).
  • Service Role / Admin: Full access via explicit policies or bypass (backend uses service_role key).

Function Security

  • handle_new_user: SECURITY DEFINER with fixed search_path = public, auth, extensions to prevent privilege escalation attacks.

Back to Index