Spaces:
Running
Running
File size: 9,519 Bytes
9c56a9c 5bf93ec 9c56a9c | 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | """
The database: one Postgres (with the pgvector extension) for everything Study AI stores.
- Folders, file text, decks, usage and feedback are ordinary tables (SCHEMA). Accounts, passwords and
sign-ins belong to Neon Auth (its own neon_auth schema); `users` keeps Study AI's details per account.
- Note passages and their search fingerprints are two more tables, data_notes and data_library,
created and searched through LlamaIndex (see backend/pipeline/indexing.py).
DATABASE_URL points at it, e.g. postgresql://user:password@host/dbname?sslmode=require
(Neon and Supabase both offer a free Postgres with pgvector).
"""
import logging
import threading
from contextlib import contextmanager
from sqlalchemy import create_engine, text
from sqlalchemy.engine import Connection, Engine
from backend import settings
logger = logging.getLogger(__name__)
# Keep in step with usage.COUNTERS (one column per daily counter).
SCHEMA = """
CREATE EXTENSION IF NOT EXISTS vector;
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY, -- the Neon Auth user id
email TEXT NOT NULL UNIQUE,
name TEXT NOT NULL DEFAULT '',
email_verified BOOLEAN NOT NULL DEFAULT FALSE,
created_at BIGINT NOT NULL,
meta JSONB NOT NULL DEFAULT '{}'::jsonb -- preferences (pref_*), privacy acceptance, ...
);
CREATE TABLE IF NOT EXISTS folders (
user_id TEXT PRIMARY KEY,
tree JSONB NOT NULL DEFAULT '[]'::jsonb, -- the folder tree shown in the explorer
file_meta JSONB NOT NULL DEFAULT '{}'::jsonb -- per-file details, e.g. "main source"
);
CREATE TABLE IF NOT EXISTS documents (
user_id TEXT NOT NULL,
path TEXT NOT NULL,
original_filename TEXT NOT NULL DEFAULT '',
content TEXT NOT NULL DEFAULT '', -- the full text read from the file
truncated BOOLEAN NOT NULL DEFAULT FALSE,
updated_at BIGINT NOT NULL,
original_key TEXT NOT NULL DEFAULT '', -- the original PDF/photo in file storage ('' = none)
original_type TEXT NOT NULL DEFAULT '',
original_size BIGINT NOT NULL DEFAULT 0,
PRIMARY KEY (user_id, path)
);
CREATE TABLE IF NOT EXISTS decks (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
name TEXT NOT NULL,
source_path TEXT NOT NULL DEFAULT '',
created_at BIGINT NOT NULL
);
CREATE INDEX IF NOT EXISTS decks_user_id ON decks (user_id);
CREATE TABLE IF NOT EXISTS cards (
id TEXT PRIMARY KEY,
deck_id TEXT NOT NULL REFERENCES decks (id) ON DELETE CASCADE,
user_id TEXT NOT NULL,
position INTEGER NOT NULL DEFAULT 0,
front TEXT NOT NULL,
back TEXT NOT NULL,
source TEXT NOT NULL DEFAULT 'notes',
due_at BIGINT NOT NULL,
interval_days DOUBLE PRECISION NOT NULL DEFAULT 0,
ease DOUBLE PRECISION NOT NULL DEFAULT 2.5,
reps INTEGER NOT NULL DEFAULT 0,
lapses INTEGER NOT NULL DEFAULT 0
);
CREATE INDEX IF NOT EXISTS cards_user_due ON cards (user_id, due_at);
CREATE INDEX IF NOT EXISTS cards_deck_id ON cards (deck_id);
CREATE TABLE IF NOT EXISTS quiz_results (
user_id TEXT NOT NULL,
path TEXT NOT NULL,
correct INTEGER NOT NULL DEFAULT 0,
total INTEGER NOT NULL DEFAULT 0,
updated_at BIGINT NOT NULL,
PRIMARY KEY (user_id, path)
);
CREATE TABLE IF NOT EXISTS exams (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
course TEXT NOT NULL,
exam_date DATE NOT NULL,
plan JSONB NOT NULL,
created_at BIGINT NOT NULL
);
CREATE INDEX IF NOT EXISTS exams_user_id ON exams (user_id);
CREATE TABLE IF NOT EXISTS usage_daily (
user_id TEXT NOT NULL,
day DATE NOT NULL,
questions BIGINT NOT NULL DEFAULT 0,
uploads BIGINT NOT NULL DEFAULT 0,
pages_read BIGINT NOT NULL DEFAULT 0,
upload_failures BIGINT NOT NULL DEFAULT 0,
study_requests BIGINT NOT NULL DEFAULT 0,
reviews BIGINT NOT NULL DEFAULT 0,
prompt_tokens BIGINT NOT NULL DEFAULT 0,
completion_tokens BIGINT NOT NULL DEFAULT 0,
ratings_up BIGINT NOT NULL DEFAULT 0,
ratings_down BIGINT NOT NULL DEFAULT 0,
PRIMARY KEY (user_id, day)
);
CREATE INDEX IF NOT EXISTS usage_daily_day ON usage_daily (day);
CREATE TABLE IF NOT EXISTS feedback (
id TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
kind TEXT NOT NULL,
rating TEXT NOT NULL DEFAULT '',
selection_stage TEXT NOT NULL DEFAULT '',
comment TEXT NOT NULL DEFAULT '',
question TEXT NOT NULL DEFAULT '',
answer TEXT NOT NULL DEFAULT '',
sources TEXT NOT NULL DEFAULT '',
created_at BIGINT NOT NULL
);
CREATE INDEX IF NOT EXISTS feedback_created_at ON feedback (created_at);
CREATE INDEX IF NOT EXISTS feedback_user_id ON feedback (user_id);
CREATE TABLE IF NOT EXISTS onenote_tokens (
user_id TEXT PRIMARY KEY,
token JSONB NOT NULL,
expires_at BIGINT NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS onenote_oauth_states (
state TEXT PRIMARY KEY,
user_id TEXT NOT NULL,
expires_at BIGINT NOT NULL
);
CREATE TABLE IF NOT EXISTS library_files (
path TEXT PRIMARY KEY,
subject TEXT NOT NULL,
etag TEXT NOT NULL DEFAULT '',
status TEXT NOT NULL, -- ok | empty | failed | pending
details JSONB NOT NULL DEFAULT '{}'::jsonb, -- size, pages, OCR counts, method, error, ...
content TEXT NOT NULL DEFAULT '', -- full text, used by the study tools
updated_at BIGINT NOT NULL
);
"""
APP_TABLES = (
"cards", "decks", "quiz_results", "exams", "usage_daily", "feedback",
"onenote_tokens", "onenote_oauth_states", "documents", "folders", "library_files", "users",
)
_engine: Engine | None = None
_async_engine = None
_engine_lock = threading.Lock()
_init_lock = threading.Lock() # separate from _engine_lock: init() creates the engine while holding it
_ready = False
def _url(driver: str) -> str:
raw = settings.DATABASE_URL
if not raw:
raise RuntimeError("DATABASE_URL is not set. See README.md (Neon: database, sign-in and file storage).")
for prefix in ("postgresql+psycopg2://", "postgresql+asyncpg://", "postgresql://", "postgres://"):
if raw.startswith(prefix):
return f"postgresql+{driver}://" + raw[len(prefix):]
raise RuntimeError("DATABASE_URL must start with postgresql:// (or postgres://).")
def engine() -> Engine:
"""The shared connection pool (created on first use)."""
global _engine
if _engine is None:
with _engine_lock:
if _engine is None:
# pre_ping: free hosts (Neon, Supabase) close idle connections; check before reuse.
_engine = create_engine(
_url("psycopg2"), pool_pre_ping=True, pool_size=5, max_overflow=10, pool_recycle=1800
)
return _engine
def async_engine():
"""LlamaIndex's vector store wants an async engine as well; Study AI itself never uses it."""
global _async_engine
if _async_engine is None:
with _engine_lock:
if _async_engine is None:
from sqlalchemy.ext.asyncio import create_async_engine
url = _url("asyncpg").split("?", 1)[0] # asyncpg doesn't take libpq options like sslmode
_async_engine = create_async_engine(url)
return _async_engine
def init() -> None:
"""Create the pgvector extension and the tables if they don't exist yet (safe to repeat)."""
global _ready
if _ready:
return
with _init_lock:
if _ready:
return
with engine().begin() as conn:
conn.exec_driver_sql(SCHEMA)
_ready = True
@contextmanager
def transaction():
"""A connection whose statements all succeed or all roll back together."""
init()
with engine().begin() as conn:
yield conn
def execute(sql: str, conn: Connection | None = None, **params) -> int:
"""Run a statement; returns the number of rows it changed."""
if conn is not None:
return conn.execute(text(sql), params).rowcount
with transaction() as c:
return c.execute(text(sql), params).rowcount
def rows(sql: str, conn: Connection | None = None, **params) -> list[dict]:
if conn is not None:
return [dict(r._mapping) for r in conn.execute(text(sql), params)]
with transaction() as c:
return [dict(r._mapping) for r in c.execute(text(sql), params)]
def one(sql: str, conn: Connection | None = None, **params) -> dict | None:
found = rows(sql, conn, **params)
return found[0] if found else None
def scalar(sql: str, conn: Connection | None = None, **params):
found = one(sql, conn, **params)
return next(iter(found.values())) if found else None
def table_exists(name: str) -> bool:
return bool(scalar("SELECT to_regclass(:name) IS NOT NULL AS ok", name=f"public.{name}"))
def empty_all_tables() -> None:
"""Delete every row (tests use this between cases). Passage tables are emptied too."""
init()
extra = [t for t in ("data_notes", "data_library") if table_exists(t)]
with engine().begin() as conn:
conn.exec_driver_sql("TRUNCATE " + ", ".join((*APP_TABLES, *extra)) + " CASCADE")
|