Skip to content

Commit 404e365

Browse files
committed
test(search): prove retired documents never spend the retirement row limit
A run of already-retired Search documents longer than the row limit must be crossed in one page. The test counts documents-phase statements and fails if the page filter on unretired rows is removed.
1 parent a921532 commit 404e365

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

‎packages/db/script-migrations/0027_retire_search_embeddings.integration.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,38 @@ describe('retiring dormant Search embeddings', () => {
390390
}
391391
}, 60_000)
392392

393+
it('scans past a run of already-retired documents longer than the row limit in one page', async () => {
394+
/** 6,000 retired Search documents exceed the initial 2,000-row limit but fit one 25,000-ID scan. */
395+
await sql`INSERT INTO document (id, knowledge_base_id, user_excluded, enabled)
396+
SELECT 'doc-' || lpad(i::text, 5, '0'), 'search', true, false FROM generate_series(1, 6000) i`
397+
await sql`INSERT INTO document (id, knowledge_base_id)
398+
SELECT 'doc-' || lpad(i::text, 5, '0'), 'search' FROM generate_series(6001, 6010) i`
399+
await sql`CREATE SEQUENCE document_page_statements`
400+
await sql`CREATE FUNCTION count_document_page() RETURNS trigger LANGUAGE plpgsql AS $$
401+
BEGIN
402+
PERFORM nextval('document_page_statements');
403+
RETURN NULL;
404+
END $$`
405+
/** Statement triggers fire even for zero rows, so this counts every documents-phase page. */
406+
await sql`CREATE TRIGGER count_document_page AFTER UPDATE ON document
407+
FOR EACH STATEMENT EXECUTE FUNCTION count_document_page()`
408+
try {
409+
expect(await pass()).toBe(true)
410+
/** One page retires the 11 unretired rows (10 bulk plus `search-doc`); one more finds the end. */
411+
expect((await sql`SELECT last_value::int AS n FROM document_page_statements`)[0].n).toBe(2)
412+
expect(
413+
(
414+
await sql`SELECT count(*)::int AS n FROM document
415+
WHERE knowledge_base_id = 'search' AND (NOT user_excluded OR enabled)`
416+
)[0].n
417+
).toBe(0)
418+
} finally {
419+
await sql`DROP TRIGGER IF EXISTS count_document_page ON document`
420+
await sql`DROP FUNCTION count_document_page()`
421+
await sql`DROP SEQUENCE document_page_statements`
422+
}
423+
})
424+
393425
it('fails at once on a timeout outside the page mutation instead of shrinking the page', async () => {
394426
await sql`CREATE SEQUENCE completion_attempts`
395427
/** Times out the completion checkpoint, a statement no smaller row limit can speed up. */

0 commit comments

Comments
 (0)