diff --git a/knoe-db/schema/migrations/001_prole_vector_schema.sql b/knoe-db/schema/migrations/001_prole_vector_schema.sql new file mode 100644 index 0000000..c17cb4b --- /dev/null +++ b/knoe-db/schema/migrations/001_prole_vector_schema.sql @@ -0,0 +1,99 @@ +-- Migration 001 — prole ekosystem vector schema +-- Tenant: prole (tenant_id=1, uuid=000necda5b3a6tc2) +-- Project embedding store UUID: 000nectf23m7865j (minted with tenant_id=1) +-- Embedding model: mxbai-embed-large-v1 (dim=1024) +-- +-- Run from ~/dev/prole/knoe-db: +-- psql "$DSN" -v ON_ERROR_STOP=1 -f schema/migrations/001_prole_vector_schema.sql + +BEGIN; + +-- Drop placeholder schema created before project UUID was minted +DROP SCHEMA IF EXISTS "p_000necda5b3a6tc2" CASCADE; +DROP ROLE IF EXISTS "u_000necda5b3a6tc2"; + +-- Service account +DO $$ BEGIN + IF NOT EXISTS (SELECT FROM pg_roles WHERE rolname = 'u_000nectf23m7865j') THEN + CREATE ROLE "u_000nectf23m7865j" + LOGIN NOSUPERUSER NOCREATEDB NOCREATEROLE INHERIT; + END IF; +END $$; + +-- Project namespace +CREATE SCHEMA IF NOT EXISTS "p_000nectf23m7865j" AUTHORIZATION knoe; +GRANT USAGE ON SCHEMA "p_000nectf23m7865j" TO "u_000nectf23m7865j"; + +-- Chunks: raw indexed text from repositories +CREATE TABLE IF NOT EXISTS "p_000nectf23m7865j".chunks ( + ekosystem_uuid text NOT NULL DEFAULT '000nectf23m7865j', + id text NOT NULL, + repo text NOT NULL, + path text NOT NULL, + lang text, + line_start integer, + line_end integer, + content text NOT NULL, + header text NOT NULL DEFAULT '', + contributor_id text NOT NULL DEFAULT 'system', + indexed_at timestamptz NOT NULL DEFAULT now(), + PRIMARY KEY (id) +); +CREATE INDEX IF NOT EXISTS idx_chunks_repo + ON "p_000nectf23m7865j".chunks (repo, path); +CREATE INDEX IF NOT EXISTS idx_chunks_contributor + ON "p_000nectf23m7865j".chunks (contributor_id); + +-- Per-contributor embeddings +CREATE TABLE IF NOT EXISTS "p_000nectf23m7865j".embeddings ( + ekosystem_uuid text NOT NULL DEFAULT '000nectf23m7865j', + id bigserial PRIMARY KEY, + chunk_id text NOT NULL + REFERENCES "p_000nectf23m7865j".chunks(id) ON DELETE CASCADE, + contributor_id text NOT NULL DEFAULT 'system', + embedding vector(1024) NOT NULL, + created_at timestamptz NOT NULL DEFAULT now() +); + +-- Merged canonical embeddings — searched by MCP +CREATE TABLE IF NOT EXISTS "p_000nectf23m7865j".embeddings_merged ( + ekosystem_uuid text NOT NULL DEFAULT '000nectf23m7865j', + id bigserial PRIMARY KEY, + chunk_id text NOT NULL + REFERENCES "p_000nectf23m7865j".chunks(id) ON DELETE CASCADE, + embedding vector(1024) NOT NULL, + source_contributor_id text NOT NULL DEFAULT 'system', + merged_at timestamptz NOT NULL DEFAULT now() +); +CREATE INDEX IF NOT EXISTS idx_merged_hnsw + ON "p_000nectf23m7865j".embeddings_merged + USING hnsw (embedding vector_cosine_ops) + WITH (m = 16, ef_construction = 64); + +-- Grants +GRANT SELECT, INSERT, UPDATE, DELETE + ON ALL TABLES IN SCHEMA "p_000nectf23m7865j" TO knoe; +GRANT USAGE, SELECT + ON ALL SEQUENCES IN SCHEMA "p_000nectf23m7865j" TO knoe; + +-- Cross-ekosystem grant: tenant 0 (knoe.dev MCP) can search this store +SELECT knoe.grant_object( + '000nectf23m7865j', + 'embedding', + 0::smallint, + '{SELECT}'::text[], + NULL +); + +COMMIT; + +-- Verify +SELECT (d).tenant_id AS owner_tenant, '000nectf23m7865j' AS project_uuid +FROM (SELECT knoe.ekosystem_decode('000nectf23m7865j') d) x; + +SELECT direction, object_uuid, object_kind, privileges +FROM knoe.my_grants(1::smallint); + +SELECT schemaname, tablename +FROM pg_tables WHERE schemaname = 'p_000nectf23m7865j' +ORDER BY tablename;