Skip to content

Commit ae18bda

Browse files
committed
chore(search): renumber the projection retirement migration to 0449 after staging's 0448
1 parent e404119 commit ae18bda

6 files changed

Lines changed: 31897 additions & 6 deletions

File tree

‎packages/db/knowledge-projection.test.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import { describe, expect, it } from 'vitest'
55
const normalized = (text: string) => text.replace(/\s+/g, ' ').trim()
66

77
describe('sync_embedding_search installers', () => {
8-
it('keeps migration 0448 and the provisioning paths on the same function', () => {
8+
it('keeps migration 0449 and the provisioning paths on the same function', () => {
99
const migration = readFileSync(
10-
new URL('./migrations/0448_retire_search_projection_writes.sql', import.meta.url),
10+
new URL('./migrations/0449_retire_search_projection_writes.sql', import.meta.url),
1111
'utf8'
1212
)
1313
const statement = migration.slice(migration.indexOf('CREATE OR REPLACE FUNCTION'))

‎packages/db/knowledge-projection.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ function searchVectorShortened(model: string, prefix: string): string {
101101

102102
/**
103103
* The `embedding_search_sync` trigger function: projects an ordinary-KB chunk and deletes the row
104-
* of a Search-KB chunk. Migration 0448 installs this exact body; fresh databases and pushes install
104+
* of a Search-KB chunk. Migration 0449 installs this exact body; fresh databases and pushes install
105105
* it through `0016_backfill_search_vectors`.
106106
*
107107
* contract-pending(after Search KB candidate rows are purged and Search KBs are unwritable): drop
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
-- Retired Search knowledge bases (is_search_index) no longer get candidate rows: a chunk write
2+
-- there deletes the chunk's row instead of projecting it. Ordinary knowledge bases project exactly
3+
-- as before. Replacing the function takes no table lock and replays idempotently; the
4+
-- embedding_search_sync trigger, its WHEN guard and the source/ACL trigger are unchanged. The body
5+
-- must stay identical to SYNC_EMBEDDING_SEARCH_FUNCTION in packages/db/knowledge-projection.ts.
6+
CREATE OR REPLACE FUNCTION "public"."sync_embedding_search"()
7+
RETURNS trigger LANGUAGE plpgsql AS $$
8+
DECLARE
9+
search_index boolean;
10+
shortened boolean;
11+
BEGIN
12+
SELECT is_search_index, embedding_model IN ('text-embedding-3-small', 'text-embedding-3-large') AND NEW.embedding_384 IS NULL
13+
INTO STRICT search_index, shortened
14+
FROM knowledge_base WHERE id = NEW.knowledge_base_id;
15+
IF search_index THEN
16+
DELETE FROM embedding_search WHERE id = NEW.id;
17+
RETURN NEW;
18+
END IF;
19+
INSERT INTO embedding_search
20+
(id, knowledge_base_id, document_id, enabled, "binary", binary_384, binary_768, binary_1024, binary_3072,
21+
vector, vector_384, vector_512, vector_768, vector_1024, vector_3072)
22+
VALUES (NEW.id, NEW.knowledge_base_id, NEW.document_id, NEW.enabled,
23+
binary_quantize(NEW.embedding)::bit(1536), binary_quantize(NEW.embedding_384)::bit(384),
24+
binary_quantize(NEW.embedding_768)::bit(768), binary_quantize(NEW.embedding_1024)::bit(1024),
25+
binary_quantize(NEW.embedding_3072)::bit(3072),
26+
CASE WHEN NOT (shortened) THEN NEW.embedding::halfvec(1536) END,
27+
CASE WHEN NOT (shortened) THEN NEW.embedding_384::halfvec(384) END,
28+
CASE WHEN shortened THEN subvector(coalesce(NEW.embedding, NEW.embedding_384, NEW.embedding_768, NEW.embedding_1024, NEW.embedding_3072), 1, 512)::halfvec(512) END,
29+
CASE WHEN NOT (shortened) THEN NEW.embedding_768::halfvec(768) END,
30+
CASE WHEN NOT (shortened) THEN NEW.embedding_1024::halfvec(1024) END,
31+
CASE WHEN NOT (shortened) THEN NEW.embedding_3072::halfvec(3072) END)
32+
ON CONFLICT (id) DO UPDATE SET
33+
knowledge_base_id = EXCLUDED.knowledge_base_id, document_id = EXCLUDED.document_id,
34+
enabled = EXCLUDED.enabled, "binary" = EXCLUDED."binary", binary_384 = EXCLUDED.binary_384,
35+
binary_768 = EXCLUDED.binary_768, binary_1024 = EXCLUDED.binary_1024,
36+
binary_3072 = EXCLUDED.binary_3072,
37+
vector = EXCLUDED.vector, vector_384 = EXCLUDED.vector_384, vector_512 = EXCLUDED.vector_512,
38+
vector_768 = EXCLUDED.vector_768, vector_1024 = EXCLUDED.vector_1024, vector_3072 = EXCLUDED.vector_3072;
39+
RETURN NEW;
40+
END;
41+
$$;

0 commit comments

Comments
 (0)