Spaces:
Sleeping

flint / scripts /schema.sql
whos-praja's picture
Deploy Flint
1cc47fc verified
Raw History Blame Contribute Delete
2.67 kB
-- Flint Phase 1 schema. Idempotent — safe to run multiple times.
-- pgvector extension
create extension if not exists vector;
-- Ideas table
create table if not exists ideas (
id uuid primary key default gen_random_uuid(),
created_at timestamptz default now(),
raw_text text not null, -- original text (or transcription / photo description)
summary text, -- 3-word AI summary
bucket text, -- one of the 7 buckets
source text default 'text', -- 'text' | 'voice' | 'photo'
expanded_plan text, -- filled when /expand is used
visited boolean default false, -- for brief tracking
embedding vector(384) -- sentence-transformers all-MiniLM-L6-v2
);
-- Approximate-NN index for cosine similarity
create index if not exists ideas_embedding_idx
on ideas using ivfflat (embedding vector_cosine_ops) with (lists = 100);
-- Cosine-similarity search, exposed as an RPC because the Supabase anon client
-- cannot run raw SQL. Called by storage.get_similar_ideas / storage.search_ideas.
create or replace function match_ideas(
query_embedding vector(384),
match_count int default 5,
similarity_threshold float default 0.0
)
returns table (
id uuid,
raw_text text,
summary text,
bucket text,
source text,
created_at timestamptz,
similarity float
)
language sql stable
as $$
select
i.id,
i.raw_text,
i.summary,
i.bucket,
i.source,
i.created_at,
1 - (i.embedding <=> query_embedding) as similarity
from ideas i
where i.embedding is not null
and 1 - (i.embedding <=> query_embedding) >= similarity_threshold
order by i.embedding <=> query_embedding
limit match_count;
$$;
-- ---------------------------------------------------------------------------
-- API access. This app is solo-user with no auth, and the Supabase project's
-- "Automatically expose new tables" is off — so grant the Data API roles
-- (anon / authenticated) access to the table + search function explicitly,
-- and keep RLS off. Guarded so the same SQL still runs on a plain Postgres
-- (e.g. a local Docker pgvector) where these roles don't exist.
do $$
begin
if exists (select 1 from pg_roles where rolname = 'anon') then
grant usage on schema public to anon, authenticated;
grant select, insert, update, delete on table ideas to anon, authenticated;
grant execute on all functions in schema public to anon, authenticated;
end if;
end $$;
alter table ideas disable row level security;