Skip to content

Commit fab76d4

Browse files
authored
fix(workboard): index card child records (#129568)
1 parent fee6a75 commit fab76d4

2 files changed

Lines changed: 136 additions & 1 deletion

File tree

‎extensions/workboard/src/sqlite-store.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,8 @@ const WORKBOARD_SCHEMA_SQL = `
209209
session_key TEXT,
210210
run_id TEXT
211211
) STRICT;
212+
CREATE INDEX IF NOT EXISTS workboard_card_events_card_idx
213+
ON workboard_card_events(card_id, ordinal);
212214
213215
CREATE TABLE IF NOT EXISTS workboard_card_attempts (
214216
id TEXT PRIMARY KEY,
@@ -224,6 +226,8 @@ const WORKBOARD_SCHEMA_SQL = `
224226
run_id TEXT,
225227
error TEXT
226228
) STRICT;
229+
CREATE INDEX IF NOT EXISTS workboard_card_attempts_card_idx
230+
ON workboard_card_attempts(card_id, ordinal);
227231
228232
CREATE TABLE IF NOT EXISTS workboard_card_comments (
229233
id TEXT PRIMARY KEY,
@@ -233,6 +237,8 @@ const WORKBOARD_SCHEMA_SQL = `
233237
created_at INTEGER NOT NULL,
234238
updated_at INTEGER
235239
) STRICT;
240+
CREATE INDEX IF NOT EXISTS workboard_card_comments_card_idx
241+
ON workboard_card_comments(card_id, ordinal);
236242
237243
CREATE TABLE IF NOT EXISTS workboard_card_links (
238244
id TEXT PRIMARY KEY,
@@ -244,6 +250,8 @@ const WORKBOARD_SCHEMA_SQL = `
244250
url TEXT,
245251
created_at INTEGER NOT NULL
246252
) STRICT;
253+
CREATE INDEX IF NOT EXISTS workboard_card_links_card_idx
254+
ON workboard_card_links(card_id, ordinal);
247255
248256
CREATE TABLE IF NOT EXISTS workboard_card_proof (
249257
id TEXT PRIMARY KEY,
@@ -256,6 +264,8 @@ const WORKBOARD_SCHEMA_SQL = `
256264
note TEXT,
257265
created_at INTEGER NOT NULL
258266
) STRICT;
267+
CREATE INDEX IF NOT EXISTS workboard_card_proof_card_idx
268+
ON workboard_card_proof(card_id, ordinal);
259269
260270
CREATE TABLE IF NOT EXISTS workboard_card_artifacts (
261271
id TEXT PRIMARY KEY,
@@ -267,6 +277,8 @@ const WORKBOARD_SCHEMA_SQL = `
267277
mime_type TEXT,
268278
created_at INTEGER NOT NULL
269279
) STRICT;
280+
CREATE INDEX IF NOT EXISTS workboard_card_artifacts_card_idx
281+
ON workboard_card_artifacts(card_id, ordinal);
270282
271283
CREATE TABLE IF NOT EXISTS workboard_card_diagnostics (
272284
card_id TEXT NOT NULL REFERENCES workboard_cards(id) ON DELETE CASCADE,
@@ -293,6 +305,8 @@ const WORKBOARD_SCHEMA_SQL = `
293305
session_key TEXT,
294306
run_id TEXT
295307
) STRICT;
308+
CREATE INDEX IF NOT EXISTS workboard_card_notifications_card_idx
309+
ON workboard_card_notifications(card_id, ordinal);
296310
297311
CREATE TABLE IF NOT EXISTS workboard_worker_logs (
298312
id TEXT PRIMARY KEY,
@@ -304,6 +318,8 @@ const WORKBOARD_SCHEMA_SQL = `
304318
session_key TEXT,
305319
run_id TEXT
306320
) STRICT;
321+
CREATE INDEX IF NOT EXISTS workboard_worker_logs_card_idx
322+
ON workboard_worker_logs(card_id, ordinal);
307323
308324
CREATE TABLE IF NOT EXISTS workboard_worker_protocol (
309325
card_id TEXT PRIMARY KEY REFERENCES workboard_cards(id) ON DELETE CASCADE,

‎extensions/workboard/src/store.test.ts‎

Lines changed: 120 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import path from "node:path";
55
import { DatabaseSync } from "node:sqlite";
66
import { type WorkboardCard, WORKBOARD_STATUSES } from "@openclaw/workboard-contract";
77
import { MAX_DATE_TIMESTAMP_MS } from "openclaw/plugin-sdk/number-runtime";
8-
import { describe, expect, it, vi } from "vitest";
8+
import { useAutoCleanupTempDirTracker } from "openclaw/plugin-sdk/test-env";
9+
import { afterEach, describe, expect, it, vi } from "vitest";
910
import type {
1011
PersistedWorkboardAttachment,
1112
PersistedWorkboardBoard,
@@ -200,6 +201,45 @@ function statfsFixture(type: number): ReturnType<typeof fs.statfsSync> {
200201
};
201202
}
202203

204+
const WORKBOARD_CARD_CHILD_INDEXES = [
205+
["workboard_card_events", "workboard_card_events_card_idx"],
206+
["workboard_card_attempts", "workboard_card_attempts_card_idx"],
207+
["workboard_card_comments", "workboard_card_comments_card_idx"],
208+
["workboard_card_links", "workboard_card_links_card_idx"],
209+
["workboard_card_proof", "workboard_card_proof_card_idx"],
210+
["workboard_card_artifacts", "workboard_card_artifacts_card_idx"],
211+
["workboard_card_notifications", "workboard_card_notifications_card_idx"],
212+
["workboard_worker_logs", "workboard_worker_logs_card_idx"],
213+
] as const;
214+
215+
const tempDirs = useAutoCleanupTempDirTracker(afterEach);
216+
217+
function explainWorkboardQueryPlan(
218+
db: DatabaseSync,
219+
sql: string,
220+
params: readonly (number | string | null)[] = [],
221+
): string {
222+
const rows = db.prepare(`EXPLAIN QUERY PLAN ${sql}`).all(...params) as Array<{
223+
detail?: unknown;
224+
}>;
225+
return rows
226+
.map((row) => (typeof row.detail === "string" ? row.detail : JSON.stringify(row.detail ?? "")))
227+
.join("\n");
228+
}
229+
230+
function withWorkboardSqliteDatabase(prefix: string, run: (db: DatabaseSync) => void): void {
231+
const dir = tempDirs.make(prefix);
232+
const dbPath = path.join(dir, "workboard.sqlite");
233+
const stores = createWorkboardSqliteStores({ dbPath });
234+
stores.close();
235+
const db = new DatabaseSync(dbPath);
236+
try {
237+
run(db);
238+
} finally {
239+
db.close();
240+
}
241+
}
242+
203243
describe("WorkboardStore", () => {
204244
it("emits one monotonic change after each visible mutation", async () => {
205245
const store = new WorkboardStore(createMemoryStore());
@@ -542,6 +582,85 @@ describe("WorkboardStore", () => {
542582
expect(restored.metadata?.archivedAt).toBeUndefined();
543583
});
544584

585+
it("uses card child indexes for per-card ordered reads", () => {
586+
withWorkboardSqliteDatabase("openclaw-workboard-index-read-", (db) => {
587+
for (const [table, index] of WORKBOARD_CARD_CHILD_INDEXES) {
588+
const plan = explainWorkboardQueryPlan(
589+
db,
590+
`SELECT * FROM ${table} WHERE card_id = ? ORDER BY ordinal ASC`,
591+
["card-1"],
592+
);
593+
expect(plan).toContain(index);
594+
expect(plan).not.toContain("USE TEMP B-TREE FOR ORDER BY");
595+
}
596+
});
597+
});
598+
599+
it("uses card child indexes for whole-board ordered scans", () => {
600+
withWorkboardSqliteDatabase("openclaw-workboard-index-scan-", (db) => {
601+
for (const [table, index] of WORKBOARD_CARD_CHILD_INDEXES) {
602+
const plan = explainWorkboardQueryPlan(
603+
db,
604+
`SELECT * FROM ${table} ORDER BY card_id ASC, ordinal ASC`,
605+
);
606+
expect(plan).toContain(index);
607+
expect(plan).not.toContain("USE TEMP B-TREE FOR ORDER BY");
608+
}
609+
});
610+
});
611+
612+
it("uses card child indexes for parent-card cascades", () => {
613+
withWorkboardSqliteDatabase("openclaw-workboard-index-cascade-", (db) => {
614+
db.exec("PRAGMA foreign_keys = ON");
615+
const plan = explainWorkboardQueryPlan(db, "DELETE FROM workboard_cards WHERE id = ?", [
616+
"card-1",
617+
]);
618+
for (const [, index] of WORKBOARD_CARD_CHILD_INDEXES) {
619+
expect(plan).toContain(index);
620+
}
621+
});
622+
});
623+
624+
it("restores dropped card child indexes without changing the schema version", () => {
625+
const dir = tempDirs.make("openclaw-workboard-index-reopen-");
626+
const dbPath = path.join(dir, "workboard.sqlite");
627+
const initialized = createWorkboardSqliteStores({ dbPath });
628+
initialized.close();
629+
const db = new DatabaseSync(dbPath);
630+
let initialMigrationIds: Array<{ id: string }>;
631+
try {
632+
initialMigrationIds = db
633+
.prepare("SELECT id FROM workboard_schema_migrations ORDER BY id")
634+
.all() as Array<{ id: string }>;
635+
for (const [, index] of WORKBOARD_CARD_CHILD_INDEXES) {
636+
db.exec(`DROP INDEX ${index}`);
637+
}
638+
} finally {
639+
db.close();
640+
}
641+
642+
const reopened = createWorkboardSqliteStores({ dbPath });
643+
reopened.close();
644+
const verified = new DatabaseSync(dbPath, { readOnly: true });
645+
try {
646+
const indexes = new Set(
647+
(
648+
verified.prepare("SELECT name FROM sqlite_schema WHERE type = 'index'").all() as Array<{
649+
name: string;
650+
}>
651+
).map((row) => row.name),
652+
);
653+
for (const [, index] of WORKBOARD_CARD_CHILD_INDEXES) {
654+
expect(indexes).toContain(index);
655+
}
656+
expect(
657+
verified.prepare("SELECT id FROM workboard_schema_migrations ORDER BY id").all(),
658+
).toEqual(initialMigrationIds);
659+
} finally {
660+
verified.close();
661+
}
662+
});
663+
545664
it("persists boards, cards, subscriptions, and attachment blobs in sqlite", async () => {
546665
const dir = fs.mkdtempSync(path.join(os.tmpdir(), "openclaw-workboard-sqlite-"));
547666
const dbPath = path.join(dir, "workboard.sqlite");

0 commit comments

Comments
 (0)