From ecf23bad29953f38cf390fec5704e147a2a26fec Mon Sep 17 00:00:00 2001 From: ZTatman Date: Sat, 28 Mar 2026 19:03:47 -0400 Subject: [PATCH] Address PR nitpicks: lru_cache pattern, updated_at trigger, missing imports - Align NEW_PLAN.md db.py example to use @lru_cache(maxsize=1) for both get_supabase() and get_embedding_model(), matching actual implementation - Add missing imports (datetime, HTTPException, get_supabase) to check_ai_limit code example in NEW_PLAN.md - Add migration to auto-update entry_analysis.updated_at on row modification via a BEFORE UPDATE trigger Co-Authored-By: Claude Sonnet 4.6 --- NEW_PLAN.md | 18 ++++++++++-------- ...00000_entry_analysis_updated_at_trigger.sql | 13 +++++++++++++ 2 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 supabase/migrations/20260328000000_entry_analysis_updated_at_trigger.sql diff --git a/NEW_PLAN.md b/NEW_PLAN.md index 8384891..35ab73d 100644 --- a/NEW_PLAN.md +++ b/NEW_PLAN.md @@ -581,6 +581,7 @@ Imported by store.py, query.py, and main.py """ import os +from functools import lru_cache from dotenv import load_dotenv from supabase import create_client, Client from sentence_transformers import SentenceTransformer @@ -589,8 +590,9 @@ load_dotenv() # ── Supabase client ─────────────────────────────────────────────────────────── +@lru_cache(maxsize=1) def get_supabase() -> Client: - """Returns authenticated Supabase client using the service key.""" + """Return the cached Supabase client (create once, reuse).""" url = os.environ["SUPABASE_URL"] key = os.environ["SUPABASE_SERVICE_KEY"] # service key for server-side ops return create_client(url, key) @@ -600,15 +602,11 @@ def get_storage(): return get_supabase().storage.from_("recordings") # ── Embedding model ─────────────────────────────────────────────────────────── -# Downloads ~80MB on first run, cached locally after that - -_model = None +@lru_cache(maxsize=1) def get_embedding_model() -> SentenceTransformer: - global _model - if _model is None: - _model = SentenceTransformer("all-MiniLM-L6-v2") - return _model + """Return the cached embedding model (downloads ~80 MB on first run).""" + return SentenceTransformer("all-MiniLM-L6-v2") def embed(text: str) -> list[float]: """Convert a string to a 384-dimensional embedding vector.""" @@ -746,6 +744,10 @@ async def revenuecat_webhook(request: Request): Add this check to every FastAPI endpoint that calls an LLM. ```python +from datetime import datetime +from fastapi import HTTPException +from db import get_supabase + def check_ai_limit(user_id: str, call_type: str): """ Raises HTTP 429 if user has exceeded their monthly AI limit for this call type. diff --git a/supabase/migrations/20260328000000_entry_analysis_updated_at_trigger.sql b/supabase/migrations/20260328000000_entry_analysis_updated_at_trigger.sql new file mode 100644 index 0000000..66abe8a --- /dev/null +++ b/supabase/migrations/20260328000000_entry_analysis_updated_at_trigger.sql @@ -0,0 +1,13 @@ +-- Auto-update entry_analysis.updated_at whenever a row is modified. + +create or replace function public.set_updated_at() +returns trigger as $$ +begin + new.updated_at = now(); + return new; +end; +$$ language plpgsql; + +create trigger trg_entry_analysis_updated_at + before update on public.entry_analysis + for each row execute function public.set_updated_at();