From 5aacee46d8b14d0284c4bcfa204ba7ee8e2f33de Mon Sep 17 00:00:00 2001 From: chrisfu Date: Sat, 30 May 2026 02:25:17 -0700 Subject: [PATCH] =?UTF-8?q?feat(migrations):=20001=20=E2=80=94=20prole=20e?= =?UTF-8?q?kosystem=20vector=20schema?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Prole registered as tenant_id=1, uuid=000necda5b3a6tc2 (DNS anchor) - Project embedding store uuid=000nectf23m7865j (tenant_id=1 in bits) - Schema p_000nectf23m7865j: chunks, embeddings, embeddings_merged (vector 1024) - HNSW index on embeddings_merged for cosine similarity search - Cross-grant issued to tenant 0 (knoe.dev MCP): grant id=000neddhjsr8eadg - Establishes knoe-db/schema/migrations/ directory (sqitch wiring: tomorrow) Applied live to pg.prole.org at 2026-05-30 ~02:25 UTC-7. Co-Authored-By: Claude Sonnet 4.5 --- .../migrations/001_prole_vector_schema.sql | 99 +++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 knoe-db/schema/migrations/001_prole_vector_schema.sql 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;