// PEP-214: горячие запросы идут по индексам; поиск настроек по префиксу — диапазоном по ключу. import assert from "node:assert/strict"; import { after, describe, test } from "node:test"; import { db, teardown } from "./helpers.ts"; import { keyPrefix, settings } from "@peppa/db"; after(teardown); const plan = (sql: string) => (db.$client.db.prepare(`explain query plan ${sql}`).all() as { detail: string }[]).map((r) => r.detail).join(" | "); describe("Индексы горячих запросов", () => { test("Scenario: события проекта по времени — поиск по индексу", () => { assert.match(plan("select * from events where project_id = 1 order by created_at desc limit 50"), /SEARCH events USING INDEX events_project_created/); }); test("Scenario Outline: частые фильтры не просматривают таблицу целиком", () => { const cases: [string, RegExp][] = [ ["select * from events where task_id = 5 and type = 'x'", /events_task_type/], ["select * from questions where project_id = 1 and answered_at is null", /questions_project_answered/], ["select * from questions where task_id = 5", /questions_task/], ["select * from sessions where task_id = 5 and status = 'idle'", /sessions_task_status/], ["select * from sessions where status = 'idle'", /sessions_status/], ["select * from chat_messages where chat_id = 1 and thread_key = 'task:5' order by id desc", /chat_messages_thread/], ["select * from tasks where project_id = 1 and status = 'review' order by position", /tasks_project_status_pos/], ["select * from check_runs where task_id = 5 and sha = 'a'", /check_runs_task_sha/], ]; for (const [q, re] of cases) assert.match(plan(q), re, q); }); }); describe("Настройки по префиксу", () => { test("Scenario: только ключи с префиксом, без масок LIKE", async () => { for (const key of ["pep214.paused:1", "pep214.paused:2", "pep214.pausedX", "pep214_paused:3", "PEP214.PAUSED:4"]) { await db.insert(settings).values({ key, value: "1" }).onConflictDoNothing(); } const rows = await db.select().from(settings).where(keyPrefix(settings.key, "pep214.paused:")); assert.deepEqual(rows.map((r) => r.key).sort(), ["pep214.paused:1", "pep214.paused:2"]); }); test("Scenario: поиск по префиксу идёт по ключу", () => { assert.match(plan("select * from settings where key >= 'a:' and key < 'a;'"), /SEARCH settings USING (COVERING )?INDEX/); }); });